From 1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 29 Jan 2018 00:47:50 -0800 Subject: Changes related to debugging an issue. It may not have had as much to do with low-level protocol details, and a lot of this can be reverted, but I can't revert it right now. I'll look it over later in the week. --- src/stable/client.cpp | 32 ++++++++++++++++++++++++++++++++ src/stable/client.h | 2 ++ src/stable/multiserver.cpp | 6 +++++- src/stable/string.cpp | 10 +++++----- 4 files changed, 44 insertions(+), 6 deletions(-) (limited to 'src/stable') diff --git a/src/stable/client.cpp b/src/stable/client.cpp index ca86f37..43ae83b 100644 --- a/src/stable/client.cpp +++ b/src/stable/client.cpp @@ -25,11 +25,13 @@ Bu::Client::Client( Bu::TcpSocket *pSocket, bWantsDisconnect( false ), pfLink( pfLink ) { + Bu::ReadWriteMutex::WriteLocker lGlobal( mGlobal ); lFilts.prepend( pSocket ); } Bu::Client::~Client() { + Bu::ReadWriteMutex::WriteLocker lGlobal( mGlobal ); for( FilterList::iterator i = lFilts.begin(); i; i++ ) { delete *i; @@ -40,6 +42,7 @@ Bu::Client::~Client() void Bu::Client::processInput() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); mRead.lock(); char buf[RBS]; Bu::size nRead, nTotal=0; @@ -85,6 +88,7 @@ void Bu::Client::processInput() void Bu::Client::processOutput() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); mWrite.lock(); char buf[RBS]; if( qbWrite.getSize() > 0 ) @@ -100,17 +104,20 @@ void Bu::Client::processOutput() void Bu::Client::setProtocol( Protocol *pProto ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); this->pProto = pProto; this->pProto->onNewConnection( this ); } Bu::Protocol *Bu::Client::getProtocol() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); return pProto; } void Bu::Client::clearProtocol() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); pProto = NULL; } /* @@ -127,127 +134,149 @@ Bu::String &Bu::Client::getOutput() bool Bu::Client::isOpen() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); if( !pTopStream ) return false; return pTopStream->isOpen(); } Bu::size Bu::Client::write( const Bu::String &sData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( sData.getStr(), sData.getSize() ); } Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( pData, nBytes ); } Bu::size Bu::Client::write( int8_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int16_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int32_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int64_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint8_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint16_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint32_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint64_t nData ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbRead.read( pData, nBytes ); } Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbRead.peek( pData, nBytes, nOffset ); } Bu::size Bu::Client::getInputSize() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbRead.getSize(); } Bu::size Bu::Client::getOutputSize() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mWrite ); return qbWrite.getSize(); } const Bu::TcpSocket *Bu::Client::getSocket() const { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); return pSocket; } void Bu::Client::disconnect() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); bWantsDisconnect = true; } bool Bu::Client::wantsDisconnect() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); return bWantsDisconnect; } void Bu::Client::close() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); pTopStream->close(); } Bu::ClientLink *Bu::Client::getLink() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); return pfLink->createLink( this ); } void Bu::Client::onMessage( const Bu::String &sMsg ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); if( pProto ) pProto->onMessage( this, sMsg ); } void Bu::Client::tick() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); if( pProto ) pProto->onTick( this ); } @@ -259,6 +288,7 @@ Bu::size Bu::Client::tell() void Bu::Client::seek( Bu::size offset ) { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mRead ); return qbRead.seek( offset ); } @@ -280,11 +310,13 @@ bool Bu::Client::isEos() void Bu::Client::flush() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); processOutput(); } bool Bu::Client::canRead() { + Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); Bu::MutexLocker l( mRead ); return qbRead.getSize() > 0; } diff --git a/src/stable/client.h b/src/stable/client.h index ac882eb..6492a6e 100644 --- a/src/stable/client.h +++ b/src/stable/client.h @@ -14,6 +14,7 @@ #include "bu/string.h" #include "bu/queuebuf.h" #include "bu/mutex.h" +#include "bu/readwritemutex.h" namespace Bu { @@ -130,6 +131,7 @@ namespace Bu class Bu::ClientLinkFactory *pfLink; Bu::Mutex mRead; Bu::Mutex mWrite; + mutable Bu::ReadWriteMutex mGlobal; }; } diff --git a/src/stable/multiserver.cpp b/src/stable/multiserver.cpp index 6fd7ff3..c9e86cf 100644 --- a/src/stable/multiserver.cpp +++ b/src/stable/multiserver.cpp @@ -11,6 +11,8 @@ #include "bu/config.h" +#include "bu/sio.h" + Bu::MultiServer::MultiServer() { } @@ -40,7 +42,9 @@ void Bu::MultiServer::onNewConnection( Bu::Client *pClient, int nPort ) void Bu::MultiServer::onClosedConnection( Bu::Client *pClient ) { - delete pClient->getProtocol(); + Bu::Protocol *pProto = pClient->getProtocol(); + pClient->clearProtocol(); + delete pProto; } void Bu::MultiServer::shutdown() diff --git a/src/stable/string.cpp b/src/stable/string.cpp index 1579826..ce679fe 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -201,7 +201,7 @@ void Bu::String::append( const char *pData, long nStart, long nLen ) _hardCopy(); - if( core->pLast && core->pLast->nLength < nMinSize ) + if( core->pLast && core->pLast->nLength+1 < nMinSize ) { int nAmnt = nMinSize - core->pLast->nLength; if( nAmnt > nLen ) @@ -228,9 +228,9 @@ void Bu::String::append( const char *pData, long nStart, long nLen ) void Bu::String::append( const char &cData ) { - if( core->pLast && core->pLast->nLength < nMinSize ) + _hardCopy(); + if( core->pLast && core->pLast->nLength+1 < nMinSize ) { - _hardCopy(); core->pLast->pData[core->pLast->nLength] = cData; ++core->pLast->nLength; ++core->nLength; // pLast->pData[pLast->nLength] = (char)0; @@ -615,9 +615,9 @@ Bu::String &Bu::String::operator+=( const Bu::String::const_iterator &i ) Bu::String &Bu::String::operator+=( const char cData ) { - if( core->pLast && core->pLast->nLength < nMinSize ) - { _hardCopy(); + if( core->pLast && core->pLast->nLength+1 < nMinSize ) + { core->pLast->pData[core->pLast->nLength] = cData; ++core->pLast->nLength; ++core->nLength; // pLast->pData[pLast->nLength] = (char)0; -- cgit v1.2.3