Пример #1
0
trap_retval RemoteGet( void *data, trap_elen len )
{
    unsigned_16         rec_len;

    len = len;

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

    if( IS_VALID_SOCKET( data_socket ) ) {
        int     size;

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

trap_retval RemotePut( void *data, trap_elen len )
{
    unsigned_16         send_len;
    int                 snd;

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

    if( IS_VALID_SOCKET( data_socket ) ) {
        send_len = len;
        CONV_LE_16( send_len );
        snd = send( data_socket, (void *)&send_len, sizeof( send_len ), 0 );
        if( IS_RET_OK( snd ) ) {
            if( len != 0 )
                snd = send( data_socket, data, len, 0 );
            if( len == 0 || IS_RET_OK( snd ) ) {
#ifdef __RDOS__
                RdosPushTcpConnection( data_socket );
#endif
                _DBG_NET(("RemotePut...OK\r\n"));
                return( len );
            }
        }
    }
    return( REQUEST_FAILED );
}
Пример #2
0
static bool Terminate( void )
{
#ifdef __RDOS__
    RdosPushTcpConnection( data_socket );
#else
    struct linger       linger;

    linger.l_onoff = 1;
    linger.l_linger = 0;
    setsockopt( data_socket, (int)SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof( linger ) );
#endif
    soclose( data_socket );
    data_socket = INVALID_SOCKET;
    return( true );
}
Пример #3
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 );
}