コード例 #1
0
ファイル: TCP_server.c プロジェクト: ameenross/toxcore
static int do_incoming(TCP_Server *TCP_server, uint32_t i)
{
    if (TCP_server->incomming_connection_queue[i].status != TCP_STATUS_CONNECTED)
        return -1;

    int ret = read_connection_handshake(&TCP_server->incomming_connection_queue[i], TCP_server->secret_key);

    if (ret == -1) {
        kill_TCP_connection(&TCP_server->incomming_connection_queue[i]);
    } else if (ret == 1) {
        int index_new = TCP_server->unconfirmed_connection_queue_index % MAX_INCOMMING_CONNECTIONS;
        TCP_Secure_Connection *conn_old = &TCP_server->incomming_connection_queue[i];
        TCP_Secure_Connection *conn_new = &TCP_server->unconfirmed_connection_queue[index_new];

        if (conn_new->status != TCP_STATUS_NO_STATUS)
            kill_TCP_connection(conn_new);

        memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection));
        memset(conn_old, 0, sizeof(TCP_Secure_Connection));
        ++TCP_server->unconfirmed_connection_queue_index;

        return index_new;
    }

    return -1;
}
コード例 #2
0
static void do_TCP_incomming(TCP_Server *TCP_server)
{
    uint32_t i;

    for (i = 0; i < MAX_INCOMMING_CONNECTIONS; ++i) {
        if (TCP_server->incomming_connection_queue[i].status != TCP_STATUS_CONNECTED)
            continue;

        int ret = read_connection_handshake(&TCP_server->incomming_connection_queue[i], TCP_server->secret_key);

        if (ret == -1) {
            kill_TCP_connection(&TCP_server->incomming_connection_queue[i]);
        } else if (ret == 1) {
            TCP_Secure_Connection *conn_old = &TCP_server->incomming_connection_queue[i];
            TCP_Secure_Connection *conn_new =
                &TCP_server->unconfirmed_connection_queue[TCP_server->unconfirmed_connection_queue_index % MAX_INCOMMING_CONNECTIONS];

            if (conn_new->status != TCP_STATUS_NO_STATUS)
                kill_TCP_connection(conn_new);

            memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection));
            memset(conn_old, 0, sizeof(TCP_Secure_Connection));
            ++TCP_server->unconfirmed_connection_queue_index;
        }
    }
}