/* write data to extended socket */ static long s_sockexWrite( PHB_SOCKEX pSock, const void * data, long len, HB_MAXINT timeout ) { if( pSock->sd == HB_NO_SOCKET ) { hb_socketSetError( HB_SOCKET_ERR_INVALIDHANDLE ); return -1; } return hb_socketSend( pSock->sd, data, len, 0, timeout ); }
static long s_sockexWrite( PHB_SOCKEX pSock, const void * data, long len, HB_MAXINT timeout ) { if( pSock->sd == HB_NO_SOCKET ) { hb_socketSetError( HB_SOCKET_ERR_INVALIDHANDLE ); return -1; } return pSock->cargo ? hb_znetWrite( HB_ZNET_GET( pSock ), pSock->sd, data, len, timeout, NULL ) : hb_socketSend( pSock->sd, data, len, 0, timeout ); }
static HB_SIZE s_fileWrite( PHB_FILE pFile, const void * data, HB_SIZE nSize, HB_MAXINT timeout ) { long lSend = nSize > LONG_MAX ? LONG_MAX : ( long ) nSize; if( timeout == -1 ) timeout = pFile->timeout; lSend = hb_socketSend( pFile->sd, data, lSend, 0, timeout ); hb_fsSetError( hb_socketGetError() ); if( lSend < 0 ) lSend = 0; return lSend; }
HB_BOOL hb_lppSend( PHB_LPP pSocket, const void * data, HB_SIZE len, HB_MAXINT timeout ) { HB_MAXINT nTime = 0; long lSend; if( ! pSocket->pSendBuffer ) { pSocket->pSendBuffer = ( char * ) hb_xgrab( len + 4 ); HB_PUT_LE_UINT32( pSocket->pSendBuffer, len ); hb_xmemcpy( pSocket->pSendBuffer + 4, data, len ); pSocket->nSendLen = len + 4; pSocket->nSendPos = 0; } if( timeout > 0 ) nTime = ( HB_MAXINT ) hb_dateMilliSeconds() + timeout; for( ;; ) { if( pSocket->nSendLen - pSocket->nSendPos < ( HB_SIZE ) LONG_MAX ) lSend = ( long ) ( pSocket->nSendLen - pSocket->nSendPos ); else lSend = LONG_MAX; lSend = hb_socketSend( pSocket->sd, pSocket->pSendBuffer + pSocket->nSendPos, lSend, 0, timeout ); if( lSend == -1 ) { pSocket->iError = hb_socketGetError(); return HB_FALSE; } pSocket->nSendPos += lSend; if( pSocket->nSendPos == pSocket->nSendLen ) { hb_xfree( pSocket->pSendBuffer ); pSocket->pSendBuffer = NULL; pSocket->iError = 0; return HB_TRUE; } if( timeout == 0 || ( timeout > 0 && ( timeout = nTime - ( HB_MAXINT ) hb_dateMilliSeconds() ) <= 0 ) ) { pSocket->iError = HB_SOCKET_ERR_TIMEOUT; return HB_FALSE; } } }
static long s_srvSendAll( PHB_CONSRV conn, void * buffer, long len ) { HB_BYTE * ptr = ( HB_BYTE * ) buffer; long lSent = 0, lLast = 1, l; HB_MAXUINT end_timer; if( ! conn->mutex || hb_threadMutexLock( conn->mutex ) ) { end_timer = conn->timeout > 0 ? hb_dateMilliSeconds() + conn->timeout : 0; while( lSent < len && ! conn->stop ) { if( conn->zstream ) l = hb_znetWrite( conn->zstream, conn->sd, ptr + lSent, len - lSent, 1000, &lLast ); else l = lLast = hb_socketSend( conn->sd, ptr + lSent, len - lSent, 0, 1000 ); if( l > 0 ) { lSent += l; conn->wr_count += l; } if( lLast <= 0 ) { if( hb_socketGetError() != HB_SOCKET_ERR_TIMEOUT || hb_vmRequestQuery() != 0 || ( end_timer != 0 && end_timer <= hb_dateMilliSeconds() ) ) break; } } if( conn->zstream && lLast > 0 && ! conn->stop ) { if( hb_znetFlush( conn->zstream, conn->sd, conn->timeout > 0 ? conn->timeout : -1 ) != 0 ) lSent = -1; } if( conn->mutex ) hb_threadMutexUnlock( conn->mutex ); } return lSent; }
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 ); } }
static long hb_znetStreamWrite( PHB_ZNETSTREAM pStream, HB_SOCKET sd, HB_MAXINT timeout ) { long tosnd = HB_ZNET_BUFSIZE - pStream->wr.avail_out; long snd = 0, rest = 0; if( pStream->crypt ) { long size = ( long ) ( pStream->wr.next_out - pStream->crypt_out ); if( size > 2 ) { HB_U16 uiLen = ( HB_U16 ) ( size - 2 ); HB_PUT_BE_UINT16( pStream->crypt_out, uiLen ); uiLen = ( HB_U16 ) ( ( ( size + 7 ) ^ 0x07 ) & 0x07 ); if( ( uInt ) uiLen > pStream->wr.avail_out ) { /* it may happen only if encryption was enabled in non empty * buffer and the unencrypted part has not been flushed yet. */ rest = size; } else { while( uiLen-- ) { *pStream->wr.next_out++ = ( Byte ) 0; /* TODO: use better hashing data */ pStream->wr.avail_out--; size++; } /* encrypt the buffer */ for( tosnd = 0; tosnd < size; tosnd += 8 ) hb_znetEncrypt( pStream, pStream->crypt_out + tosnd ); pStream->crypt_out = pStream->wr.next_out; pStream->wr.next_out += 2; if( pStream->wr.avail_out < 2 ) pStream->skip_out = 2 - pStream->wr.avail_out; pStream->wr.avail_out -= 2 - pStream->skip_out; } tosnd = ( long ) ( pStream->crypt_out - pStream->outbuf ); } else tosnd -= 2; } if( tosnd > 0 ) { snd = hb_socketSend( sd, pStream->outbuf, tosnd, 0, timeout ); if( snd > 0 ) { tosnd += rest - snd; if( tosnd > 0 ) memmove( pStream->outbuf, pStream->outbuf + snd, tosnd ); pStream->wr.avail_out += ( uInt ) snd; pStream->wr.next_out -= snd; pStream->crypt_out -= snd; if( pStream->skip_out ) { if( pStream->skip_out <= pStream->wr.avail_out ) { pStream->wr.avail_out -= pStream->skip_out; pStream->skip_out = 0; } else { pStream->skip_out -= pStream->wr.avail_out; pStream->wr.avail_out = 0; } } } } return snd; }
static long hb_znetStreamWrite( PHB_ZNETSTREAM pStream, HB_SOCKET sd, HB_MAXINT timeout ) { long snd = 0, rest = 0, tosnd; if( pStream->crypt ) { rest = ( long ) ( pStream->wr.next_out - pStream->crypt_out ); if( rest > 2 ) { HB_U16 uiLen = ( HB_U16 ) ( rest - 2 ); HB_PUT_BE_UINT16( pStream->crypt_out, uiLen ); uiLen = ( HB_U16 ) ( ( ( rest + 0x07 ) ^ 0x07 ) & 0x07 ); if( ( uInt ) uiLen <= pStream->wr.avail_out ) { while( uiLen-- ) { *pStream->wr.next_out++ = ( Byte ) 0; /* TODO: use better hashing data */ pStream->wr.avail_out--; rest++; } /* encrypt the buffer */ for( tosnd = 0; tosnd < rest; tosnd += 8 ) hb_znetEncrypt( pStream, pStream->crypt_out + tosnd ); rest = 0; pStream->crypt_out = pStream->wr.next_out; pStream->wr.next_out += 2; if( pStream->wr.avail_out < 2 ) pStream->skip_out = 2 - pStream->wr.avail_out; pStream->wr.avail_out -= 2 - pStream->skip_out; } } else rest = 0; tosnd = ( long ) ( pStream->crypt_out - pStream->outbuf ); } else tosnd = HB_ZNET_BUFSIZE - pStream->wr.avail_out; if( tosnd > 0 ) { snd = hb_socketSend( sd, pStream->outbuf, tosnd, 0, timeout ); if( snd > 0 ) { tosnd += rest - snd; if( tosnd > 0 ) memmove( pStream->outbuf, pStream->outbuf + snd, tosnd ); pStream->wr.avail_out += ( uInt ) snd; pStream->wr.next_out -= snd; pStream->crypt_out -= snd; if( pStream->skip_out ) { if( pStream->skip_out <= pStream->wr.avail_out ) { pStream->wr.avail_out -= pStream->skip_out; pStream->skip_out = 0; } else { pStream->skip_out -= pStream->wr.avail_out; pStream->wr.avail_out = 0; } } } } return snd; }