STCP/network_io_tcp.c
ssize_t _network_recv_packet(network_context_t *ctx, void *dst, size_t max_len)
{
    network_context_socket_tcp_t *tcp_io_ctx;
    uint16_t packet_len;
    socket_t io_socket;
    int rc;

    assert(ctx && dst);

    tcp_io_ctx = (network_context_socket_tcp_t *) ctx->impl_data;
    assert(tcp_io_ctx);
    assert(tcp_io_ctx->sock_ctx);

    VERIFY_SOCKET(ctx);
    io_socket = tcp_io_ctx->base.socket;

    if (tcp_io_ctx->sock_ctx->is_active && _tcp_connect(ctx) < 0)
        return -1;

    if (tcp_io_ctx->sock_ctx->listening/* ||
        (tcp_io_ctx->sock_ctx->is_active && !tcp_io_ctx->connected)*/)
    {
        socket_t tmp_sd;

        ctx->peer_addr_len = sizeof(ctx->peer_addr);
        if ((tmp_sd = accept(GET_SOCKET(ctx),
                             &ctx->peer_addr,
                             &ctx->peer_addr_len)) < 0)
        {
            perror("accept (network_io_tcp)");
            return tmp_sd;
        }

        DEBUG_LOG(("accepted from peer, tmp_sd=%d...\n", (int) tmp_sd));

        /* keep listening socket open for futher connection requests */
        /* we will not reenter this function until this SYN packet has
         * been dispatched to the right context, and that context's
         * socket updated to be 'new_socket'
         */
        assert(tcp_io_ctx->new_socket == -1);
        tcp_io_ctx->new_socket = tmp_sd;
        io_socket = tmp_sd;
    }

    DEBUG_PEER(ctx);

#ifdef DEBUG
    if (getpeername(io_socket, &ctx->peer_addr, &ctx->peer_addr_len) < 0)
    {
        DEBUG_LOG(("getpeername failed (errno=%d)\n", errno));
        return -1;
    }
#endif

    if ((rc = _tcp_io(io_socket, &packet_len, sizeof(packet_len), read)) <= 0)
    {
        DEBUG_LOG(("couldn't read packet len: %d\n", rc));
        return rc;
    }

    packet_len = ntohs(packet_len);
    if ((rc = _tcp_io(io_socket, dst, MIN(packet_len, max_len), read)) <= 0)
    {
        DEBUG_LOG(("couldn't read packet: %d\n", rc));
        return rc;
    }

    if (packet_len > max_len)
    {
        /* discard unread remainder of packet */
        char *dummy = (char *) alloca(packet_len - max_len);
        (void) _tcp_io(io_socket, dummy, packet_len - max_len, read);
    }

    return packet_len;
}
/* read a packet from the peer */
read a packet from the peer
read a packet from the peer
ssize_t _network_recv_packet(network_context_t *ctx, void *dst, size_t max_len)
{
    network_context_socket_tcp_t *tcp_io_ctx;
    uint16_t packet_len;
    socket_t io_socket;
    int rc;
    assert(ctx && dst);
    tcp_io_ctx = (network_context_socket_tcp_t *) ctx->impl_data;
    assert(tcp_io_ctx);
    assert(tcp_io_ctx->sock_ctx);
    VERIFY_SOCKET(ctx);
    io_socket = tcp_io_ctx->base.socket;
    if (tcp_io_ctx->sock_ctx->is_active && _tcp_connect(ctx) < 0)
        return -1;
    if (tcp_io_ctx->sock_ctx->listening)
    {
        socket_t tmp_sd;
        ctx->peer_addr_len = sizeof(ctx->peer_addr);
        if ((tmp_sd = accept(GET_SOCKET(ctx),
                             &ctx->peer_addr,
                             &ctx->peer_addr_len)) < 0)
        {
            perror("accept (network_io_tcp)");
            return tmp_sd;
        }
        DEBUG_LOG(("accepted from peer, tmp_sd=%d...\n", (int) tmp_sd));
        assert(tcp_io_ctx->new_socket == -1);
        tcp_io_ctx->new_socket = tmp_sd;
        io_socket = tmp_sd;
    }
    DEBUG_PEER(ctx);
#ifdef DEBUG
    if (getpeername(io_socket, &ctx->peer_addr, &ctx->peer_addr_len) < 0)
    {
        DEBUG_LOG(("getpeername failed (errno=%d)\n", errno));
        return -1;
    }
#endif
    if ((rc = _tcp_io(io_socket, &packet_len, sizeof(packet_len), read)) <= 0)
    {
        DEBUG_LOG(("couldn't read packet len: %d\n", rc));
        return rc;
    }
    packet_len = ntohs(packet_len);
    if ((rc = _tcp_io(io_socket, dst, MIN(packet_len, max_len), read)) <= 0)
    {
        DEBUG_LOG(("couldn't read packet: %d\n", rc));
        return rc;
    }
    if (packet_len > max_len)
    {
        char *dummy = (char *) alloca(packet_len - max_len);
        (void) _tcp_io(io_socket, dummy, packet_len - max_len, read);
    }
    return packet_len;
}
ssize_t _network_recv_packet ( network_context_t * ctx , void * dst , size_t max_len ) { network_context_socket_tcp_t * tcp_io_ctx ; uint16_t packet_len ; socket_t io_socket ; int rc ; assert ( ctx && dst ) ; tcp_io_ctx = ( network_context_socket_tcp_t * ) ctx -> impl_data ; assert ( tcp_io_ctx ) ; assert ( tcp_io_ctx -> sock_ctx ) ; VERIFY_SOCKET ( ctx ) ; io_socket = tcp_io_ctx -> base . socket ; if ( tcp_io_ctx -> sock_ctx -> is_active && _tcp_connect ( ctx ) < 0 ) return -1 ; if ( tcp_io_ctx -> sock_ctx -> listening ) { socket_t tmp_sd ; ctx -> peer_addr_len = sizeof ( ctx -> peer_addr ) ; if ( ( tmp_sd = accept ( GET_SOCKET ( ctx ) , & ctx -> peer_addr , & ctx -> peer_addr_len ) ) < 0 ) { perror ( " " ) ; return tmp_sd ; } DEBUG_LOG ( ( " \n " , ( int ) tmp_sd ) ) ; assert ( tcp_io_ctx -> new_socket == -1 ) ; tcp_io_ctx -> new_socket = tmp_sd ; io_socket = tmp_sd ; } DEBUG_PEER ( ctx ) ; #ifdef DEBUG if ( getpeername ( io_socket , & ctx -> peer_addr , & ctx -> peer_addr_len ) < 0 ) { DEBUG_LOG ( ( " \n " , errno ) ) ; return -1 ; } #endif if ( ( rc = _tcp_io ( io_socket , & packet_len , sizeof ( packet_len ) , read ) ) <= 0 ) { DEBUG_LOG ( ( " \n " , rc ) ) ; return rc ; } packet_len = ntohs ( packet_len ) ; if ( ( rc = _tcp_io ( io_socket , dst , MIN ( packet_len , max_len ) , read ) ) <= 0 ) { DEBUG_LOG ( ( " \n " , rc ) ) ; return rc ; } if ( packet_len > max_len ) { char * dummy = ( char * ) alloca ( packet_len - max_len ) ; ( void ) _tcp_io ( io_socket , dummy , packet_len - max_len , read ) ; } return packet_len ; }
read a packet from the peer
read a packet from the peer
/* ||
        (tcp_io_ctx->sock_ctx->is_active && !tcp_io_ctx->connected)*/ /* keep listening socket open for futher connection requests */ /* we will not reenter this function until this SYN packet has
         * been dispatched to the right context, and that context's
         * socket updated to be 'new_socket'
         */ /* discard unread remainder of packet */