From 508e2ec93f3d8cd74adff2d0959591bf709595d0 Mon Sep 17 00:00:00 2001 From: Mindaugas Kavaliauskas Date: Thu, 10 Jun 2010 16:12:39 +0000 Subject: [PATCH] 2010-06-10 19:12 UTC+0300 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) * harbour/examples/httpsrv/uhttpd.prg * harbour/examples/uhttpd2/umain.prg * optimized hb_socketRecv() calls * optimized (removed) hb_socketSelect() calls, current API has timeout parameter and make things simple * harbour/examples/commouse/commouse.prg ! fixed error reporting (missing hb_comGetError()) parameter * harbour/include/hbsocket.ch * adjusted comment text * harbour/contrib/hbtpathy/telepath.prg ! fixed missing nTimeout parameter in tp_send() * harbour/source/rtl/hbcomhb.c ! fixed typo in hb_comSend() ! fixed hb_storni() paramter order bugs --- harbour/ChangeLog | 20 +++++++++ harbour/contrib/hbtpathy/telepath.prg | 2 +- harbour/examples/commouse/commouse.prg | 4 +- harbour/examples/httpsrv/uhttpd.prg | 41 ++++++------------ harbour/examples/uhttpd2/umain.prg | 59 +++++++++++++------------- harbour/include/hbsocket.ch | 2 +- harbour/src/rtl/hbcomhb.c | 10 ++--- 7 files changed, 73 insertions(+), 65 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index b78520a08c..cbeec0a175 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,26 @@ The license applies to all entries newer than 2009-04-28. */ +2010-06-10 19:12 UTC+0300 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) + * harbour/examples/httpsrv/uhttpd.prg + * harbour/examples/uhttpd2/umain.prg + * optimized hb_socketRecv() calls + * optimized (removed) hb_socketSelect() calls, current API + has timeout parameter and make things simple + + * harbour/examples/commouse/commouse.prg + ! fixed error reporting (missing hb_comGetError()) parameter + + * harbour/include/hbsocket.ch + * adjusted comment text + + * harbour/contrib/hbtpathy/telepath.prg + ! fixed missing nTimeout parameter in tp_send() + + * harbour/source/rtl/hbcomhb.c + ! fixed typo in hb_comSend() + ! fixed hb_storni() paramter order bugs + 2010-06-10 18:00 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/include/hbclass.ch ! added missing TIMESTAMP class to ASSOCIATE CLASS command, diff --git a/harbour/contrib/hbtpathy/telepath.prg b/harbour/contrib/hbtpathy/telepath.prg index 09a4b127b6..376130aa1e 100644 --- a/harbour/contrib/hbtpathy/telepath.prg +++ b/harbour/contrib/hbtpathy/telepath.prg @@ -288,7 +288,7 @@ FUNCTION tp_send( nPort, cString, nTimeout ) RETURN 0 ENDIF - RETURN hb_comSend( t_aPorts[ nPort, TPFP_HANDLE ], cString ) + RETURN hb_comSend( t_aPorts[ nPort, TPFP_HANDLE ], cString,, nTimeout ) FUNCTION tp_sendsub( nPort, cString, nStart, nLength, nTimeout ) diff --git a/harbour/examples/commouse/commouse.prg b/harbour/examples/commouse/commouse.prg index 83ba95297a..d1d829a37d 100644 --- a/harbour/examples/commouse/commouse.prg +++ b/harbour/examples/commouse/commouse.prg @@ -22,12 +22,12 @@ LOCAL nPort, cBuf, cBuffer, nLen, nType, lL, lM, lR, nX, nY ENDIF IF ! hb_comOpen( nPort ) - ? "Unable to open port. Error:", hb_comGetError(), " OS error:", hb_comGetOsError() + ? "Unable to open port. Error:", hb_comGetError( nPort ), " OS error:", hb_comGetOsError( nPort ) RETURN ENDIF IF ! hb_comInit( nPort, 1200, "N", 8, 1 ) - ? "Unable to initialize port. Error:", hb_comGetError(), " OS error:", hb_comGetOsError() + ? "Unable to initialize port. Error:", hb_comGetError( nPort ), " OS error:", hb_comGetOsError( nPort ) hb_comClose( nPort ) RETURN ENDIF diff --git a/harbour/examples/httpsrv/uhttpd.prg b/harbour/examples/httpsrv/uhttpd.prg index db1b138498..af8feaf002 100644 --- a/harbour/examples/httpsrv/uhttpd.prg +++ b/harbour/examples/httpsrv/uhttpd.prg @@ -599,32 +599,19 @@ FUNCTION MAIN( ... ) ENDIF // Wait a connection - IF hb_socketSelect( { hListen },,,,,, 50 ) > 0 - - // reset remote values - aRemote := NIL - - // Accept a remote connection - hSocket := hb_socketAccept( hListen, @aRemote ) - IF hSocket == NIL - - WriteToConsole( hb_StrFormat( "accept() error: %s", hb_socketGetError() ) ) - + IF EMPTY( hSocket := hb_socketAccept( hListen, @aRemote, 50 ) ) + IF hb_socketGerError() == HB_SOCKET_ERR_TIMEOUT + // Checking if I have to quit + IF HB_FileExists( FILE_STOP ) + FERASE( FILE_STOP ) + EXIT + ENDIF ELSE - - // Send accepted connection to AcceptConnections() thread - hb_mutexNotify( s_hmtxQueue, hSocket ) - + WriteToConsole( hb_StrFormat( "accept() error: %s", hb_socketGetError() ) ) ENDIF - ELSE - - // Checking if I have to quit - IF HB_FileExists( FILE_STOP ) - FERASE( FILE_STOP ) - EXIT - ENDIF - + // Send accepted connection to AcceptConnections() thread + hb_mutexNotify( s_hmtxQueue, hSocket ) ENDIF // Memory release @@ -1700,12 +1687,12 @@ STATIC FUNCTION readRequest( hSocket, /* @ */ cRequest ) /* receive query */ cRequest := "" DO WHILE .T. - cBuf := Space( 1 ) + cBuf := Space( 4096 ) nLen := hb_socketRecv( hSocket, @cBuf ) IF nLen <= 0 EXIT ENDIF - cRequest += cBuf + cRequest += LEFT( cBuf, nLen ) IF CR_LF + CR_LF $ cRequest EXIT ENDIF @@ -1722,12 +1709,12 @@ STATIC FUNCTION readRequest( hSocket, /* @ */ cRequest ) */ nPos -= Len( cRequest ) - At( CR_LF + CR_LF, cRequest ) - 3 WHILE nPos > 0 - cBuf := Space( 1 ) + cBuf := Space( nPos ) nLen := hb_socketRecv( hSocket, @cBuf, nPos ) IF nLen <= 0 EXIT ENDIF - cRequest += cBuf + cRequest += LEFT( cBuf, nPos ) nPos -= nLen ENDDO ENDIF diff --git a/harbour/examples/uhttpd2/umain.prg b/harbour/examples/uhttpd2/umain.prg index ce0af6a08b..053c57c37a 100644 --- a/harbour/examples/uhttpd2/umain.prg +++ b/harbour/examples/uhttpd2/umain.prg @@ -132,30 +132,29 @@ LOCAL nWaiters Self:aSession := {=>} DO WHILE .T. - IF (nI := hb_socketSelect({Self:hListen},,,,,, 1000)) > 0 - hSocket := hb_socketAccept(Self:hListen) - IF hSocket == NIL - Self:LogError("[error] Accept error " + LTRIM(STR(hb_socketGetError()))) - ELSE - hb_mutexQueueInfo( Self:hmtxQueue, @nWaiters ) - ? "New connection", hSocket - ? "Waiters:", nWaiters - IF nWaiters < 2 .AND. LEN(aThreads) < THREAD_COUNT_MAX - /* - We need two threads in worst case. If first thread becomes a sessioned - thread, the second one will continue to serve sessionless requests for - the same connection. We create two threads here to avoid free thread count - check (and aThreads variable sync) in ProcessRequest(). - */ - AADD(aThreads, hb_threadStart(@ProcessConnection(), Self)) - AADD(aThreads, hb_threadStart(@ProcessConnection(), Self)) + IF EMPTY( hSocket := hb_socketAccept(Self:hListen,, 1000) ) + IF hb_socketGetError() == HB_SOCKET_ERR_TIMEOUT + EVAL(Self:bIdle, Self) + IF Self:lStop; EXIT ENDIF - hb_mutexNotify(Self:hmtxQueue, {hSocket, ""}) + ELSE + Self:LogError("[error] Accept error " + LTRIM(STR(hb_socketGetError()))) ENDIF ELSE - EVAL(Self:bIdle, Self) - IF Self:lStop; EXIT + hb_mutexQueueInfo( Self:hmtxQueue, @nWaiters ) + ? "New connection", hSocket + ? "Waiters:", nWaiters + IF nWaiters < 2 .AND. LEN(aThreads) < THREAD_COUNT_MAX + /* + We need two threads in worst case. If first thread becomes a sessioned + thread, the second one will continue to serve sessionless requests for + the same connection. We create two threads here to avoid free thread count + check (and aThreads variable sync) in ProcessRequest(). + */ + AADD(aThreads, hb_threadStart(@ProcessConnection(), Self)) + AADD(aThreads, hb_threadStart(@ProcessConnection(), Self)) ENDIF + hb_mutexNotify(Self:hmtxQueue, {hSocket, ""}) ENDIF ENDDO hb_socketClose(Self:hListen) @@ -218,13 +217,14 @@ LOCAL hSocket, cRequest, cSend, aI, nLen, nI, nReqLen, cBuf cRequest := "" nLen := 1 DO WHILE AT(CR_LF + CR_LF, cRequest) == 0 .AND. nLen > 0 - IF (nI := hb_socketSelect({hSocket},,,,,, 10000)) > 0 /* Timeout */ - cBuf := Space( 1 ) - nLen := hb_socketRecv(hSocket, @cBuf) - cRequest += cBuf + cBuf := Space( 4096 ) + IF (nLen := hb_socketRecv(hSocket, @cBuf,,, 10000)) > 0 /* Timeout */ + cRequest += LEFT(cBuf, nLen) ELSE - nLen := 0 - ? "recv() timeout", hSocket + IF nLen == -1 .AND. hb_socketGetError() == HB_SOCKET_ERR_TIMEOUT + nLen := 0 + ? "recv() timeout", hSocket + ENDIF ENDIF ENDDO @@ -263,9 +263,10 @@ LOCAL hSocket, cRequest, cSend, aI, nLen, nI, nReqLen, cBuf /* receive query body */ DO WHILE LEN(cRequest) < nReqLen .AND. nLen > 0 - cBuf := Space( 1 ) - nLen := hb_socketRecv(hSocket, @cBuf) - cRequest += cBuf + cBuf := Space( 4096 ) + IF (nLen := hb_socketRecv(hSocket, @cBuf,,, 500)) > 0 + cRequest += LEFT( cBuf, nLen ) + ENDIF ENDDO IF nLen == -1 diff --git a/harbour/include/hbsocket.ch b/harbour/include/hbsocket.ch index b76f947f95..fcc274a9ef 100644 --- a/harbour/include/hbsocket.ch +++ b/harbour/include/hbsocket.ch @@ -204,7 +204,7 @@ #define HB_SOCKET_IFINFO_HWADDR 8 /* hardware address */ #define HB_SOCKET_IFINFO_LEN 8 -/* HB_SOCKETGETSOCKNAME(), HB_SOCKETGETPEERNAME() return value indexes */ +/* Socket address array indexes */ #define HB_SOCKET_ADINFO_FAMILY 1 #define HB_SOCKET_ADINFO_ADDRESS 2 /* HB_SOCKET_AF_INET, HB_SOCKET_AF_INET6 */ #define HB_SOCKET_ADINFO_PATH 2 /* HB_SOCKET_AF_LOCAL */ diff --git a/harbour/src/rtl/hbcomhb.c b/harbour/src/rtl/hbcomhb.c index f6f6540cca..50386dd8f9 100644 --- a/harbour/src/rtl/hbcomhb.c +++ b/harbour/src/rtl/hbcomhb.c @@ -108,7 +108,7 @@ HB_FUNC( HB_COMFLOWCONTROL ) { int iValue = 0; hb_retl( hb_comFlowControl( hb_parni( 1 ), &iValue, hb_parni( 3 ) ) == 0 ); - hb_storni( 2, iValue ); + hb_storni( iValue, 2 ); } HB_FUNC( HB_COMFLOWSET ) @@ -163,21 +163,21 @@ HB_FUNC( HB_COMLSR ) { int iValue = 0; hb_retl( hb_comLSR( hb_parni( 1 ), &iValue ) == 0 ); - hb_storni( 2, iValue ); + hb_storni( iValue, 2 ); } HB_FUNC( HB_COMMCR ) { int iValue = 0; hb_retl( hb_comMCR( hb_parni( 1 ), &iValue, hb_parni( 3 ), hb_parni( 4 ) ) == 0 ); - hb_storni( 2, iValue ); + hb_storni( iValue, 2 ); } HB_FUNC( HB_COMMSR ) { int iValue = 0; hb_retl( hb_comMSR( hb_parni( 1 ), &iValue ) == 0 ); - hb_storni( 2, iValue ); + hb_storni( iValue, 2 ); } HB_FUNC( HB_COMOPEN ) @@ -237,5 +237,5 @@ HB_FUNC( HB_COMSEND ) if( lParam >= 0 && lParam < lLen ) lLen = lParam; } - hb_retnl( hb_comSend( hb_parni( 1 ), hb_parc( 2 ), lLen, hb_parnint( 5 ) ) ); + hb_retnl( hb_comSend( hb_parni( 1 ), hb_parc( 2 ), lLen, hb_parnint( 4 ) ) ); }