diff options
author | Mike Buland <mbuland@penny-arcade.com> | 2018-01-29 00:47:50 -0800 |
---|---|---|
committer | Mike Buland <mbuland@penny-arcade.com> | 2018-01-29 00:47:50 -0800 |
commit | 1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078 (patch) | |
tree | 1417fdcc919d74ca25dc33b27714d10dd169700e /src | |
parent | 65ffc3b58ca865a7f83bf39290df1760c35b57f0 (diff) | |
download | libbu++-1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078.tar.gz libbu++-1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078.tar.bz2 libbu++-1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078.tar.xz libbu++-1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/stable/client.cpp | 32 | ||||
-rw-r--r-- | src/stable/client.h | 2 | ||||
-rw-r--r-- | src/stable/multiserver.cpp | 6 | ||||
-rw-r--r-- | src/stable/string.cpp | 10 | ||||
-rw-r--r-- | src/unstable/protocolwebsocket.cpp | 23 | ||||
-rw-r--r-- | src/unstable/protocolwebsocket.h | 3 | ||||
-rw-r--r-- | src/unstable/readwritemutex.cpp | 4 |
7 files changed, 69 insertions, 11 deletions
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, | |||
25 | bWantsDisconnect( false ), | 25 | bWantsDisconnect( false ), |
26 | pfLink( pfLink ) | 26 | pfLink( pfLink ) |
27 | { | 27 | { |
28 | Bu::ReadWriteMutex::WriteLocker lGlobal( mGlobal ); | ||
28 | lFilts.prepend( pSocket ); | 29 | lFilts.prepend( pSocket ); |
29 | } | 30 | } |
30 | 31 | ||
31 | Bu::Client::~Client() | 32 | Bu::Client::~Client() |
32 | { | 33 | { |
34 | Bu::ReadWriteMutex::WriteLocker lGlobal( mGlobal ); | ||
33 | for( FilterList::iterator i = lFilts.begin(); i; i++ ) | 35 | for( FilterList::iterator i = lFilts.begin(); i; i++ ) |
34 | { | 36 | { |
35 | delete *i; | 37 | delete *i; |
@@ -40,6 +42,7 @@ Bu::Client::~Client() | |||
40 | 42 | ||
41 | void Bu::Client::processInput() | 43 | void Bu::Client::processInput() |
42 | { | 44 | { |
45 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
43 | mRead.lock(); | 46 | mRead.lock(); |
44 | char buf[RBS]; | 47 | char buf[RBS]; |
45 | Bu::size nRead, nTotal=0; | 48 | Bu::size nRead, nTotal=0; |
@@ -85,6 +88,7 @@ void Bu::Client::processInput() | |||
85 | 88 | ||
86 | void Bu::Client::processOutput() | 89 | void Bu::Client::processOutput() |
87 | { | 90 | { |
91 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
88 | mWrite.lock(); | 92 | mWrite.lock(); |
89 | char buf[RBS]; | 93 | char buf[RBS]; |
90 | if( qbWrite.getSize() > 0 ) | 94 | if( qbWrite.getSize() > 0 ) |
@@ -100,17 +104,20 @@ void Bu::Client::processOutput() | |||
100 | 104 | ||
101 | void Bu::Client::setProtocol( Protocol *pProto ) | 105 | void Bu::Client::setProtocol( Protocol *pProto ) |
102 | { | 106 | { |
107 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
103 | this->pProto = pProto; | 108 | this->pProto = pProto; |
104 | this->pProto->onNewConnection( this ); | 109 | this->pProto->onNewConnection( this ); |
105 | } | 110 | } |
106 | 111 | ||
107 | Bu::Protocol *Bu::Client::getProtocol() | 112 | Bu::Protocol *Bu::Client::getProtocol() |
108 | { | 113 | { |
114 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
109 | return pProto; | 115 | return pProto; |
110 | } | 116 | } |
111 | 117 | ||
112 | void Bu::Client::clearProtocol() | 118 | void Bu::Client::clearProtocol() |
113 | { | 119 | { |
120 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
114 | pProto = NULL; | 121 | pProto = NULL; |
115 | } | 122 | } |
116 | /* | 123 | /* |
@@ -127,127 +134,149 @@ Bu::String &Bu::Client::getOutput() | |||
127 | 134 | ||
128 | bool Bu::Client::isOpen() | 135 | bool Bu::Client::isOpen() |
129 | { | 136 | { |
137 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
130 | if( !pTopStream ) return false; | 138 | if( !pTopStream ) return false; |
131 | return pTopStream->isOpen(); | 139 | return pTopStream->isOpen(); |
132 | } | 140 | } |
133 | 141 | ||
134 | Bu::size Bu::Client::write( const Bu::String &sData ) | 142 | Bu::size Bu::Client::write( const Bu::String &sData ) |
135 | { | 143 | { |
144 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
136 | Bu::MutexLocker l( mWrite ); | 145 | Bu::MutexLocker l( mWrite ); |
137 | return qbWrite.write( sData.getStr(), sData.getSize() ); | 146 | return qbWrite.write( sData.getStr(), sData.getSize() ); |
138 | } | 147 | } |
139 | 148 | ||
140 | Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) | 149 | Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) |
141 | { | 150 | { |
151 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
142 | Bu::MutexLocker l( mWrite ); | 152 | Bu::MutexLocker l( mWrite ); |
143 | return qbWrite.write( pData, nBytes ); | 153 | return qbWrite.write( pData, nBytes ); |
144 | } | 154 | } |
145 | 155 | ||
146 | Bu::size Bu::Client::write( int8_t nData ) | 156 | Bu::size Bu::Client::write( int8_t nData ) |
147 | { | 157 | { |
158 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
148 | Bu::MutexLocker l( mWrite ); | 159 | Bu::MutexLocker l( mWrite ); |
149 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 160 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
150 | } | 161 | } |
151 | 162 | ||
152 | Bu::size Bu::Client::write( int16_t nData ) | 163 | Bu::size Bu::Client::write( int16_t nData ) |
153 | { | 164 | { |
165 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
154 | Bu::MutexLocker l( mWrite ); | 166 | Bu::MutexLocker l( mWrite ); |
155 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 167 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
156 | } | 168 | } |
157 | 169 | ||
158 | Bu::size Bu::Client::write( int32_t nData ) | 170 | Bu::size Bu::Client::write( int32_t nData ) |
159 | { | 171 | { |
172 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
160 | Bu::MutexLocker l( mWrite ); | 173 | Bu::MutexLocker l( mWrite ); |
161 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 174 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
162 | } | 175 | } |
163 | 176 | ||
164 | Bu::size Bu::Client::write( int64_t nData ) | 177 | Bu::size Bu::Client::write( int64_t nData ) |
165 | { | 178 | { |
179 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
166 | Bu::MutexLocker l( mWrite ); | 180 | Bu::MutexLocker l( mWrite ); |
167 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 181 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
168 | } | 182 | } |
169 | 183 | ||
170 | Bu::size Bu::Client::write( uint8_t nData ) | 184 | Bu::size Bu::Client::write( uint8_t nData ) |
171 | { | 185 | { |
186 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
172 | Bu::MutexLocker l( mWrite ); | 187 | Bu::MutexLocker l( mWrite ); |
173 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 188 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
174 | } | 189 | } |
175 | 190 | ||
176 | Bu::size Bu::Client::write( uint16_t nData ) | 191 | Bu::size Bu::Client::write( uint16_t nData ) |
177 | { | 192 | { |
193 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
178 | Bu::MutexLocker l( mWrite ); | 194 | Bu::MutexLocker l( mWrite ); |
179 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 195 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
180 | } | 196 | } |
181 | 197 | ||
182 | Bu::size Bu::Client::write( uint32_t nData ) | 198 | Bu::size Bu::Client::write( uint32_t nData ) |
183 | { | 199 | { |
200 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
184 | Bu::MutexLocker l( mWrite ); | 201 | Bu::MutexLocker l( mWrite ); |
185 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 202 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
186 | } | 203 | } |
187 | 204 | ||
188 | Bu::size Bu::Client::write( uint64_t nData ) | 205 | Bu::size Bu::Client::write( uint64_t nData ) |
189 | { | 206 | { |
207 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
190 | Bu::MutexLocker l( mWrite ); | 208 | Bu::MutexLocker l( mWrite ); |
191 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); | 209 | return qbWrite.write( (const char *)&nData, sizeof(nData) ); |
192 | } | 210 | } |
193 | 211 | ||
194 | Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) | 212 | Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) |
195 | { | 213 | { |
214 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
196 | Bu::MutexLocker l( mWrite ); | 215 | Bu::MutexLocker l( mWrite ); |
197 | return qbRead.read( pData, nBytes ); | 216 | return qbRead.read( pData, nBytes ); |
198 | } | 217 | } |
199 | 218 | ||
200 | Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) | 219 | Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) |
201 | { | 220 | { |
221 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
202 | Bu::MutexLocker l( mWrite ); | 222 | Bu::MutexLocker l( mWrite ); |
203 | return qbRead.peek( pData, nBytes, nOffset ); | 223 | return qbRead.peek( pData, nBytes, nOffset ); |
204 | } | 224 | } |
205 | 225 | ||
206 | Bu::size Bu::Client::getInputSize() | 226 | Bu::size Bu::Client::getInputSize() |
207 | { | 227 | { |
228 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
208 | Bu::MutexLocker l( mWrite ); | 229 | Bu::MutexLocker l( mWrite ); |
209 | return qbRead.getSize(); | 230 | return qbRead.getSize(); |
210 | } | 231 | } |
211 | 232 | ||
212 | Bu::size Bu::Client::getOutputSize() | 233 | Bu::size Bu::Client::getOutputSize() |
213 | { | 234 | { |
235 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
214 | Bu::MutexLocker l( mWrite ); | 236 | Bu::MutexLocker l( mWrite ); |
215 | return qbWrite.getSize(); | 237 | return qbWrite.getSize(); |
216 | } | 238 | } |
217 | 239 | ||
218 | const Bu::TcpSocket *Bu::Client::getSocket() const | 240 | const Bu::TcpSocket *Bu::Client::getSocket() const |
219 | { | 241 | { |
242 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
220 | return pSocket; | 243 | return pSocket; |
221 | } | 244 | } |
222 | 245 | ||
223 | void Bu::Client::disconnect() | 246 | void Bu::Client::disconnect() |
224 | { | 247 | { |
248 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
225 | bWantsDisconnect = true; | 249 | bWantsDisconnect = true; |
226 | } | 250 | } |
227 | 251 | ||
228 | bool Bu::Client::wantsDisconnect() | 252 | bool Bu::Client::wantsDisconnect() |
229 | { | 253 | { |
254 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
230 | return bWantsDisconnect; | 255 | return bWantsDisconnect; |
231 | } | 256 | } |
232 | 257 | ||
233 | void Bu::Client::close() | 258 | void Bu::Client::close() |
234 | { | 259 | { |
260 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
235 | pTopStream->close(); | 261 | pTopStream->close(); |
236 | } | 262 | } |
237 | 263 | ||
238 | Bu::ClientLink *Bu::Client::getLink() | 264 | Bu::ClientLink *Bu::Client::getLink() |
239 | { | 265 | { |
266 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
240 | return pfLink->createLink( this ); | 267 | return pfLink->createLink( this ); |
241 | } | 268 | } |
242 | 269 | ||
243 | void Bu::Client::onMessage( const Bu::String &sMsg ) | 270 | void Bu::Client::onMessage( const Bu::String &sMsg ) |
244 | { | 271 | { |
272 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
245 | if( pProto ) | 273 | if( pProto ) |
246 | pProto->onMessage( this, sMsg ); | 274 | pProto->onMessage( this, sMsg ); |
247 | } | 275 | } |
248 | 276 | ||
249 | void Bu::Client::tick() | 277 | void Bu::Client::tick() |
250 | { | 278 | { |
279 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
251 | if( pProto ) | 280 | if( pProto ) |
252 | pProto->onTick( this ); | 281 | pProto->onTick( this ); |
253 | } | 282 | } |
@@ -259,6 +288,7 @@ Bu::size Bu::Client::tell() | |||
259 | 288 | ||
260 | void Bu::Client::seek( Bu::size offset ) | 289 | void Bu::Client::seek( Bu::size offset ) |
261 | { | 290 | { |
291 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
262 | Bu::MutexLocker l( mRead ); | 292 | Bu::MutexLocker l( mRead ); |
263 | return qbRead.seek( offset ); | 293 | return qbRead.seek( offset ); |
264 | } | 294 | } |
@@ -280,11 +310,13 @@ bool Bu::Client::isEos() | |||
280 | 310 | ||
281 | void Bu::Client::flush() | 311 | void Bu::Client::flush() |
282 | { | 312 | { |
313 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
283 | processOutput(); | 314 | processOutput(); |
284 | } | 315 | } |
285 | 316 | ||
286 | bool Bu::Client::canRead() | 317 | bool Bu::Client::canRead() |
287 | { | 318 | { |
319 | Bu::ReadWriteMutex::ReadLocker lGlobal( mGlobal ); | ||
288 | Bu::MutexLocker l( mRead ); | 320 | Bu::MutexLocker l( mRead ); |
289 | return qbRead.getSize() > 0; | 321 | return qbRead.getSize() > 0; |
290 | } | 322 | } |
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 @@ | |||
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 | #include "bu/mutex.h" |
17 | #include "bu/readwritemutex.h" | ||
17 | 18 | ||
18 | namespace Bu | 19 | namespace Bu |
19 | { | 20 | { |
@@ -130,6 +131,7 @@ namespace Bu | |||
130 | class Bu::ClientLinkFactory *pfLink; | 131 | class Bu::ClientLinkFactory *pfLink; |
131 | Bu::Mutex mRead; | 132 | Bu::Mutex mRead; |
132 | Bu::Mutex mWrite; | 133 | Bu::Mutex mWrite; |
134 | mutable Bu::ReadWriteMutex mGlobal; | ||
133 | }; | 135 | }; |
134 | } | 136 | } |
135 | 137 | ||
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 @@ | |||
11 | 11 | ||
12 | #include "bu/config.h" | 12 | #include "bu/config.h" |
13 | 13 | ||
14 | #include "bu/sio.h" | ||
15 | |||
14 | Bu::MultiServer::MultiServer() | 16 | Bu::MultiServer::MultiServer() |
15 | { | 17 | { |
16 | } | 18 | } |
@@ -40,7 +42,9 @@ void Bu::MultiServer::onNewConnection( Bu::Client *pClient, int nPort ) | |||
40 | 42 | ||
41 | void Bu::MultiServer::onClosedConnection( Bu::Client *pClient ) | 43 | void Bu::MultiServer::onClosedConnection( Bu::Client *pClient ) |
42 | { | 44 | { |
43 | delete pClient->getProtocol(); | 45 | Bu::Protocol *pProto = pClient->getProtocol(); |
46 | pClient->clearProtocol(); | ||
47 | delete pProto; | ||
44 | } | 48 | } |
45 | 49 | ||
46 | void Bu::MultiServer::shutdown() | 50 | 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 ) | |||
201 | 201 | ||
202 | _hardCopy(); | 202 | _hardCopy(); |
203 | 203 | ||
204 | if( core->pLast && core->pLast->nLength < nMinSize ) | 204 | if( core->pLast && core->pLast->nLength+1 < nMinSize ) |
205 | { | 205 | { |
206 | int nAmnt = nMinSize - core->pLast->nLength; | 206 | int nAmnt = nMinSize - core->pLast->nLength; |
207 | if( nAmnt > nLen ) | 207 | if( nAmnt > nLen ) |
@@ -228,9 +228,9 @@ void Bu::String::append( const char *pData, long nStart, long nLen ) | |||
228 | 228 | ||
229 | void Bu::String::append( const char &cData ) | 229 | void Bu::String::append( const char &cData ) |
230 | { | 230 | { |
231 | if( core->pLast && core->pLast->nLength < nMinSize ) | 231 | _hardCopy(); |
232 | if( core->pLast && core->pLast->nLength+1 < nMinSize ) | ||
232 | { | 233 | { |
233 | _hardCopy(); | ||
234 | core->pLast->pData[core->pLast->nLength] = cData; | 234 | core->pLast->pData[core->pLast->nLength] = cData; |
235 | ++core->pLast->nLength; ++core->nLength; | 235 | ++core->pLast->nLength; ++core->nLength; |
236 | // pLast->pData[pLast->nLength] = (char)0; | 236 | // pLast->pData[pLast->nLength] = (char)0; |
@@ -615,9 +615,9 @@ Bu::String &Bu::String::operator+=( const Bu::String::const_iterator &i ) | |||
615 | 615 | ||
616 | Bu::String &Bu::String::operator+=( const char cData ) | 616 | Bu::String &Bu::String::operator+=( const char cData ) |
617 | { | 617 | { |
618 | if( core->pLast && core->pLast->nLength < nMinSize ) | ||
619 | { | ||
620 | _hardCopy(); | 618 | _hardCopy(); |
619 | if( core->pLast && core->pLast->nLength+1 < nMinSize ) | ||
620 | { | ||
621 | core->pLast->pData[core->pLast->nLength] = cData; | 621 | core->pLast->pData[core->pLast->nLength] = cData; |
622 | ++core->pLast->nLength; ++core->nLength; | 622 | ++core->pLast->nLength; ++core->nLength; |
623 | // pLast->pData[pLast->nLength] = (char)0; | 623 | // pLast->pData[pLast->nLength] = (char)0; |
diff --git a/src/unstable/protocolwebsocket.cpp b/src/unstable/protocolwebsocket.cpp index 9200904..576249c 100644 --- a/src/unstable/protocolwebsocket.cpp +++ b/src/unstable/protocolwebsocket.cpp | |||
@@ -9,6 +9,7 @@ | |||
9 | #include "bu/protocolwebsocket.h" | 9 | #include "bu/protocolwebsocket.h" |
10 | 10 | ||
11 | #include "bu/sio.h" | 11 | #include "bu/sio.h" |
12 | #include "bu/fmt.h" | ||
12 | #include "bu/client.h" | 13 | #include "bu/client.h" |
13 | 14 | ||
14 | #include "bu/membuf.h" | 15 | #include "bu/membuf.h" |
@@ -16,6 +17,8 @@ | |||
16 | #include "bu/sha1.h" | 17 | #include "bu/sha1.h" |
17 | #include "bu/json.h" | 18 | #include "bu/json.h" |
18 | 19 | ||
20 | #include "bu/mutexlocker.h" | ||
21 | |||
19 | #include <stdlib.h> | 22 | #include <stdlib.h> |
20 | 23 | ||
21 | Bu::ProtocolWebSocket::ProtocolWebSocket() : | 24 | Bu::ProtocolWebSocket::ProtocolWebSocket() : |
@@ -25,11 +28,17 @@ Bu::ProtocolWebSocket::ProtocolWebSocket() : | |||
25 | 28 | ||
26 | Bu::ProtocolWebSocket::~ProtocolWebSocket() | 29 | Bu::ProtocolWebSocket::~ProtocolWebSocket() |
27 | { | 30 | { |
31 | mClient.lock(); | ||
32 | printf("ProtocolWebSocket::~ProtocolWebSocket(): Clearing pClient (%p)\n", (void *)this->pClient ); | ||
33 | this->pClient = NULL; | ||
34 | mClient.unlock(); | ||
28 | } | 35 | } |
29 | 36 | ||
30 | void Bu::ProtocolWebSocket::onNewConnection( Bu::Client *pClient ) | 37 | void Bu::ProtocolWebSocket::onNewConnection( Bu::Client *pClient ) |
31 | { | 38 | { |
39 | mClient.lock(); | ||
32 | this->pClient = pClient; | 40 | this->pClient = pClient; |
41 | mClient.unlock(); | ||
33 | } | 42 | } |
34 | 43 | ||
35 | void Bu::ProtocolWebSocket::onNewData( Bu::Client * /*pClient*/ ) | 44 | void Bu::ProtocolWebSocket::onNewData( Bu::Client * /*pClient*/ ) |
@@ -95,6 +104,9 @@ void Bu::ProtocolWebSocket::writeMessage( const Bu::String &sData, | |||
95 | } | 104 | } |
96 | Bu::println(""); | 105 | Bu::println(""); |
97 | */ | 106 | */ |
107 | Bu::MutexLocker l( mClient ); | ||
108 | if( pClient == NULL ) | ||
109 | return; | ||
98 | pClient->write( cHeader, idx ); | 110 | pClient->write( cHeader, idx ); |
99 | pClient->write( sData ); | 111 | pClient->write( sData ); |
100 | } | 112 | } |
@@ -108,18 +120,21 @@ bool Bu::ProtocolWebSocket::stateProtoId() | |||
108 | Bu::StringList lChunks = sLine.split(' '); | 120 | Bu::StringList lChunks = sLine.split(' '); |
109 | if( lChunks.getSize() != 3 ) | 121 | if( lChunks.getSize() != 3 ) |
110 | { | 122 | { |
123 | Bu::MutexLocker l( mClient ); | ||
111 | pClient->disconnect(); | 124 | pClient->disconnect(); |
112 | return false; | 125 | return false; |
113 | } | 126 | } |
114 | Bu::StringList::iterator i = lChunks.begin(); | 127 | Bu::StringList::iterator i = lChunks.begin(); |
115 | if( *i != "GET" ) | 128 | if( *i != "GET" ) |
116 | { | 129 | { |
130 | Bu::MutexLocker l( mClient ); | ||
117 | pClient->disconnect(); | 131 | pClient->disconnect(); |
118 | return false; | 132 | return false; |
119 | } | 133 | } |
120 | sPath = *(++i); | 134 | sPath = *(++i); |
121 | if( *(++i) != "HTTP/1.1" ) | 135 | if( *(++i) != "HTTP/1.1" ) |
122 | { | 136 | { |
137 | Bu::MutexLocker l( mClient ); | ||
123 | pClient->disconnect(); | 138 | pClient->disconnect(); |
124 | return false; | 139 | return false; |
125 | } | 140 | } |
@@ -147,6 +162,7 @@ bool Bu::ProtocolWebSocket::stateHandshake() | |||
147 | int iPos = sLine.findIdx(':'); | 162 | int iPos = sLine.findIdx(':'); |
148 | if( iPos < 0 ) | 163 | if( iPos < 0 ) |
149 | { | 164 | { |
165 | Bu::MutexLocker l( mClient ); | ||
150 | pClient->disconnect(); | 166 | pClient->disconnect(); |
151 | return false; | 167 | return false; |
152 | } | 168 | } |
@@ -183,6 +199,7 @@ bool Bu::ProtocolWebSocket::readHttpHdrLine( Bu::String &sLine ) | |||
183 | 199 | ||
184 | bool Bu::ProtocolWebSocket::processHeaders() | 200 | bool Bu::ProtocolWebSocket::processHeaders() |
185 | { | 201 | { |
202 | Bu::MutexLocker l( mClient ); | ||
186 | if( !headerMatch("Connection", "Upgrade") || | 203 | if( !headerMatch("Connection", "Upgrade") || |
187 | !headerMatch("Upgrade", "websocket") || | 204 | !headerMatch("Upgrade", "websocket") || |
188 | !headerMatch("Sec-WebSocket-Version", "13") ) | 205 | !headerMatch("Sec-WebSocket-Version", "13") ) |
@@ -209,7 +226,7 @@ bool Bu::ProtocolWebSocket::processHeaders() | |||
209 | sum.writeResult( bOut ); | 226 | sum.writeResult( bOut ); |
210 | bOut.stop(); | 227 | bOut.stop(); |
211 | 228 | ||
212 | Bu::println("accept: %1").arg( mbOut.getString() ); | 229 | // Bu::println("accept: %1").arg( mbOut.getString() ); |
213 | 230 | ||
214 | pClient->write("HTTP/1.1 101 Switching Protocols\r\n" | 231 | pClient->write("HTTP/1.1 101 Switching Protocols\r\n" |
215 | "Upgrade: websocket\r\n" | 232 | "Upgrade: websocket\r\n" |
@@ -290,8 +307,8 @@ bool Bu::ProtocolWebSocket::parseMessage() | |||
290 | } | 307 | } |
291 | } | 308 | } |
292 | 309 | ||
293 | Bu::println(""); | 310 | // Bu::println(""); |
294 | Bu::println("Data: >>%1<<").arg( sData ); | 311 | // Bu::println("Data: >>%1<<").arg( sData ); |
295 | 312 | ||
296 | onNewMessage( sData, eOp ); | 313 | onNewMessage( sData, eOp ); |
297 | 314 | ||
diff --git a/src/unstable/protocolwebsocket.h b/src/unstable/protocolwebsocket.h index cf00d34..4a3d0a1 100644 --- a/src/unstable/protocolwebsocket.h +++ b/src/unstable/protocolwebsocket.h | |||
@@ -11,6 +11,8 @@ | |||
11 | #include "bu/protocol.h" | 11 | #include "bu/protocol.h" |
12 | #include "bu/hash.h" | 12 | #include "bu/hash.h" |
13 | 13 | ||
14 | #include "bu/mutex.h" | ||
15 | |||
14 | namespace Bu | 16 | namespace Bu |
15 | { | 17 | { |
16 | class ProtocolWebSocket : public Bu::Protocol | 18 | class ProtocolWebSocket : public Bu::Protocol |
@@ -58,6 +60,7 @@ namespace Bu | |||
58 | Status eStatus; | 60 | Status eStatus; |
59 | Bu::String sPath; | 61 | Bu::String sPath; |
60 | Bu::Hash<Bu::String, Bu::StringList> hHeader; | 62 | Bu::Hash<Bu::String, Bu::StringList> hHeader; |
63 | Bu::Mutex mClient; | ||
61 | }; | 64 | }; |
62 | } | 65 | } |
63 | 66 | ||
diff --git a/src/unstable/readwritemutex.cpp b/src/unstable/readwritemutex.cpp index 651a343..9719bfb 100644 --- a/src/unstable/readwritemutex.cpp +++ b/src/unstable/readwritemutex.cpp | |||
@@ -51,11 +51,11 @@ void Bu::ReadWriteMutex::unlockRead() | |||
51 | 51 | ||
52 | // | 52 | // |
53 | // The bWantWrite could be a counter like the read lock counter, however | 53 | // The bWantWrite could be a counter like the read lock counter, however |
54 | // once a write lock occurs and bWantWrite is set at least one wite | 54 | // once a write lock occurs and bWantWrite is set at least one write |
55 | // will definately occur. In practice most writes all happen one after | 55 | // will definately occur. In practice most writes all happen one after |
56 | // the other anyway and this way reads get a chance to mingle in. | 56 | // the other anyway and this way reads get a chance to mingle in. |
57 | // | 57 | // |
58 | // Really, just getting all currint reads to stop so a write can happen | 58 | // Really, just getting all current reads to stop so a write can happen |
59 | // I think is sufficient right now. | 59 | // I think is sufficient right now. |
60 | // | 60 | // |
61 | void Bu::ReadWriteMutex::lockWrite() | 61 | void Bu::ReadWriteMutex::lockWrite() |