Beispiel #1
0
static long s_sockexRead( PHB_SOCKEX pSock, void * data, long len, HB_MAXINT timeout )
{
   long lRead = HB_MIN( pSock->inbuffer, len );

   if( lRead > 0 )
   {
      memcpy( data, pSock->buffer + pSock->posbuffer, lRead );
      pSock->inbuffer -= lRead;
      if( pSock->inbuffer )
         pSock->posbuffer += lRead;
      else
         pSock->posbuffer = 0;
      len -= lRead;
      if( len == 0 || pSock->sd == HB_NO_SOCKET )
         return lRead;
      data = ( HB_BYTE * ) data + lRead;
      timeout = 0;
   }
   else if( pSock->sd == HB_NO_SOCKET )
   {
      hb_socketSetError( HB_SOCKET_ERR_INVALIDHANDLE );
      return -1;
   }

   len = pSock->cargo ? hb_znetRead( HB_ZNET_GET( pSock ), pSock->sd, data, len, timeout ) :
                        hb_socketRecv( pSock->sd, data, len, 0, timeout );

   return lRead > 0 ? HB_MAX( len, 0 ) + lRead : len;
}
Beispiel #2
0
static int s_sockexCanRead( PHB_SOCKEX pSock, HB_BOOL fBuffer, HB_MAXINT timeout )
{
   if( pSock->inbuffer )
      return 1;
   else if( pSock->sd == HB_NO_SOCKET )
   {
      hb_socketSetError( HB_SOCKET_ERR_INVALIDHANDLE );
      return -1;
   }
   else if( pSock->cargo )
   {
      long len;

      if( pSock->buffer == NULL )
      {
         if( pSock->readahead <= 0 )
            pSock->readahead = HB_ZNET_READAHEAD;
         pSock->buffer = ( HB_BYTE * ) hb_xgrab( pSock->readahead );
      }
      len = hb_znetRead( HB_ZNET_GET( pSock ), pSock->sd, pSock->buffer,
                         pSock->readahead, 0 );
      if( len > 0 )
      {
         pSock->inbuffer = len;
         len = 1;
      }
      return ( int ) len;
   }
   return fBuffer ? 0 : hb_socketSelectRead( pSock->sd, timeout );
}
Beispiel #3
0
static long s_srvRecvAll( PHB_CONSRV conn, void * buffer, long len )
{
   HB_BYTE * ptr = ( HB_BYTE * ) buffer;
   long lRead = 0, l;
   HB_MAXUINT end_timer;

   end_timer = conn->timeout > 0 ? hb_dateMilliSeconds() + conn->timeout : 0;

   while( lRead < len && ! conn->stop )
   {
      if( conn->zstream )
         l = hb_znetRead( conn->zstream, conn->sd, ptr + lRead, len - lRead, 1000 );
      else
         l = hb_socketRecv( conn->sd, ptr + lRead, len - lRead, 0, 1000 );
      if( l <= 0 )
      {
         if( hb_socketGetError() != HB_SOCKET_ERR_TIMEOUT ||
             hb_vmRequestQuery() != 0 ||
             ( end_timer != 0 && end_timer <= hb_dateMilliSeconds() ) )
            break;
      }
      else
      {
         lRead += l;
         conn->rd_count += l;
      }
   }

   return lRead;
}