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/protocoltelnet.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/stable/protocoltelnet.cpp') diff --git a/src/stable/protocoltelnet.cpp b/src/stable/protocoltelnet.cpp index 1461b0e..153af57 100644 --- a/src/stable/protocoltelnet.cpp +++ b/src/stable/protocoltelnet.cpp @@ -7,6 +7,7 @@ #include "bu/protocoltelnet.h" #include "bu/client.h" +#include "bu/mutexlocker.h" /* We apparently at least want defs for the lower 13, not sure we care about * the rest of the chars, maybe escape. @@ -81,11 +82,16 @@ Bu::ProtocolTelnet::~ProtocolTelnet() void Bu::ProtocolTelnet::onNewConnection( Bu::Client *pClient ) { + mRead.lock(); + mWrite.lock(); this->pClient = pClient; + mWrite.unlock(); + mRead.unlock(); } void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) { + MutexLocker l( mRead ); char bc; int iLeft; while( (iLeft = pClient->getInputSize()) ) @@ -233,7 +239,9 @@ void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) sDataBuf += bc; if( oEcho.isLocalSet() && bEcho ) { + mWrite.lock(); pClient->write( &bc, 1 ); + mWrite.unlock(); #ifdef __TELNET_DEBUG printf("%c", bc ); fflush( stdout ); @@ -246,7 +254,9 @@ void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) sDataBuf += bc; if( oEcho.isLocalSet() && bEcho ) { + mWrite.lock(); pClient->write( &bc, 1 ); + mWrite.unlock(); } } } @@ -291,6 +301,7 @@ void Bu::ProtocolTelnet::write( const Bu::String &sData ) void Bu::ProtocolTelnet::write( const char *pData, int iSize ) { + Bu::MutexLocker l( mWrite ); int iLast = 0, j; for( j = iLast; j < iSize; j++ ) { @@ -323,11 +334,13 @@ void Bu::ProtocolTelnet::write( char cData ) void Bu::ProtocolTelnet::flush() { + Bu::MutexLocker l( mWrite ); pClient->flush(); } void Bu::ProtocolTelnet::onWill( char cCode ) { + Bu::MutexLocker l( mWrite ); try { Option *pOpt = hOpts[cCode]; @@ -353,6 +366,7 @@ void Bu::ProtocolTelnet::onWill( char cCode ) void Bu::ProtocolTelnet::onWont( char cCode ) { + Bu::MutexLocker l( mWrite ); try { Option *pOpt = hOpts[cCode]; @@ -370,6 +384,7 @@ void Bu::ProtocolTelnet::onWont( char cCode ) void Bu::ProtocolTelnet::onDo( char cCode ) { + Bu::MutexLocker l( mWrite ); try { Option *pOpt = hOpts[cCode]; @@ -395,6 +410,7 @@ void Bu::ProtocolTelnet::onDo( char cCode ) void Bu::ProtocolTelnet::onDont( char cCode ) { + Bu::MutexLocker l( mWrite ); try { Option *pOpt = hOpts[cCode]; @@ -467,7 +483,9 @@ void Bu::ProtocolTelnet::onCtlChar( char cChr ) { sDataBuf.resize( sDataBuf.getSize()-1 ); char buf[3] = { CH_BS, ' ', CH_BS }; + mWrite.lock(); pClient->write( buf, 3 ); + mWrite.unlock(); } } break; -- cgit v1.2.3