Пример #1
0
static bool us_tool_send_osc_tcpslip(
    struct addrinfo *dest_addr,
    us_buffer_t *buf
)
{
    bool r=false;
    int s;
    uint8_t slipped_mem[4096];
    int l;
    us_buffer_t slipped_buffer;
    us_buffer_init(&slipped_buffer, 0, slipped_mem, sizeof( slipped_mem ) );
    us_slip_encode(&slipped_buffer, buf);
    l = us_buffer_readable_count( &slipped_buffer );
    s = us_net_create_tcp_socket(dest_addr, false);
    if ( s>=0 )
    {
        if ( connect(s, dest_addr->ai_addr, dest_addr->ai_addrlen)>=0 )
        {
            r=true;
            r&=us_net_blocking_send(s, slipped_buffer.m_buffer, l );
            if ( !r )
            {
                us_stderr->printf( us_stderr, "Error sending slipped packet to TCP socket: %s\n", strerror(errno) );
            }
        }
        else
        {
            r=false;
            us_stderr->printf( us_stderr, "Error connecting: %s\n", strerror(errno) );
        }
        closesocket(s);
    }
    return r;
}
Пример #2
0
int us_socket_collection_add_tcp_client( us_socket_collection_t *self,
                                         const char *remote_addr_name,
                                         const char *remote_port_name,
                                         void *context )
{
    int fd = -1;

    // make sure we have room before trying anything
    if ( self->num_sockets < US_SOCKET_COLLECTION_MAX_SOCKETS )
    {
        // get the address of the remote
        struct addrinfo *remote_addr;
        remote_addr = us_net_get_addrinfo( remote_addr_name, remote_port_name, SOCK_STREAM, false );

        // create the tcp client socket for this kind of address
        fd = us_net_create_tcp_socket( remote_addr, false );

        // did it work?
        if ( fd != -1 )
        {
            // yes, try initiate a connect
            int con_result;

            // we are doing a non blocking connect
            us_net_set_socket_nonblocking( fd );

            // try connect, ignore signals during this time
            do
            {
                con_result = connect( fd, remote_addr->ai_addr, remote_addr->ai_addrlen );
            } while ( con_result == -1 && errno == EINTR );

            // If the connect succeeded or it is in progress, then track this fd
            if ( con_result == 0 || ( con_result == -1 && errno == EINPROGRESS ) )
            {
                // Add the fd to the collection
                if ( !us_socket_collection_add_fd( self, fd, context ) )
                {
                    // unable to add the fd to the collection, close the socket
                    closesocket( fd );
                    fd = -1;
                }
            }
        }
    }
    return fd;
}
Пример #3
0
static bool us_tool_send_osc_tcp(
    struct addrinfo *dest_addr,
    us_buffer_t *buf
)
{
    bool r=false;
    int s = us_net_create_tcp_socket(dest_addr, false);
    if ( s>=0 )
    {
        if ( connect(s, dest_addr->ai_addr, dest_addr->ai_addrlen)>=0 )
        {
            uint8_t length[4];
            int len=us_buffer_readable_count( buf );
            r=true;
            length[0] = US_GET_BYTE_3( len );
            length[1] = US_GET_BYTE_2( len );
            length[2] = US_GET_BYTE_1( len );
            length[3] = US_GET_BYTE_0( len );
            r&=us_net_blocking_send(s, length, sizeof(length) );
            if ( r )
            {
                r&=us_net_blocking_send(s, buf->m_buffer, len );
            }
            if ( !r )
            {
                us_stderr->printf( us_stderr, "Error sending to TCP socket: %s\n", strerror(errno) );
            }
        }
        else
        {
            r=false;
            us_stderr->printf( us_stderr, "Error connecting: %s\n", strerror(errno) );
        }
        closesocket(s);
    }
    return r;
}