From 8f3db79f040d620d32eaed11f937196fba4118f4 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Sat, 29 Jun 2019 20:42:54 +0300
Subject: [PATCH] Resolves #122 with handshake fix

---
 components/net/cpp/src/peer.cpp | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/components/net/cpp/src/peer.cpp b/components/net/cpp/src/peer.cpp
index 9b54887ac..ba32248ba 100644
--- a/components/net/cpp/src/peer.cpp
+++ b/components/net/cpp/src/peer.cpp
@@ -465,22 +465,27 @@ bool Peer::_data() {
 					_badClose(false);
 					LOG(ERROR) << "Missing handshake - got '" << get<1>(hs) << "'";
 					return false;
+				} else {
+					// Must handle immediately with no other thread able
+					// to read next message before completion.
+					// The handshake handler must not block.
+					disp_->dispatch(*this, obj);
 				}
 			} catch(...) {
 				_badClose(false);
 				LOG(ERROR) << "Bad first message format";
 				return false;
 			}
+		} else {
+			// CHECK Safe to unlock here?
+			is_waiting_ = true;
+			lk.unlock();
+			disp_->dispatch(*this, obj);
+			// Relock before next loop of while
+			lk.lock();
+			is_waiting_ = false;
 		}
 
-		// CHECK Safe to unlock here?
-		is_waiting_ = true;
-		lk.unlock();
-		disp_->dispatch(*this, obj);
-		// Relock before next loop of while
-		lk.lock();
-		is_waiting_ = false;
-
 		if (scheme_ == ftl::URI::SCHEME_WS && recv_buf_.nonparsed_size() > 0) {
 			wsheader_type ws;
 			if (ws_parse(recv_buf_, ws) < 0) {
-- 
GitLab