From 87d76df1720e29e4195cf4a8845c07450fb3b2f2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 1 Aug 2023 11:38:26 -0700 Subject: Fixed issues around disconnected sockets. We were bailing out when sockets closed, that was silly. --- src/stable/server.cpp | 97 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 30 deletions(-) (limited to 'src/stable/server.cpp') diff --git a/src/stable/server.cpp b/src/stable/server.cpp index 3aa8a34..6ee7d6e 100644 --- a/src/stable/server.cpp +++ b/src/stable/server.cpp @@ -247,6 +247,8 @@ void Bu::Server::addClient( const Bu::ServerSocket *pSrv, Bu::Socket *pSocket ) Bu::Client *Bu::Server::getClient( fd iId ) { Bu::MutexLocker l( mClients ); + if( !hClients.has( iId ) ) + return NULL; return hClients.get( iId ); } @@ -520,30 +522,38 @@ void Bu::Server::IoWorker::handleRead( Client *pClient, Socket *pSocket ) Bu::size iTotal=0; BU_PROFILE_START("client.read"); - for(;;) + try { - try + for(;;) { - iRead = pSocket->read( buf, RBS ); - - if( iRead == 0 ) + try { - break; + iRead = pSocket->read( buf, RBS ); + + if( iRead == 0 ) + { + break; + } + else + { + iTotal += iRead; + pClient->cbBuffer.server().write( buf, iRead ); + if( !pSocket->canRead() ) + break; + } } - else + catch( Bu::ExceptionBase &e ) { - iTotal += iRead; - pClient->cbBuffer.server().write( buf, iRead ); - if( !pSocket->canRead() ) - break; + pClient->disconnect(); + //close( pSocket ); + return; } } - catch( Bu::ExceptionBase &e ) - { - pClient->disconnect(); - //close( pSocket ); - return; - } + } + catch( std::exception &e ) + { + // Probably the socket is dead. We should maybe disconnect, but we'll + // also notice soon enough anyway? } BU_PROFILE_END("client.read"); @@ -565,12 +575,28 @@ void Bu::Server::IoWorker::handleRead( Client *pClient, Socket *pSocket ) void Bu::Server::IoWorker::handleWrite( Client *pClient, Socket *pSocket ) { char buf[RBS]; - while( pClient->hasOutput() > 0 ) + try + { + while( pClient->hasOutput() ) + { + int iAmnt = RBS; + iAmnt = pClient->cbBuffer.server().peek( buf, iAmnt ); + int iReal = pSocket->write( buf, iAmnt ); + pClient->cbBuffer.server().seek( iReal ); + if( iReal < iAmnt ) + { + // We wrote less than expected, the kernel buffer must be full, + // we should queue ourselves again. + rSrv.clientWriteReady( pClient->getId() ); + break; + } + } + } + catch( std::exception &e ) { - int iAmnt = RBS; - iAmnt = pClient->cbBuffer.server().peek( buf, iAmnt ); - int iReal = pSocket->write( buf, iAmnt ); - pClient->cbBuffer.server().seek( iReal ); + // Error working with socket, it's probably closed. + if( pClient->hasOutput() ) + rSrv.clientWriteReady( pClient->getId() ); } } @@ -596,17 +622,28 @@ void Bu::Server::ClientWorker::run() if( pEv == NULL ) continue; - Client *pClient = rSrv.getClient( pEv->getId() ); - if( pClient == NULL ) + try { - delete pEv; - continue; + Client *pClient = rSrv.getClient( pEv->getId() ); + if( pClient == NULL ) + { + delete pEv; + continue; + } + + pClient->processInput(); + if( pClient->getOutputSize() > 0 ) + { + rSrv.clientWriteReady( pClient->getId() ); + } } - - pClient->processInput(); - if( pClient->getOutputSize() > 0 ) + catch( std::exception &e ) + { + // Probably we're fine, the client just closed between queuing and + // working. + } + catch(...) { - rSrv.clientWriteReady( pClient->getId() ); } delete pEv; } -- cgit v1.2.3