Beispiel #1
0
static int recv( int handle, void *buf, int size, int timeout )
{
    int count = 0;
    while( !RdosIsTcpConnectionClosed( handle ) && count == 0 ) {
        count = RdosReadTcpConnection( handle, buf, size );
    }
    return( count );
}
Beispiel #2
0
trap_retval RemoteGet( byte *rec, trap_elen len )
{
    unsigned_16         rec_len;

    len = len;

    _DBG_NET(("RemoteGet\r\n"));

#ifdef __RDOS__
    if( !IS_INVALID_SOCKET( data_socket ) && !RdosIsTcpConnectionClosed( data_socket ) ) {
#else
    if( !IS_INVALID_SOCKET( data_socket ) ) {
#endif
        if( FullGet( &rec_len, sizeof( rec_len ) ) == sizeof( rec_len ) ) {
            CONV_LE_16( rec_len );
            if( rec_len == 0 || FullGet( rec, rec_len ) == rec_len ) {
                _DBG_NET(("Got a packet - size=%d\r\n", rec_len));
                return( rec_len );
            }
        }
    }
    return( REQUEST_FAILED );
}

trap_retval RemotePut( byte *rec, trap_elen len )
{
    unsigned_16         send_len;

    _DBG_NET(("RemotePut\r\n"));

#ifdef __RDOS__
    if( !IS_INVALID_SOCKET( data_socket ) && !RdosIsTcpConnectionClosed( data_socket ) ) {
#else
    if( !IS_INVALID_SOCKET( data_socket ) ) {
#endif
        send_len = len;
        CONV_LE_16( send_len );
        if( !IS_SOCK_ERROR( send( data_socket, (void *)&send_len, sizeof( send_len ), 0 ) ) ) {
            if( len == 0 || !IS_SOCK_ERROR( send( data_socket, (void *)rec, len, 0 ) ) ) {
#ifdef __RDOS__
                 RdosPushTcpConnection( data_socket );
#endif
                _DBG_NET(("RemotePut...OK\r\n"));
                return( len );
            }
        }
    }
    return( REQUEST_FAILED );
}

#ifndef __RDOS__

static void nodelay( void )
{
    struct protoent     *proto;
    int                 delayoff;
    int                 p;

    delayoff = 1;
    proto = getprotobyname( "tcp" );
    p = proto ? proto->p_proto : IPPROTO_TCP;
    setsockopt( data_socket, p, TCP_NODELAY, (void *)&delayoff, sizeof( delayoff ) );
}

#endif

bool RemoteConnect( void )
{
#ifdef SERVER
  #ifdef __RDOS__
    void *obj;

    obj = RdosWaitTimeout( wait_handle, 250 );
    if( obj != NULL ) {
        data_socket = RdosGetTcpListen( listen_handle );
        if( !IS_INVALID_SOCKET( data_socket ) ) {
            _DBG_NET(("Found a connection\r\n"));
            return( TRUE );
        }
    }
  #else
    struct          timeval timeout;
    fd_set          ready;
    struct          sockaddr dummy;
    trp_socklen     dummy_len = sizeof( dummy );

    FD_ZERO( &ready );
    FD_SET( control_socket, &ready );
    timeout.tv_sec = 0;
    timeout.tv_usec = 10000;
    if( select( control_socket + 1, &ready, 0, 0, &timeout ) > 0 ) {
        data_socket = accept( control_socket, &dummy, &dummy_len );
        if( !IS_INVALID_SOCKET( data_socket ) ) {
            nodelay();
            _DBG_NET(("Found a connection\r\n"));
            return( TRUE );
        }
    }
  #endif
#else
  #ifdef __RDOS__
    // todo: Add code for connect!
  #else
    data_socket = socket( AF_INET, SOCK_STREAM, 0 );
    if( !IS_INVALID_SOCKET( data_socket ) ) {
        if( connect( data_socket, (struct sockaddr TRAPFAR *)&socket_address, sizeof( socket_address ) ) >= 0 ) {
            nodelay();
            return( TRUE );
        }
    }
  #endif
#endif
    return( FALSE );
}