From 96256790018cab246f59d708eaeae19dd8a93bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Czerpak?= Date: Thu, 3 Dec 2015 22:08:08 +0100 Subject: [PATCH] 2015-12-03 22:08 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * contrib/hbnetio/netiosrv.c ! fixed wrongly set error code when last stream is closed. It was minor problem invisible for client PRG code without any side effects. * lock stream mutex a little bit earlier to avoid problems with future code extensions - i.e. streams closed asynchronously by other server threads * contrib/hbnetio/netiocli.c * allow to send data to currently registered stream. Now if server stream function sends some data to stream before it returns then this data is not dropped be client but saved and later can be accessed by netio_GetData() function --- ChangeLog.txt | 15 +++++++++++++ contrib/hbnetio/netiocli.c | 44 ++++++++++++++++++-------------------- contrib/hbnetio/netiosrv.c | 34 +++++++++++++++++------------ 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 8d76b7d76a..dd975c1c89 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,21 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2015-12-03 22:08 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + * contrib/hbnetio/netiosrv.c + ! fixed wrongly set error code when last stream is closed. + It was minor problem invisible for client PRG code without any + side effects. + * lock stream mutex a little bit earlier to avoid problems with + future code extensions - i.e. streams closed asynchronously by + other server threads + + * contrib/hbnetio/netiocli.c + * allow to send data to currently registered stream. + Now if server stream function sends some data to stream before it + returns then this data is not dropped be client but saved and later + can be accessed by netio_GetData() function + 2015-12-03 12:26 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * include/hbstack.h * src/vm/estack.c diff --git a/contrib/hbnetio/netiocli.c b/contrib/hbnetio/netiocli.c index 1dc7d62bd7..e4b556f578 100644 --- a/contrib/hbnetio/netiocli.c +++ b/contrib/hbnetio/netiocli.c @@ -250,6 +250,23 @@ static PHB_SRVDATA s_fileFindSrvData( PHB_CONCLI conn, int iStreamID, int iType return NULL; } +static int s_fileNewSrvData( PHB_CONCLI conn, int iType ) +{ + PHB_SRVDATA pSrvData = ( PHB_SRVDATA ) hb_xgrabz( sizeof( HB_SRVDATA ) ); + + pSrvData->id = s_fileGenSrvDataID( conn ); + pSrvData->type = iType; + if( iType == NETIO_SRVITEM ) + pSrvData->maxsize = 4096; + else if( iType == NETIO_SRVDATA ) + pSrvData->maxsize = 0x10000; + pSrvData->next = conn->srvdata; + if( ! conn->srvdata ) + hb_atomic_inc( &conn->used ); + conn->srvdata = pSrvData; + return pSrvData->id; +} + static HB_BOOL s_fileCloseSrvData( PHB_CONCLI conn, int iStreamID ) { PHB_SRVDATA * pSrvDataPtr = &conn->srvdata; @@ -275,27 +292,6 @@ static HB_BOOL s_fileCloseSrvData( PHB_CONCLI conn, int iStreamID ) return HB_FALSE; } -static void s_fileNewSrvData( PHB_CONCLI conn, int iStreamID, int iType ) -{ - PHB_SRVDATA pSrvData = s_fileFindSrvData( conn, iStreamID, 0 ); - - if( ! pSrvData ) - { - pSrvData = ( PHB_SRVDATA ) memset( hb_xgrab( sizeof( HB_SRVDATA ) ), - 0, sizeof( HB_SRVDATA ) ); - pSrvData->id = iStreamID; - pSrvData->type = iType; - if( iType == NETIO_SRVITEM ) - pSrvData->maxsize = 4096; - else if( iType == NETIO_SRVDATA ) - pSrvData->maxsize = 0x10000; - pSrvData->next = conn->srvdata; - if( ! conn->srvdata ) - hb_atomic_inc( &conn->used ); - conn->srvdata = pSrvData; - } -} - static HB_BOOL s_fileRecvSrvData( PHB_CONCLI conn, long len, int iStreamID, int iType ) { char * buffer = ( char * ) hb_xgrab( len ); @@ -1208,7 +1204,7 @@ static HB_BOOL s_netio_procexec( int iMsg, int iType ) HB_PUT_LE_UINT32( &msgbuf[ 4 ], size ); if( iMsg == NETIO_FUNCCTRL ) { - iStreamID = s_fileGenSrvDataID( conn ); + iStreamID = s_fileNewSrvData( conn, iType ); HB_PUT_LE_UINT32( &msgbuf[ 8 ], iStreamID ); HB_PUT_LE_UINT32( &msgbuf[ 12 ], iType ); memset( msgbuf + 16, '\0', sizeof( msgbuf ) - 16 ); @@ -1243,7 +1239,7 @@ static HB_BOOL s_netio_procexec( int iMsg, int iType ) if( iMsg == NETIO_FUNCCTRL ) { if( iStreamID == hb_itemGetNI( pItem ) ) - s_fileNewSrvData( conn, iStreamID, iType ); + iStreamID = 0; else hb_itemPutNI( pItem, -1 ); } @@ -1262,6 +1258,8 @@ static HB_BOOL s_netio_procexec( int iMsg, int iType ) } } } + if( iStreamID != 0 ) + s_fileCloseSrvData( conn, iStreamID ); if( buffer ) hb_xfree( buffer ); s_fileConUnlock( conn ); diff --git a/contrib/hbnetio/netiosrv.c b/contrib/hbnetio/netiosrv.c index a8371f7726..3747621a33 100644 --- a/contrib/hbnetio/netiosrv.c +++ b/contrib/hbnetio/netiosrv.c @@ -1402,15 +1402,16 @@ HB_FUNC( NETIO_SERVER ) while( *pStreamPtr ) { if( ( *pStreamPtr )->id == iStreamID ) - { - PHB_CONSTREAM stream = *pStreamPtr; - *pStreamPtr = stream->next; - hb_xfree( stream ); break; - } pStreamPtr = &( *pStreamPtr )->next; } - if( *pStreamPtr == NULL ) + if( *pStreamPtr != NULL ) + { + PHB_CONSTREAM stream = *pStreamPtr; + *pStreamPtr = stream->next; + hb_xfree( stream ); + } + else iStreamID = 0; hb_threadMutexUnlock( conn->mutex ); } @@ -1508,17 +1509,22 @@ HB_FUNC( NETIO_SERVER ) iStreamID = 0; if( iStreamID ) { - PHB_CONSTREAM stream = ( PHB_CONSTREAM ) - hb_xgrab( sizeof( HB_CONSTREAM ) ); - stream->id = iStreamID; - stream->type = iStreamType; - stream->next = conn->streams; - conn->streams = stream; - if( conn->mutex == NULL ) conn->mutex = hb_threadMutexCreate(); - if( ! hb_threadMutexLock( conn->mutex ) ) + if( hb_threadMutexLock( conn->mutex ) ) + { + PHB_CONSTREAM stream = ( PHB_CONSTREAM ) + hb_xgrab( sizeof( HB_CONSTREAM ) ); + stream->id = iStreamID; + stream->type = iStreamType; + stream->next = conn->streams; + conn->streams = stream; + } + else + { errCode = NETIO_ERR_REFUSED; + iStreamID = 0; + } } else errCode = NETIO_ERR_WRONG_PARAM;