From c5f69c22ca51510da1674bf56982f8f1e3ca4a40 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 14 Aug 2014 15:49:05 +0000 Subject: Neither the Bu::Client class or the Bu::ProtocolTelnet class were threadsafe and both really need to be. Bu::Client is used in multi-threaded applications and has been for a while, so it's a no brainer. It may need a little bit more safety added around variable access, but all the buffers are safe now. ProtocolTelnet has it's own issus, and I should probably rework a few parts of it before too long so we can better accomidate things like line editing. --- src/stable/client.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/stable/client.cpp') diff --git a/src/stable/client.cpp b/src/stable/client.cpp index a9deb00..ca86f37 100644 --- a/src/stable/client.cpp +++ b/src/stable/client.cpp @@ -12,6 +12,7 @@ #include "bu/protocol.h" #include "bu/clientlink.h" #include "bu/clientlinkfactory.h" +#include "bu/mutexlocker.h" /** Read buffer size. */ #define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess @@ -39,6 +40,7 @@ Bu::Client::~Client() void Bu::Client::processInput() { + mRead.lock(); char buf[RBS]; Bu::size nRead, nTotal=0; @@ -67,6 +69,7 @@ void Bu::Client::processInput() break; } } + mRead.unlock(); if( nTotal == 0 ) { @@ -82,6 +85,7 @@ void Bu::Client::processInput() void Bu::Client::processOutput() { + mWrite.lock(); char buf[RBS]; if( qbWrite.getSize() > 0 ) { @@ -91,6 +95,7 @@ void Bu::Client::processOutput() qbWrite.seek( nReal ); pTopStream->flush(); } + mWrite.unlock(); } void Bu::Client::setProtocol( Protocol *pProto ) @@ -128,71 +133,85 @@ bool Bu::Client::isOpen() Bu::size Bu::Client::write( const Bu::String &sData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( sData.getStr(), sData.getSize() ); } Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( pData, nBytes ); } Bu::size Bu::Client::write( int8_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int16_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int32_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( int64_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint8_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint16_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint32_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::write( uint64_t nData ) { + Bu::MutexLocker l( mWrite ); return qbWrite.write( (const char *)&nData, sizeof(nData) ); } Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) { + Bu::MutexLocker l( mWrite ); return qbRead.read( pData, nBytes ); } Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) { + Bu::MutexLocker l( mWrite ); return qbRead.peek( pData, nBytes, nOffset ); } Bu::size Bu::Client::getInputSize() { + Bu::MutexLocker l( mWrite ); return qbRead.getSize(); } Bu::size Bu::Client::getOutputSize() { + Bu::MutexLocker l( mWrite ); return qbWrite.getSize(); } @@ -240,6 +259,7 @@ Bu::size Bu::Client::tell() void Bu::Client::seek( Bu::size offset ) { + Bu::MutexLocker l( mRead ); return qbRead.seek( offset ); } @@ -265,6 +285,7 @@ void Bu::Client::flush() bool Bu::Client::canRead() { + Bu::MutexLocker l( mRead ); return qbRead.getSize() > 0; } -- cgit v1.2.3