Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger CS104_CONNECTION_FAILED event, if tlsSocket fails #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

m-unkel
Copy link
Contributor

@m-unkel m-unkel commented Jan 7, 2025

Issue

When using CS104_Connection_connectAsync() in combination with TLS, the connectionHandler callback does not trigger the CS104_CONNECTION_FAILED event as expected.

Expected behavior

image

Description

Previously, the thread function handleConnection() failed to trigger the CS104_CONNECTION_FAILED event if the tlsSocket creation was unsuccessful.

static void*
handleConnection(void* parameter)
{
    CS104_Connection self = (CS104_Connection) parameter;

    CS104_ConnectionEvent event = CS104_CONNECTION_OPENED;

    resetConnection(self);

    self->socket = TcpSocket_create();

    if (self->socket) {
//     [...] code skipped for readability

        if (Socket_connect(self->socket, self->hostname, self->tcpPort)) {

//         [...] code skipped for readability

            if (self->tlsConfig != NULL) {
                self->tlsSocket = TLSSocket_create(self->socket, self->tlsConfig, false);

                if (self->tlsSocket)
                    self->running = true;
                else
                    self->failure = true;  // <--- this is the problematic case... NOT RUNNING and therefore NO EVENT
            }
            else
                self->running = true;

//     [...] code skipped for readability

            if (isRunning(self)) {

//             [...] code skipped for readability

                /* Call connection handler */
                if (self->connectionHandler)
                    self->connectionHandler(self->connectionHandlerParameter, self, CS104_CONNECTION_OPENED);

//             [...] code skipped for readability

                /* register CLOSED event */
                event = CS104_CONNECTION_CLOSED;
            }
        }
        else {

//         [...] code skipped for readability

            /* register CLOSED event */
            event = CS104_CONNECTION_FAILED;
        }

// [...] code skipped for readability

    /* Call connection handler */
    if ((event == CS104_CONNECTION_CLOSED) || (event == CS104_CONNECTION_FAILED)) {
        if (self->connectionHandler)
            self->connectionHandler(self->connectionHandlerParameter, self, event);
    }

    return NULL;
}

To address this, instead of adding an additional exception handler, the proposed solution sets CS104_CONNECTION_FAILED as the default event unless the connection is successfully established.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant