aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/protocolwebsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/protocolwebsocket.cpp')
-rw-r--r--src/unstable/protocolwebsocket.cpp23
1 files changed, 20 insertions, 3 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