summaryrefslogtreecommitdiff
path: root/src/unstable
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable')
-rw-r--r--src/unstable/protocolwebsocket.cpp23
-rw-r--r--src/unstable/protocolwebsocket.h3
-rw-r--r--src/unstable/readwritemutex.cpp4
3 files changed, 25 insertions, 5 deletions
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
21Bu::ProtocolWebSocket::ProtocolWebSocket() : 24Bu::ProtocolWebSocket::ProtocolWebSocket() :
@@ -25,11 +28,17 @@ Bu::ProtocolWebSocket::ProtocolWebSocket() :
25 28
26Bu::ProtocolWebSocket::~ProtocolWebSocket() 29Bu::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
30void Bu::ProtocolWebSocket::onNewConnection( Bu::Client *pClient ) 37void 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
35void Bu::ProtocolWebSocket::onNewData( Bu::Client * /*pClient*/ ) 44void 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
184bool Bu::ProtocolWebSocket::processHeaders() 200bool 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
14namespace Bu 16namespace 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//
61void Bu::ReadWriteMutex::lockWrite() 61void Bu::ReadWriteMutex::lockWrite()