From 1cb0fc6ab3f05e37f8c4c0bf5549b320c8b89078 Mon Sep 17 00:00:00 2001
From: Mike Buland <mbuland@penny-arcade.com>
Date: Mon, 29 Jan 2018 00:47:50 -0800
Subject: 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.
---
 src/unstable/protocolwebsocket.cpp | 23 ++++++++++++++++++++---
 src/unstable/protocolwebsocket.h   |  3 +++
 src/unstable/readwritemutex.cpp    |  4 ++--
 3 files changed, 25 insertions(+), 5 deletions(-)

(limited to 'src/unstable')

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 @@
 #include "bu/protocolwebsocket.h"
 
 #include "bu/sio.h"
+#include "bu/fmt.h"
 #include "bu/client.h"
 
 #include "bu/membuf.h"
@@ -16,6 +17,8 @@
 #include "bu/sha1.h"
 #include "bu/json.h"
 
+#include "bu/mutexlocker.h"
+
 #include <stdlib.h>
 
 Bu::ProtocolWebSocket::ProtocolWebSocket() :
@@ -25,11 +28,17 @@ Bu::ProtocolWebSocket::ProtocolWebSocket() :
 
 Bu::ProtocolWebSocket::~ProtocolWebSocket()
 {
+    mClient.lock();
+    printf("ProtocolWebSocket::~ProtocolWebSocket(): Clearing pClient (%p)\n", (void *)this->pClient );
+    this->pClient = NULL;
+    mClient.unlock();
 }
 
 void Bu::ProtocolWebSocket::onNewConnection( Bu::Client *pClient )
 {
+    mClient.lock();
     this->pClient = pClient;
+    mClient.unlock();
 }
 
 void Bu::ProtocolWebSocket::onNewData( Bu::Client * /*pClient*/ )
@@ -95,6 +104,9 @@ void Bu::ProtocolWebSocket::writeMessage( const Bu::String &sData,
     }
     Bu::println("");
 */
+    Bu::MutexLocker l( mClient );
+    if( pClient == NULL )
+        return;
     pClient->write( cHeader, idx );
     pClient->write( sData );
 }
@@ -108,18 +120,21 @@ bool Bu::ProtocolWebSocket::stateProtoId()
     Bu::StringList lChunks = sLine.split(' ');
     if( lChunks.getSize() != 3 )
     {
+        Bu::MutexLocker l( mClient );
         pClient->disconnect();
         return false;
     }
     Bu::StringList::iterator i = lChunks.begin();
     if( *i != "GET" )
     {
+        Bu::MutexLocker l( mClient );
         pClient->disconnect();
         return false;
     }
     sPath = *(++i);
     if( *(++i) != "HTTP/1.1" )
     {
+        Bu::MutexLocker l( mClient );
         pClient->disconnect();
         return false;
     }
@@ -147,6 +162,7 @@ bool Bu::ProtocolWebSocket::stateHandshake()
     int iPos = sLine.findIdx(':');
     if( iPos < 0 )
     {
+        Bu::MutexLocker l( mClient );
         pClient->disconnect();
         return false;
     }
@@ -183,6 +199,7 @@ bool Bu::ProtocolWebSocket::readHttpHdrLine( Bu::String &sLine )
 
 bool Bu::ProtocolWebSocket::processHeaders()
 {
+    Bu::MutexLocker l( mClient );
     if( !headerMatch("Connection", "Upgrade") ||
         !headerMatch("Upgrade", "websocket") ||
         !headerMatch("Sec-WebSocket-Version", "13") )
@@ -209,7 +226,7 @@ bool Bu::ProtocolWebSocket::processHeaders()
     sum.writeResult( bOut );
     bOut.stop();
 
-    Bu::println("accept: %1").arg( mbOut.getString() );
+//    Bu::println("accept: %1").arg( mbOut.getString() );
 
     pClient->write("HTTP/1.1 101 Switching Protocols\r\n"
         "Upgrade: websocket\r\n"
@@ -290,8 +307,8 @@ bool Bu::ProtocolWebSocket::parseMessage()
         }
     }
 
-    Bu::println("");
-    Bu::println("Data: >>%1<<").arg( sData );
+//    Bu::println("");
+//    Bu::println("Data: >>%1<<").arg( sData );
 
     onNewMessage( sData, eOp );
 
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 @@
 #include "bu/protocol.h"
 #include "bu/hash.h"
 
+#include "bu/mutex.h"
+
 namespace Bu
 {
     class ProtocolWebSocket : public Bu::Protocol
@@ -58,6 +60,7 @@ namespace Bu
         Status eStatus;
         Bu::String sPath;
         Bu::Hash<Bu::String, Bu::StringList> hHeader;
+        Bu::Mutex mClient;
     };
 }
 
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()
 
 //
 // The bWantWrite could be a counter like the read lock counter, however
-// once a write lock occurs and bWantWrite is set at least one wite
+// once a write lock occurs and bWantWrite is set at least one write
 // will definately occur.  In practice most writes all happen one after
 // the other anyway and this way reads get a chance to mingle in.
 //
-// Really, just getting all currint reads to stop so a write can happen
+// Really, just getting all current reads to stop so a write can happen
 // I think is sufficient right now.
 //
 void Bu::ReadWriteMutex::lockWrite()
-- 
cgit v1.2.3