From 02651dc13aecf7f2f4acd8fb71d9f8b1db56d441 Mon Sep 17 00:00:00 2001 From: Mindaugas Kavaliauskas Date: Wed, 9 Jun 2010 12:42:08 +0000 Subject: [PATCH] 2010-06-09 15:37 UTC+0300 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) * harbour/src/rtl/hbsockhb.c * changed return values of some functions: nSuccess to lSuccess * HB_SOCKET_PF_* changed to HB_SOCKET_AF_* - hb_parnintdef() (moved to extend.c) ! some bug fixes ! some typo in documentation * harbour/examples/udpds/udpds.prg * sychronized with hb_socket*() changes * changed server thread exit condition (proposed by Przemek) * harbour/include/hbapi.h * harbour/src/vm/extend.c + hb_parnintdef() --- harbour/ChangeLog | 17 ++++++ harbour/examples/udpds/udpds.prg | 10 ++-- harbour/include/hbapi.h | 1 + harbour/src/rtl/hbsockhb.c | 88 ++++++++++++++++---------------- harbour/src/vm/extend.c | 28 ++++++++++ 5 files changed, 95 insertions(+), 49 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 3621e28b19..6a4b7fc943 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,11 +17,28 @@ */ +2010-06-09 15:37 UTC+0300 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) + * harbour/src/rtl/hbsockhb.c + * changed return values of some functions: nSuccess to lSuccess + * HB_SOCKET_PF_* changed to HB_SOCKET_AF_* + - hb_parnintdef() (moved to extend.c) + ! some bug fixes + ! some typo in documentation + + * harbour/examples/udpds/udpds.prg + * sychronized with hb_socket*() changes + * changed server thread exit condition (proposed by Przemek) + + * harbour/include/hbapi.h + * harbour/src/vm/extend.c + + hb_parnintdef() + 2010-06-09 12:55 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/src/rtl/hbsocket.c * harbour/include/hbsocket.ch ! cleaned a little bit HB_SOCKET_AF_*/HB_SOCKET_PF_* usage to not confuse other developers and users. + 2010-06-08 23:19 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * include/hbsetup.h * Renamed HB_OS_IPHONE -> HB_OS_IOS diff --git a/harbour/examples/udpds/udpds.prg b/harbour/examples/udpds/udpds.prg index ea7322a956..62f28fcf91 100644 --- a/harbour/examples/udpds/udpds.prg +++ b/harbour/examples/udpds/udpds.prg @@ -89,7 +89,7 @@ RETURN aRet FUNC UDPDS_Start( nPort, cName, cVersion ) LOCAL hSocket IF ! EMPTY( hSocket := hb_socketOpen( , HB_SOCKET_PT_DGRAM ) ) - IF hb_socketBind( hSocket, { HB_SOCKET_AF_INET, "0.0.0.0", nPort } ) == 0 + IF hb_socketBind( hSocket, { HB_SOCKET_AF_INET, "0.0.0.0", nPort } ) hb_threadDetach( hb_threadStart( @UDPDS(), hSocket, cName, cVersion ) ) RETURN hSocket ENDIF @@ -107,9 +107,11 @@ STATIC PROC UDPDS( hSocket, cName, cVersion ) LOCAL cBuffer, nLen, aAddr DO WHILE .T. cBuffer := SPACE( 2000 ) - nLen := hb_socketRecvFrom( hSocket, @cBuffer,,, @aAddr ) - IF nLen == -1 - RETURN + nLen := hb_socketRecvFrom( hSocket, @cBuffer,,, @aAddr, 1000 ) + IF nLen == -1 + IF hb_socketGetError() != HB_SOCKET_ERR_TIMEOUT + RETURN + ENDIF ELSE /* * Communication protocol: diff --git a/harbour/include/hbapi.h b/harbour/include/hbapi.h index 314eb25e84..6cbf5cf429 100644 --- a/harbour/include/hbapi.h +++ b/harbour/include/hbapi.h @@ -640,6 +640,7 @@ extern HB_EXPORT int hb_parnidef( int iParam, int iDefValue ); /* retri extern HB_EXPORT long hb_parnl( int iParam ); /* retrieve a numeric parameter as a long */ extern HB_EXPORT long hb_parnldef( int iParam, long lDefValue ); /* retrieve a numeric parameter as a long, return default value if parameter isn't numeric */ extern HB_EXPORT HB_MAXINT hb_parnint( int iParam ); /* retrieve a numeric parameter as a HB_MAXINT */ +extern HB_EXPORT HB_MAXINT hb_parnintdef( int iParam, HB_MAXINT lDefValue ); /* retrieve a numeric parameter as a HB_MAXINT, return default value if parameter isn't numeric */ extern HB_EXPORT void * hb_parptr( int iParam ); /* retrieve a parameter as a pointer */ extern HB_EXPORT void * hb_parptrGC( const HB_GC_FUNCS * pFuncs, int iParam ); /* retrieve a parameter as a pointer if it's a pointer to GC allocated block */ #ifndef HB_LONG_LONG_OFF diff --git a/harbour/src/rtl/hbsockhb.c b/harbour/src/rtl/hbsockhb.c index 016ce66182..50b96abaa1 100644 --- a/harbour/src/rtl/hbsockhb.c +++ b/harbour/src/rtl/hbsockhb.c @@ -56,27 +56,27 @@ * HB_SOCKETERRORSTRING( [ nSocketErrror = hb_socketGetError() ] ) --> cError * HB_SOCKETGETSOCKNAME( hSocket ) --> aAddr | NIL * HB_SOCKETGETPEERNAME( hSocket ) --> aAddr | NIL - * HB_SOCKETOPEN( [ nDomain = HB_SOCKET_PF_INET ] , [ nType = HB_SOCKET_PT_STREAM ], [ nProtocol = 0 ] ) --> hSocket - * HB_SOCKETCLOSE( hSocket ) --> nSuccess - * HB_SOCKETSHUTDOWN( hSocket, [ nMode = HB_SOCKET_SHUT_RDWR ] ) --> nSuccess - * HB_SOCKETBIND( hSocket, aAddr ) --> nSuccess - * HB_SOCKETLISTEN( hSocket, [ iQueueLen = 10 ] ) --> nSuccess - * HB_SOCKETACCEPT( hSocket, [ @aAddr ], [ nTimeout = FOREVER ] ) --> nSuccess - * HB_SOCKETCONNECT( hSocket, aAddr, [ nTimeout = FOREVER ] ) --> nSuccess + * HB_SOCKETOPEN( [ nDomain = HB_SOCKET_AF_INET ] , [ nType = HB_SOCKET_PT_STREAM ], [ nProtocol = 0 ] ) --> hSocket + * HB_SOCKETCLOSE( hSocket ) --> lSuccess + * HB_SOCKETSHUTDOWN( hSocket, [ nMode = HB_SOCKET_SHUT_RDWR ] ) --> lSuccess + * HB_SOCKETBIND( hSocket, aAddr ) --> lSuccess + * HB_SOCKETLISTEN( hSocket, [ iQueueLen = 10 ] ) --> lSuccess + * HB_SOCKETACCEPT( hSocket, [ @aAddr ], [ nTimeout = FOREVER ] ) --> hConnectionSocket + * HB_SOCKETCONNECT( hSocket, aAddr, [ nTimeout = FOREVER ] ) --> lSuccess * HB_SOCKETSEND( hSocket, cBuffer, [ nLen = LEN( cBuffer ) ], [ nFlags = 0 ], [ nTimeout = FOREVER ] ) --> nBytesSent * HB_SOCKETSENDTO( hSocket, cBuffer, [ nLen = LEN( cBuffer ) ], [ nFlags = 0 ], aAddr, [ nTimeout = FOREVER ] ) --> nBytesSent * HB_SOCKETRECV( hSocket, @cBuffer, [ nLen = LEN( cBuffer ) ], [ nFlags = 0 ], [ nTimeout = FOREVER ] ) --> nBytesRecv * HB_SOCKETRECVFROM( hSocket, @cBuffer, [ nLen = LEN( cBuffer ) ], [ nFlags = 0 ], @aAddr, [ nTimeout = FOREVER ] ) --> nBytesRecv - * HB_SOCKETSETBLOCKINGIO( hSocket, lValue ) --> nSuccess - * HB_SOCKETSETNODELAY( hSocket, lValue ) --> nSuccess - * HB_SOCKETSETREUSEADDR( hSocket, lValue ) --> nSuccess - * HB_SOCKETSETKEEPALIVE( hSocket, lValue ) --> nSuccess - * HB_SOCKETSETBROADCAST( hSocket, lValue ) --> nSuccess - * HB_SOCKETSETSNDBUFSIZE( hSocket, nValue ) --> nSuccess - * HB_SOCKETSETRCVBUFSIZE( hSocket, nValue ) --> nSuccess - * HB_SOCKETGETSNDBUFSIZE( hSocket, @nValue ) --> nSuccess - * HB_SOCKETGETRCVBUFSIZE( hSocket, @nValue ) --> nSuccess - * HB_SOCKETSETMULTICAST( hSocket, cAddr ) --> nSuccess + * HB_SOCKETSETBLOCKINGIO( hSocket, lValue ) --> lSuccess + * HB_SOCKETSETNODELAY( hSocket, lValue ) --> lSuccess + * HB_SOCKETSETREUSEADDR( hSocket, lValue ) --> lSuccess + * HB_SOCKETSETKEEPALIVE( hSocket, lValue ) --> lSuccess + * HB_SOCKETSETBROADCAST( hSocket, lValue ) --> lSuccess + * HB_SOCKETSETSNDBUFSIZE( hSocket, nValue ) --> lSuccess + * HB_SOCKETSETRCVBUFSIZE( hSocket, nValue ) --> lSuccess + * HB_SOCKETGETSNDBUFSIZE( hSocket, @nValue ) --> lSuccess + * HB_SOCKETGETRCVBUFSIZE( hSocket, @nValue ) --> lSuccess + * HB_SOCKETSETMULTICAST( hSocket, [ nFamily = HB_SOCKET_AF_INET ], cAddr ) --> lSuccess * HB_SOCKETSELECTREAD( hSocket, [ nTimeout = FOREVER ] ) --> nRet * HB_SOCKETSELECTWRITE( hSocket, [ nTimeout = FOREVER ] ) --> nRet * HB_SOCKETSELECTWRITEEX( hSocket, [ nTimeout = FOREVER ] ) --> nRet @@ -92,11 +92,6 @@ #include "hbvm.h" #include "hbsocket.h" -HB_MAXINT hb_parnintdef( int iParam, HB_MAXINT iDefault ) -{ - return HB_ISNUM( iParam ) ? hb_parnint( iParam ) : iDefault; -} - typedef struct { HB_SOCKET socket; @@ -237,7 +232,7 @@ HB_FUNC( HB_SOCKETGETPEERNAME ) HB_FUNC( HB_SOCKETOPEN ) { HB_SOCKET socket; - int iDomain = hb_parnidef( 1, HB_SOCKET_PF_INET ); + int iDomain = hb_parnidef( 1, HB_SOCKET_AF_INET ); int iType = hb_parnidef( 2, HB_SOCKET_PT_STREAM ); int iProtocol = hb_parni( 3 ); @@ -260,7 +255,7 @@ HB_FUNC( HB_SOCKETCLOSE ) { int iRet = hb_socketClose( pSocket->socket ); pSocket->socket = HB_NO_SOCKET; - hb_retni( iRet ); + hb_retl( iRet == 0 ); } } @@ -269,7 +264,7 @@ HB_FUNC( HB_SOCKETSHUTDOWN ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketShutdown( pSocket->socket, hb_parnidef( 2, HB_SOCKET_SHUT_RDWR ) ) ); + hb_retl( hb_socketShutdown( pSocket->socket, hb_parnidef( 2, HB_SOCKET_SHUT_RDWR ) ) == 0 ); } } @@ -281,7 +276,7 @@ HB_FUNC( HB_SOCKETBIND ) if( pSocket && socketaddrParam( 2, &addr, &len ) ) { - hb_retni( hb_socketBind( pSocket->socket, addr, len ) ); + hb_retl( hb_socketBind( pSocket->socket, addr, len ) == 0 ); hb_xfree( addr ); } } @@ -291,7 +286,7 @@ HB_FUNC( HB_SOCKETLISTEN ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketListen( pSocket->socket, hb_parnidef( 2, 10 ) ) ); + hb_retl( hb_socketListen( pSocket->socket, hb_parnidef( 2, 10 ) ) == 0 ); } } @@ -319,8 +314,8 @@ HB_FUNC( HB_SOCKETACCEPT ) if( HB_ISBYREF( 2 ) ) { - PHB_ITEM pItem = hb_socketAddrToItem( addr, len ); - if( pItem ) + PHB_ITEM pItem; + if( socket != HB_NO_SOCKET && ( pItem = hb_socketAddrToItem( addr, len ) ) != NULL ) { hb_itemParamStoreForward( 2, pItem ); hb_itemRelease( pItem ); @@ -342,7 +337,7 @@ HB_FUNC( HB_SOCKETCONNECT ) if( pSocket && socketaddrParam( 2, &addr, &len ) ) { - hb_retni( hb_socketConnect( pSocket->socket, addr, len, hb_parnintdef( 3, -1 ) ) ); + hb_retl( hb_socketConnect( pSocket->socket, addr, len, hb_parnintdef( 3, -1 ) ) == 0 ); hb_xfree( addr ); } } @@ -427,19 +422,22 @@ HB_FUNC( HB_SOCKETRECVFROM ) { void * addr = NULL; unsigned int len; + long lRet; + if( HB_ISNUM( 3 ) ) { long lRead = hb_parnl( 3 ); if( lRead >= 0 && lRead < ( long ) iLen ) iLen = lRead; } - hb_retnl( hb_socketRecvFrom( pSocket->socket, pBuffer, ( long ) iLen, - hb_parni( 4 ), &addr, &len, - hb_parnintdef( 6, -1 ) ) ); + hb_retnl( lRet = hb_socketRecvFrom( pSocket->socket, pBuffer, ( long ) iLen, + hb_parni( 4 ), &addr, &len, + hb_parnintdef( 6, -1 ) ) ); if( HB_ISBYREF( 5 ) ) { - PHB_ITEM pAddr = hb_socketAddrToItem( addr, len ); - if( pAddr ) + PHB_ITEM pAddr; + + if( lRet != -1 && ( pAddr = hb_socketAddrToItem( addr, len ) ) != NULL ) { hb_itemParamStoreForward( 5, pAddr ); hb_itemRelease( pAddr ); @@ -461,7 +459,7 @@ HB_FUNC( HB_SOCKETSETBLOCKINGIO ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetBlockingIO( pSocket->socket, hb_parl( 2 ) ) ); + hb_retl( hb_socketSetBlockingIO( pSocket->socket, hb_parl( 2 ) ) == 0 ); } } @@ -470,7 +468,7 @@ HB_FUNC( HB_SOCKETSETNODELAY ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetNoDelay( pSocket->socket, hb_parl( 2 ) ) ); + hb_retl( hb_socketSetNoDelay( pSocket->socket, hb_parl( 2 ) ) == 0 ); } } @@ -479,7 +477,7 @@ HB_FUNC( HB_SOCKETSETREUSEADDR ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetReuseAddr( pSocket->socket, hb_parl( 2 ) ) ); + hb_retl( hb_socketSetReuseAddr( pSocket->socket, hb_parl( 2 ) ) == 0 ); } } @@ -488,7 +486,7 @@ HB_FUNC( HB_SOCKETSETKEEPALIVE ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetKeepAlive( pSocket->socket, hb_parl( 2 ) ) ); + hb_retl( hb_socketSetKeepAlive( pSocket->socket, hb_parl( 2 ) ) == 0 ); } } @@ -497,7 +495,7 @@ HB_FUNC( HB_SOCKETSETBROADCAST ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetBroadcast( pSocket->socket, hb_parl( 2 ) ) ); + hb_retl( hb_socketSetBroadcast( pSocket->socket, hb_parl( 2 ) ) == 0 ); } } @@ -506,7 +504,7 @@ HB_FUNC( HB_SOCKETSETSNDBUFSIZE ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetSndBufSize( pSocket->socket, hb_parni( 2 ) ) ); + hb_retl( hb_socketSetSndBufSize( pSocket->socket, hb_parni( 2 ) ) == 0 ); } } @@ -515,7 +513,7 @@ HB_FUNC( HB_SOCKETSETRCVBUFSIZE ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetRcvBufSize( pSocket->socket, hb_parni( 2 ) ) ); + hb_retl( hb_socketSetRcvBufSize( pSocket->socket, hb_parni( 2 ) ) == 0 ); } } @@ -525,7 +523,7 @@ HB_FUNC( HB_SOCKETGETSNDBUFSIZE ) if( pSocket ) { int size; - hb_retni( hb_socketGetSndBufSize( pSocket->socket, &size ) ); + hb_retl( hb_socketGetSndBufSize( pSocket->socket, &size ) == 0 ); hb_storni( size, 2 ); } } @@ -536,7 +534,7 @@ HB_FUNC( HB_SOCKETGETRCVBUFSIZE ) if( pSocket ) { int size; - hb_retni( hb_socketGetRcvBufSize( pSocket->socket, &size ) ); + hb_retl( hb_socketGetRcvBufSize( pSocket->socket, &size ) == 0 ); hb_storni( size, 2 ); } } @@ -546,7 +544,7 @@ HB_FUNC( HB_SOCKETSETMULTICAST ) PHB_PRG_SOCKET pSocket = socketParam( 1 ); if( pSocket ) { - hb_retni( hb_socketSetMulticast( pSocket->socket, hb_parnidef( 2, HB_SOCKET_AF_INET ), hb_parc( 3 ) ) ); + hb_retl( hb_socketSetMulticast( pSocket->socket, hb_parnidef( 2, HB_SOCKET_AF_INET ), hb_parc( 3 ) ) == 0 ); } } diff --git a/harbour/src/vm/extend.c b/harbour/src/vm/extend.c index fd0ee9eecf..5e99e0afd1 100644 --- a/harbour/src/vm/extend.c +++ b/harbour/src/vm/extend.c @@ -645,6 +645,34 @@ HB_MAXINT hb_parnint( int iParam ) return 0; } +HB_MAXINT hb_parnintdef( int iParam, HB_MAXINT lDefValue ) +{ + HB_STACK_TLS_PRELOAD + + HB_TRACE(HB_TR_DEBUG, ("hb_parnint(%d)", iParam)); + + if( iParam >= -1 && iParam <= hb_pcount() ) + { + PHB_ITEM pItem = ( iParam == -1 ) ? hb_stackReturnItem() : hb_stackItemFromBase( iParam ); + + if( HB_IS_BYREF( pItem ) ) + pItem = hb_itemUnRef( pItem ); + + if( HB_IS_LONG( pItem ) ) + return ( HB_MAXINT ) pItem->item.asLong.value; + else if( HB_IS_INTEGER( pItem ) ) + return ( HB_MAXINT ) pItem->item.asInteger.value; + else if( HB_IS_DOUBLE( pItem ) ) +#if defined( __GNUC__ ) + return ( HB_MAXINT ) ( HB_MAXUINT ) pItem->item.asDouble.value; +#else + return ( HB_MAXINT ) pItem->item.asDouble.value; +#endif + } + + return lDefValue; +} + void * hb_parptr( int iParam ) { HB_STACK_TLS_PRELOAD