diff options
author | Mike Buland <eichlan@xagasoft.com> | 2014-08-14 15:49:05 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2014-08-14 15:49:05 +0000 |
commit | c5f69c22ca51510da1674bf56982f8f1e3ca4a40 (patch) | |
tree | 344332bc84359d6da4e626ede92bf31ae5d1a52b /src/stable | |
parent | d55d73c7050e6452f18678e60b42a088175cb7ce (diff) | |
download | libbu++-c5f69c22ca51510da1674bf56982f8f1e3ca4a40.tar.gz libbu++-c5f69c22ca51510da1674bf56982f8f1e3ca4a40.tar.bz2 libbu++-c5f69c22ca51510da1674bf56982f8f1e3ca4a40.tar.xz libbu++-c5f69c22ca51510da1674bf56982f8f1e3ca4a40.zip |
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.
Diffstat (limited to 'src/stable')
-rw-r--r-- | src/stable/client.cpp | 21 | ||||
-rw-r--r-- | src/stable/client.h | 3 | ||||
-rw-r--r-- | src/stable/protocoltelnet.cpp | 18 | ||||
-rw-r--r-- | src/stable/protocoltelnet.h | 3 |
4 files changed, 45 insertions, 0 deletions
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 @@ | |||
12 | #include "bu/protocol.h" | 12 | #include "bu/protocol.h" |
13 | #include "bu/clientlink.h" | 13 | #include "bu/clientlink.h" |
14 | #include "bu/clientlinkfactory.h" | 14 | #include "bu/clientlinkfactory.h" |
15 | #include "bu/mutexlocker.h" | ||
15 | 16 | ||
16 | /** Read buffer size. */ | 17 | /** Read buffer size. */ |
17 | #define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess | 18 | #define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess |
@@ -39,6 +40,7 @@ Bu::Client::~Client() | |||
39 | 40 | ||
40 | void Bu::Client::processInput() | 41 | void Bu::Client::processInput() |
41 | { | 42 | { |
43 | mRead.lock(); | ||
42 | char buf[RBS]; | 44 | char buf[RBS]; |
43 | Bu::size nRead, nTotal=0; | 45 | Bu::size nRead, nTotal=0; |
44 | 46 | ||
@@ -67,6 +69,7 @@ void Bu::Client::processInput() | |||
67 | break; | 69 | break; |
68 | } | 70 | } |
69 | } | 71 | } |
72 | mRead.unlock(); | ||
70 | 73 | ||
71 | if( nTotal == 0 ) | 74 | if( nTotal == 0 ) |
72 | { | 75 | { |
@@ -82,6 +85,7 @@ void Bu::Client::processInput() | |||
82 | 85 | ||
83 | void Bu::Client::processOutput() | 86 | void Bu::Client::processOutput() |
84 | { | 87 | { |
88 | mWrite.lock(); | ||
85 | char buf[RBS]; | 89 | char buf[RBS]; |
86 | if( qbWrite.getSize() > 0 ) | 90 | if( qbWrite.getSize() > 0 ) |
87 | { | 91 | { |
@@ -91,6 +95,7 @@ void Bu::Client::processOutput() | |||
91 | qbWrite.seek( nReal ); | 95 | qbWrite.seek( nReal ); |
92 | pTopStream->flush(); | 96 | pTopStream->flush(); |
93 | } | 97 | } |
98 | mWrite.unlock(); | ||
94 | } | 99 | } |
95 | 100 | ||
96 | void Bu::Client::setProtocol( Protocol *pProto ) | 101 | void Bu::Client::setProtocol( Protocol *pProto ) |
@@ -128,71 +133,85 @@ bool Bu::Client::isOpen() | |||
128 | 133 | ||
129 | Bu::size Bu::Client::write( const Bu::String &sData ) | 134 | Bu::size Bu::Client::write( const Bu::String &sData ) |
130 | { | 135 | { |
136 | Bu::MutexLocker l( mWrite ); | ||
131 | return qbWrite.write( sData.getStr(), sData.getSize() ); | 137 | return qbWrite.write( sData.getStr(), sData.getSize() ); |
132 | } | 138 | } |
133 | 139 | ||
134 | Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) | 140 | Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) |
135 | { | 141 | { |
142 | Bu::MutexLocker l( mWrite ); | ||
136 | return qbWrite.write( pData, nBytes ); | 143 | return qbWrite.write( pData, nBytes ); |
137 | } | 144 | } |
138 | 145 | ||
139 | Bu::size Bu::Client::write( int8_t nData ) | 146 | Bu::size Bu::Client::write( int8_t nData ) |
140 | { | 147 | { |
148 | Bu::MutexLocker l( mWrite ); | ||
141 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 149 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
142 | } | 150 | } |
143 | 151 | ||
144 | Bu::size Bu::Client::write( int16_t nData ) | 152 | Bu::size Bu::Client::write( int16_t nData ) |
145 | { | 153 | { |
154 | Bu::MutexLocker l( mWrite ); | ||
146 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 155 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
147 | } | 156 | } |
148 | 157 | ||
149 | Bu::size Bu::Client::write( int32_t nData ) | 158 | Bu::size Bu::Client::write( int32_t nData ) |
150 | { | 159 | { |
160 | Bu::MutexLocker l( mWrite ); | ||
151 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 161 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
152 | } | 162 | } |
153 | 163 | ||
154 | Bu::size Bu::Client::write( int64_t nData ) | 164 | Bu::size Bu::Client::write( int64_t nData ) |
155 | { | 165 | { |
166 | Bu::MutexLocker l( mWrite ); | ||
156 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 167 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
157 | } | 168 | } |
158 | 169 | ||
159 | Bu::size Bu::Client::write( uint8_t nData ) | 170 | Bu::size Bu::Client::write( uint8_t nData ) |
160 | { | 171 | { |
172 | Bu::MutexLocker l( mWrite ); | ||
161 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 173 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
162 | } | 174 | } |
163 | 175 | ||
164 | Bu::size Bu::Client::write( uint16_t nData ) | 176 | Bu::size Bu::Client::write( uint16_t nData ) |
165 | { | 177 | { |
178 | Bu::MutexLocker l( mWrite ); | ||
166 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 179 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
167 | } | 180 | } |
168 | 181 | ||
169 | Bu::size Bu::Client::write( uint32_t nData ) | 182 | Bu::size Bu::Client::write( uint32_t nData ) |
170 | { | 183 | { |
184 | Bu::MutexLocker l( mWrite ); | ||
171 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 185 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
172 | } | 186 | } |
173 | 187 | ||
174 | Bu::size Bu::Client::write( uint64_t nData ) | 188 | Bu::size Bu::Client::write( uint64_t nData ) |
175 | { | 189 | { |
190 | Bu::MutexLocker l( mWrite ); | ||
176 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 191 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
177 | } | 192 | } |
178 | 193 | ||
179 | Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) | 194 | Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) |
180 | { | 195 | { |
196 | Bu::MutexLocker l( mWrite ); | ||
181 | return qbRead.read( pData, nBytes ); | 197 | return qbRead.read( pData, nBytes ); |
182 | } | 198 | } |
183 | 199 | ||
184 | Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) | 200 | Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) |
185 | { | 201 | { |
202 | Bu::MutexLocker l( mWrite ); | ||
186 | return qbRead.peek( pData, nBytes, nOffset ); | 203 | return qbRead.peek( pData, nBytes, nOffset ); |
187 | } | 204 | } |
188 | 205 | ||
189 | Bu::size Bu::Client::getInputSize() | 206 | Bu::size Bu::Client::getInputSize() |
190 | { | 207 | { |
208 | Bu::MutexLocker l( mWrite ); | ||
191 | return qbRead.getSize(); | 209 | return qbRead.getSize(); |
192 | } | 210 | } |
193 | 211 | ||
194 | Bu::size Bu::Client::getOutputSize() | 212 | Bu::size Bu::Client::getOutputSize() |
195 | { | 213 | { |
214 | Bu::MutexLocker l( mWrite ); | ||
196 | return qbWrite.getSize(); | 215 | return qbWrite.getSize(); |
197 | } | 216 | } |
198 | 217 | ||
@@ -240,6 +259,7 @@ Bu::size Bu::Client::tell() | |||
240 | 259 | ||
241 | void Bu::Client::seek( Bu::size offset ) | 260 | void Bu::Client::seek( Bu::size offset ) |
242 | { | 261 | { |
262 | Bu::MutexLocker l( mRead ); | ||
243 | return qbRead.seek( offset ); | 263 | return qbRead.seek( offset ); |
244 | } | 264 | } |
245 | 265 | ||
@@ -265,6 +285,7 @@ void Bu::Client::flush() | |||
265 | 285 | ||
266 | bool Bu::Client::canRead() | 286 | bool Bu::Client::canRead() |
267 | { | 287 | { |
288 | Bu::MutexLocker l( mRead ); | ||
268 | return qbRead.getSize() > 0; | 289 | return qbRead.getSize() > 0; |
269 | } | 290 | } |
270 | 291 | ||
diff --git a/src/stable/client.h b/src/stable/client.h index 89e8144..ac882eb 100644 --- a/src/stable/client.h +++ b/src/stable/client.h | |||
@@ -13,6 +13,7 @@ | |||
13 | #include "bu/config.h" | 13 | #include "bu/config.h" |
14 | #include "bu/string.h" | 14 | #include "bu/string.h" |
15 | #include "bu/queuebuf.h" | 15 | #include "bu/queuebuf.h" |
16 | #include "bu/mutex.h" | ||
16 | 17 | ||
17 | namespace Bu | 18 | namespace Bu |
18 | { | 19 | { |
@@ -127,6 +128,8 @@ namespace Bu | |||
127 | Bu::QueueBuf qbWrite; | 128 | Bu::QueueBuf qbWrite; |
128 | bool bWantsDisconnect; | 129 | bool bWantsDisconnect; |
129 | class Bu::ClientLinkFactory *pfLink; | 130 | class Bu::ClientLinkFactory *pfLink; |
131 | Bu::Mutex mRead; | ||
132 | Bu::Mutex mWrite; | ||
130 | }; | 133 | }; |
131 | } | 134 | } |
132 | 135 | ||
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 @@ | |||
7 | 7 | ||
8 | #include "bu/protocoltelnet.h" | 8 | #include "bu/protocoltelnet.h" |
9 | #include "bu/client.h" | 9 | #include "bu/client.h" |
10 | #include "bu/mutexlocker.h" | ||
10 | 11 | ||
11 | /* We apparently at least want defs for the lower 13, not sure we care about | 12 | /* We apparently at least want defs for the lower 13, not sure we care about |
12 | * the rest of the chars, maybe escape. | 13 | * the rest of the chars, maybe escape. |
@@ -81,11 +82,16 @@ Bu::ProtocolTelnet::~ProtocolTelnet() | |||
81 | 82 | ||
82 | void Bu::ProtocolTelnet::onNewConnection( Bu::Client *pClient ) | 83 | void Bu::ProtocolTelnet::onNewConnection( Bu::Client *pClient ) |
83 | { | 84 | { |
85 | mRead.lock(); | ||
86 | mWrite.lock(); | ||
84 | this->pClient = pClient; | 87 | this->pClient = pClient; |
88 | mWrite.unlock(); | ||
89 | mRead.unlock(); | ||
85 | } | 90 | } |
86 | 91 | ||
87 | void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) | 92 | void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) |
88 | { | 93 | { |
94 | MutexLocker l( mRead ); | ||
89 | char bc; | 95 | char bc; |
90 | int iLeft; | 96 | int iLeft; |
91 | while( (iLeft = pClient->getInputSize()) ) | 97 | while( (iLeft = pClient->getInputSize()) ) |
@@ -233,7 +239,9 @@ void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) | |||
233 | sDataBuf += bc; | 239 | sDataBuf += bc; |
234 | if( oEcho.isLocalSet() && bEcho ) | 240 | if( oEcho.isLocalSet() && bEcho ) |
235 | { | 241 | { |
242 | mWrite.lock(); | ||
236 | pClient->write( &bc, 1 ); | 243 | pClient->write( &bc, 1 ); |
244 | mWrite.unlock(); | ||
237 | #ifdef __TELNET_DEBUG | 245 | #ifdef __TELNET_DEBUG |
238 | printf("%c", bc ); | 246 | printf("%c", bc ); |
239 | fflush( stdout ); | 247 | fflush( stdout ); |
@@ -246,7 +254,9 @@ void Bu::ProtocolTelnet::onNewData( Bu::Client *pClient ) | |||
246 | sDataBuf += bc; | 254 | sDataBuf += bc; |
247 | if( oEcho.isLocalSet() && bEcho ) | 255 | if( oEcho.isLocalSet() && bEcho ) |
248 | { | 256 | { |
257 | mWrite.lock(); | ||
249 | pClient->write( &bc, 1 ); | 258 | pClient->write( &bc, 1 ); |
259 | mWrite.unlock(); | ||
250 | } | 260 | } |
251 | } | 261 | } |
252 | } | 262 | } |
@@ -291,6 +301,7 @@ void Bu::ProtocolTelnet::write( const Bu::String &sData ) | |||
291 | 301 | ||
292 | void Bu::ProtocolTelnet::write( const char *pData, int iSize ) | 302 | void Bu::ProtocolTelnet::write( const char *pData, int iSize ) |
293 | { | 303 | { |
304 | Bu::MutexLocker l( mWrite ); | ||
294 | int iLast = 0, j; | 305 | int iLast = 0, j; |
295 | for( j = iLast; j < iSize; j++ ) | 306 | for( j = iLast; j < iSize; j++ ) |
296 | { | 307 | { |
@@ -323,11 +334,13 @@ void Bu::ProtocolTelnet::write( char cData ) | |||
323 | 334 | ||
324 | void Bu::ProtocolTelnet::flush() | 335 | void Bu::ProtocolTelnet::flush() |
325 | { | 336 | { |
337 | Bu::MutexLocker l( mWrite ); | ||
326 | pClient->flush(); | 338 | pClient->flush(); |
327 | } | 339 | } |
328 | 340 | ||
329 | void Bu::ProtocolTelnet::onWill( char cCode ) | 341 | void Bu::ProtocolTelnet::onWill( char cCode ) |
330 | { | 342 | { |
343 | Bu::MutexLocker l( mWrite ); | ||
331 | try | 344 | try |
332 | { | 345 | { |
333 | Option *pOpt = hOpts[cCode]; | 346 | Option *pOpt = hOpts[cCode]; |
@@ -353,6 +366,7 @@ void Bu::ProtocolTelnet::onWill( char cCode ) | |||
353 | 366 | ||
354 | void Bu::ProtocolTelnet::onWont( char cCode ) | 367 | void Bu::ProtocolTelnet::onWont( char cCode ) |
355 | { | 368 | { |
369 | Bu::MutexLocker l( mWrite ); | ||
356 | try | 370 | try |
357 | { | 371 | { |
358 | Option *pOpt = hOpts[cCode]; | 372 | Option *pOpt = hOpts[cCode]; |
@@ -370,6 +384,7 @@ void Bu::ProtocolTelnet::onWont( char cCode ) | |||
370 | 384 | ||
371 | void Bu::ProtocolTelnet::onDo( char cCode ) | 385 | void Bu::ProtocolTelnet::onDo( char cCode ) |
372 | { | 386 | { |
387 | Bu::MutexLocker l( mWrite ); | ||
373 | try | 388 | try |
374 | { | 389 | { |
375 | Option *pOpt = hOpts[cCode]; | 390 | Option *pOpt = hOpts[cCode]; |
@@ -395,6 +410,7 @@ void Bu::ProtocolTelnet::onDo( char cCode ) | |||
395 | 410 | ||
396 | void Bu::ProtocolTelnet::onDont( char cCode ) | 411 | void Bu::ProtocolTelnet::onDont( char cCode ) |
397 | { | 412 | { |
413 | Bu::MutexLocker l( mWrite ); | ||
398 | try | 414 | try |
399 | { | 415 | { |
400 | Option *pOpt = hOpts[cCode]; | 416 | Option *pOpt = hOpts[cCode]; |
@@ -467,7 +483,9 @@ void Bu::ProtocolTelnet::onCtlChar( char cChr ) | |||
467 | { | 483 | { |
468 | sDataBuf.resize( sDataBuf.getSize()-1 ); | 484 | sDataBuf.resize( sDataBuf.getSize()-1 ); |
469 | char buf[3] = { CH_BS, ' ', CH_BS }; | 485 | char buf[3] = { CH_BS, ' ', CH_BS }; |
486 | mWrite.lock(); | ||
470 | pClient->write( buf, 3 ); | 487 | pClient->write( buf, 3 ); |
488 | mWrite.unlock(); | ||
471 | } | 489 | } |
472 | } | 490 | } |
473 | break; | 491 | break; |
diff --git a/src/stable/protocoltelnet.h b/src/stable/protocoltelnet.h index 60faef4..19298dc 100644 --- a/src/stable/protocoltelnet.h +++ b/src/stable/protocoltelnet.h | |||
@@ -11,6 +11,7 @@ | |||
11 | #include "bu/protocol.h" | 11 | #include "bu/protocol.h" |
12 | #include "bu/hash.h" | 12 | #include "bu/hash.h" |
13 | #include "bu/string.h" | 13 | #include "bu/string.h" |
14 | #include "bu/mutex.h" | ||
14 | 15 | ||
15 | // #define __TELNET_DEBUG | 16 | // #define __TELNET_DEBUG |
16 | 17 | ||
@@ -210,6 +211,8 @@ namespace Bu | |||
210 | #endif | 211 | #endif |
211 | 212 | ||
212 | private: | 213 | private: |
214 | Mutex mRead; | ||
215 | Mutex mWrite; | ||
213 | Client *pClient; | 216 | Client *pClient; |
214 | 217 | ||
215 | Bu::String sDataBuf; /**< Buffer for regular line data. */ | 218 | Bu::String sDataBuf; /**< Buffer for regular line data. */ |