From e54cdf5c48b8edbad6729c39ad4daf2cb301b87c Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 15 Jun 2019 16:55:33 +0300
Subject: [PATCH] Code tidy, still has bug on close

---
 components/net/cpp/include/ftl/net/peer.hpp |  4 ++--
 components/net/cpp/src/peer.cpp             | 18 +++++++++++-------
 components/rgbd-sources/src/net.cpp         |  2 --
 components/rgbd-sources/src/streamer.cpp    |  2 +-
 4 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/components/net/cpp/include/ftl/net/peer.hpp b/components/net/cpp/include/ftl/net/peer.hpp
index 605c0fb0d..16393a55c 100644
--- a/components/net/cpp/include/ftl/net/peer.hpp
+++ b/components/net/cpp/include/ftl/net/peer.hpp
@@ -320,8 +320,6 @@ int Peer::asyncCall(
 		ARGS... args) {
 	auto args_obj = std::make_tuple(args...);
 	auto rpcid = 0;
-	
-	LOG(INFO) << "RPC " << name << "() -> " << uri_;
 
 	{
 		// Could this be the problem????
@@ -331,6 +329,8 @@ int Peer::asyncCall(
 		callbacks_[rpcid] = std::make_unique<caller<T>>(cb);
 	}
 
+	LOG(INFO) << "RPC " << name << "(" << rpcid << ") -> " << uri_;
+
 	auto call_obj = std::make_tuple(0,rpcid,name,args_obj);
 	
 	UNIQUE_LOCK(send_mtx_,lk);
diff --git a/components/net/cpp/src/peer.cpp b/components/net/cpp/src/peer.cpp
index 04beff81f..f3f875ae8 100644
--- a/components/net/cpp/src/peer.cpp
+++ b/components/net/cpp/src/peer.cpp
@@ -409,9 +409,11 @@ void Peer::data() {
 	}
 	if (cap < (kMaxMessage / 10)) LOG(WARNING) << "NO BUFFER";
 
-	if (rc <= 0) {
-		//LOG(WARNING) << "Weird rc: " << rc;
-		//close();
+	if (rc == 0) {
+		close();
+		return;
+	} else if (rc < 0) {
+		socketError();
 		return;
 	}
 	
@@ -427,7 +429,7 @@ void Peer::data() {
 	}
 	lk.unlock();
 
-	LOG(INFO) << "Received " << rc << " bytes";
+	//LOG(INFO) << "Received " << rc << " bytes";
 }
 
 bool Peer::_data() {
@@ -443,8 +445,6 @@ bool Peer::_data() {
 
 	msgpack::object_handle msg;
 	while (recv_buf_.next(msg)) {
-		// CHECK Safe to unlock here?
-		lk.unlock();
 		ws_read_header_ = false;
 		msgpack::object obj = msg.get();
 		if (status_ != kConnected) {
@@ -464,10 +464,14 @@ bool Peer::_data() {
 				return false;
 			}
 		}
-		disp_->dispatch(*this, obj);
 
+		// 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;
diff --git a/components/rgbd-sources/src/net.cpp b/components/rgbd-sources/src/net.cpp
index c23ec9376..ef3fdd5d2 100644
--- a/components/rgbd-sources/src/net.cpp
+++ b/components/rgbd-sources/src/net.cpp
@@ -64,8 +64,6 @@ NetSource::~NetSource() {
 void NetSource::_recv(const vector<unsigned char> &jpg, const vector<unsigned char> &d) {
 	cv::Mat tmp_rgb, tmp_depth;
 
-	DLOG(INFO) << "Received frame";
-
 	// Decode in temporary buffers to prevent long locks
 	cv::imdecode(jpg, cv::IMREAD_COLOR, &tmp_rgb);
 	cv::imdecode(d, cv::IMREAD_UNCHANGED, &tmp_depth);
diff --git a/components/rgbd-sources/src/streamer.cpp b/components/rgbd-sources/src/streamer.cpp
index 651434ee0..9b90cd4b8 100644
--- a/components/rgbd-sources/src/streamer.cpp
+++ b/components/rgbd-sources/src/streamer.cpp
@@ -170,7 +170,7 @@ void Streamer::poll() {
 	if (elapsed.count() >= wait) {
 		LOG(WARNING) << "Frame rate below optimal @ " << (1.0f / elapsed.count());
 	} else {
-		LOG(INFO) << "Frame rate @ " << (1.0f / elapsed.count());
+		//LOG(INFO) << "Frame rate @ " << (1.0f / elapsed.count());
 		// Otherwise, wait until next frame should start.
 		// CHECK(Nick) Is this accurate enough? Almost certainly not
 		// TODO(Nick) Synchronise by time corrections and use of fixed time points
-- 
GitLab