int guac_user_handle_connection()

in src/libguac/user-handshake.c [292:384]


int guac_user_handle_connection(guac_user* user, int usec_timeout) {

    guac_socket* socket = user->socket;
    guac_client* client = user->client;
    
    user->info.audio_mimetypes = NULL;
    user->info.image_mimetypes = NULL;
    user->info.video_mimetypes = NULL;
    user->info.name = NULL;
    user->info.timezone = NULL;
    
    /* Count number of arguments. */
    int num_args;
    for (num_args = 0; client->args[num_args] != NULL; num_args++);
    
    /* Send args */
    if (guac_protocol_send_args(socket, client->args)
            || guac_socket_flush(socket)) {

        /* Log error */
        guac_user_log_handshake_failure(user);
        guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
                "Error sending \"args\" to new user");

        return 1;
    }

    guac_parser* parser = guac_parser_alloc();

    /* Perform the handshake with the client. */
    if (__guac_user_handshake(user, parser, usec_timeout)) {
        guac_parser_free(parser);
        return 1;
    }

    /* Acknowledge connection availability */
    guac_protocol_send_ready(socket, client->connection_id);
    guac_socket_flush(socket);
    
    /* Verify argument count. */
    if (parser->argc != (num_args + 1)) {
        guac_client_log(client, GUAC_LOG_ERROR, "Client did not return the "
                "expected number of arguments.");
        return 1;
    }
    
    /* Attempt to join user to connection. */
    if (guac_client_add_user(client, user, (parser->argc - 1), parser->argv + 1))
        guac_client_log(client, GUAC_LOG_ERROR, "User \"%s\" could NOT "
                "join connection \"%s\"", user->user_id, client->connection_id);

    /* Begin user connection if join successful */
    else {

        guac_client_log(client, GUAC_LOG_INFO, "User \"%s\" joined connection "
                "\"%s\" (%i users now present)", user->user_id,
                client->connection_id, client->connected_users);
        if (strcmp(parser->argv[0],"") != 0) {
            guac_client_log(client, GUAC_LOG_DEBUG, "Client is using protocol "
                    "version \"%s\"", parser->argv[0]);
            user->info.protocol_version = guac_protocol_string_to_version(parser->argv[0]);
        }
        else {
            guac_client_log(client, GUAC_LOG_DEBUG, "Client has not defined "
                    "its protocol version.");
            user->info.protocol_version = GUAC_PROTOCOL_VERSION_1_0_0;
        }

        /* Handle user I/O, wait for connection to terminate */
        guac_user_start(parser, user, usec_timeout);

        /* Remove/free user */
        guac_client_remove_user(client, user);
        guac_client_log(client, GUAC_LOG_INFO, "User \"%s\" disconnected (%i "
                "users remain)", user->user_id, client->connected_users);

    }
    
    /* Free mimetype character arrays. */
    guac_free_mimetypes((char **) user->info.audio_mimetypes);
    guac_free_mimetypes((char **) user->info.image_mimetypes);
    guac_free_mimetypes((char **) user->info.video_mimetypes);
    
    /* Free name and timezone info. */
    guac_mem_free_const(user->info.name);
    guac_mem_free_const(user->info.timezone);
    
    guac_parser_free(parser);

    /* Successful disconnect */
    return 0;

}