Esempio n. 1
0
static long s_inetRecv( PHB_SOCKET_STRUCT socket, char * buffer, long size, HB_BOOL readahead )
{
   long rec = 0;

   if( readahead && socket->inbuffer == 0 && socket->readahead > size )
   {
      if( socket->buffer == NULL )
         socket->buffer = ( char * ) hb_xgrab( socket->readahead );
      socket->posbuffer = 0;
      if( socket->recvFunc )
         rec = socket->recvFunc( socket->stream, socket->sd,
                                 socket->buffer, socket->readahead,
                                 socket->iTimeout );
      else
         rec = hb_socketRecv( socket->sd, socket->buffer, socket->readahead,
                              0, socket->iTimeout );
      socket->inbuffer = HB_MAX( 0, rec );
   }
   else
      readahead = HB_FALSE;

   if( socket->inbuffer > 0 )
   {
      rec = HB_MIN( size, socket->inbuffer );
      memcpy( buffer, socket->buffer + socket->posbuffer, rec );
      socket->posbuffer += rec;
      socket->inbuffer -= rec;
      if( size > rec && ! readahead )
      {
         if( socket->recvFunc )
            rec = socket->recvFunc( socket->stream, socket->sd,
                                    buffer + rec, size - rec,
                                    socket->iTimeout );
         else
            size = hb_socketRecv( socket->sd, buffer + rec, size - rec, 0, 0 );

         if( size > 0 )
            rec += size;
      }
   }
   else if( ! readahead )
   {
      if( socket->recvFunc )
         rec = socket->recvFunc( socket->stream, socket->sd,
                                 buffer, size, socket->iTimeout );
      else
         rec = hb_socketRecv( socket->sd, buffer, size, 0, socket->iTimeout );
   }

   return rec;
}
Esempio n. 2
0
static void hb_inetCloseStream( PHB_SOCKET_STRUCT socket )
{
   if( socket->flushFunc && socket->sd != HB_NO_SOCKET )
      socket->flushFunc( socket->stream, socket->sd,
                         HB_MAX( socket->iTimeout, 10000 ), HB_TRUE );

   if( socket->cleanFunc )
      socket->cleanFunc( socket->stream );

   socket->recvFunc = NULL;
   socket->sendFunc = NULL;
   socket->flushFunc = NULL;
   socket->cleanFunc = NULL;
   socket->stream = NULL;
}
Esempio n. 3
0
static void hb_inetCloseStream( PHB_SOCKET_STRUCT socket )
{
   if( socket->cleanFunc )
      socket->cleanFunc( socket->stream );

   socket->recvFunc = NULL;
   socket->sendFunc = NULL;
   socket->flushFunc = NULL;
   socket->cleanFunc = NULL;
   socket->stream = NULL;
}
Esempio n. 4
0
static int s_inetGetError( PHB_SOCKET_STRUCT socket )
{
   int iError;

   iError = socket->errorFunc ? socket->errorFunc( socket->stream ) :
                                hb_socketGetError();

   if( iError == HB_SOCKET_ERR_TIMEOUT )
      iError = HB_INET_ERR_TIMEOUT;

   return iError;
}
Esempio n. 5
0
static void s_inetSendInternal( HB_BOOL lAll )
{
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 1 );
   PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING );
   const char * buffer;
   int iLen, iSent, iSend;
   long lLastSnd = 1;

   if( socket == NULL || pBuffer == NULL )
      hb_inetErrRT();
   else if( ! hb_inetIsOpen( socket ) )
      hb_retni( -1 );
   else
   {
      buffer = hb_itemGetCPtr( pBuffer );
      iSend = ( int ) hb_itemGetCLen( pBuffer );
      if( HB_ISNUM( 3 ) )
      {
         iLen = hb_parni( 3 );
         if( iLen < iSend )
            iSend = iLen;
      }

      socket->iError = HB_INET_ERR_OK;

      iSent = iLen = 0;
      while( iSent < iSend )
      {
         if( socket->sendFunc )
         {
            iLen = socket->sendFunc( socket->stream, socket->sd,
                                     buffer + iSent, iSend - iSent,
                                     socket->iTimeout, &lLastSnd );
            if( lLastSnd <= 0 && iLen > 0 )
            {
               iSent += iLen;
               iLen = ( int ) lLastSnd;
            }
         }
         else
            iLen = hb_socketSend( socket->sd, buffer + iSent, iSend - iSent,
                                  0, socket->iTimeout );
         if( iLen > 0 )
         {
            iSent += iLen;
            if( ! lAll )
               break;
         }
         else
         {
            hb_inetGetError( socket );
            break;
         }
      }
      socket->iCount = iSent;

      if( socket->flushFunc && ( lLastSnd > 0 || ( lLastSnd == -1 &&
             socket->iTimeout >= 0 && socket->iTimeout < 10000 &&
             s_inetIsTimeout( socket ) ) ) )
      {
         /* TODO: safe information about unflushed data and try to call
                  flush before entering receive wait sate */
         socket->flushFunc( socket->stream, socket->sd, socket->iTimeout < 0 ?
                            socket->iTimeout : HB_MAX( socket->iTimeout, 10000 ),
                            HB_FALSE );
      }

      hb_retni( iSent > 0 ? iSent : iLen );
   }
}