int socket_transport_connect()

in win32/src/socket_transport_win32.c [314:369]


int socket_transport_connect(SOCKET_TRANSPORT_HANDLE socket_transport, const char* hostname, uint16_t port, uint32_t connection_timeout_ms)
{
    int result;

    // Codes_SOCKET_TRANSPORT_WIN32_09_009: [ If socket_transport is NULL, socket_transport_connect shall fail and return a non-zero value. ]
    if (socket_transport == NULL ||
        // Codes_SOCKET_TRANSPORT_WIN32_09_010: [If hostname is NULL, socket_transport_connect shall fail and return a non - zero value.]
        hostname == NULL ||
        // Codes_SOCKET_TRANSPORT_WIN32_09_011 : [If port is 0, socket_transport_connect shall fail and return a non - zero value.]
        port == 0)
    {
        LogError("Invalid arguments: SOCKET_TRANSPORT_HANDLE socket_transport: %p, const char* hostname: %s, uint16_t port: %" PRIu16 ", uint32_t connection_timeout: %" PRIu32 "",
            socket_transport, MU_P_OR_NULL(hostname), port, connection_timeout_ms);
        result = MU_FAILURE;
    }
    else
    {
        // Codes_SOCKET_TRANSPORT_WIN32_09_012: [ If the socket_transport type is not SOCKET_CLIENT, socket_transport_connect shall fail and return a non-zero value. ]
        if (socket_transport->type != SOCKET_CLIENT)
        {
            LogError("Invalid socket type for this API expected: SOCKET_CLIENT, actual: %" PRI_MU_ENUM "", MU_ENUM_VALUE(SOCKET_TYPE, socket_transport->type));
            result = MU_FAILURE;
        }
        else
        {
            // Codes_SOCKET_TRANSPORT_WIN32_09_013: [ socket_transport_connect shall call sm_open_begin to begin the open. ]
            SM_RESULT open_result = sm_open_begin(socket_transport->sm);

            // Codes_SOCKET_TRANSPORT_WIN32_09_014: [ If sm_open_begin does not return SM_EXEC_GRANTED, socket_transport_connect shall fail and return a non-zero value. ]
            if (open_result != SM_EXEC_GRANTED)
            {
                LogError("sm_open_begin failed with %" PRI_MU_ENUM, MU_ENUM_VALUE(SM_RESULT, open_result));
                result = MU_FAILURE;
            }
            else
            {
                socket_transport->socket = connect_to_client(hostname, port, connection_timeout_ms);

                if (socket_transport->socket == INVALID_SOCKET)
                {
                    LogError("Failure connecting to client hostname: %s:%" PRIu16 "", hostname, port);
                    result = MU_FAILURE;
                    // Codes_SOCKET_TRANSPORT_WIN32_09_018: [ If any failure is encountered, socket_transport_connect shall call sm_open_end with false, fail and return a non-zero value. ]
                    sm_open_end(socket_transport->sm, false);
                }
                else
                {
                    // Codes_SOCKET_TRANSPORT_WIN32_09_017: [ If successful socket_transport_connect shall call sm_open_end with true. ]
                    sm_open_end(socket_transport->sm, true);
                    result = 0;
                }
            }
        }
    }
    return result;
}