From d78c5f4074749fd4b935be6d7e47d6bc6be16989 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 4 Apr 2019 08:43:08 +0300
Subject: [PATCH] Minor code tidy

---
 cv-node/src/main.cpp   | 27 +++++++++++----------------
 net/cpp/src/socket.cpp | 20 +++++++++++++-------
 2 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/cv-node/src/main.cpp b/cv-node/src/main.cpp
index 0b664073c..76efb5973 100644
--- a/cv-node/src/main.cpp
+++ b/cv-node/src/main.cpp
@@ -1,5 +1,7 @@
 /*
- * Copyright 2019 Nicolas Pope
+ * Copyright 2019 Nicolas Pope. All rights reserved.
+ *
+ * See LICENSE.
  */
 
 #include <glog/logging.h>
@@ -45,18 +47,11 @@ static json config;
 static bool findConfiguration(const string &file) {
 	ifstream i;
 	
-	if (file != "") {
-		i.open(file);
-	}
-	if (!i.is_open()) {
-		i.open("./config.json");
-	}
-	if (!i.is_open()) {
-		i.open(FTL_LOCAL_CONFIG_ROOT "/config.json");
-	}
-	if (!i.is_open()) {
-		i.open(FTL_GLOBAL_CONFIG_ROOT "/config.json");
-	}
+	if (file != "") i.open(file);
+	if (!i.is_open()) i.open("./config.json");
+	if (!i.is_open()) i.open(FTL_LOCAL_CONFIG_ROOT "/config.json");
+	if (!i.is_open()) i.open(FTL_GLOBAL_CONFIG_ROOT "/config.json");
+	if (!i.is_open()) return false;
 	i >> config;
 	return true;
 }
@@ -140,7 +135,7 @@ static void run(const string &file) {
     // Choose and configure disparity algorithm
     auto disparity = Disparity::create(config["disparity"]);
 
-	Mat l, r, disparity32F, depth32F, lbw, rbw;
+	Mat l, r, disparity;
 
 	Display display(calibrate, config["display"]);
 
@@ -156,10 +151,10 @@ static void run(const string &file) {
 		sync->get(ftl::LEFT, l);
 		sync->get(ftl::RIGHT, r);
 
-        disparity->compute(l, r, disparity32F);
+        disparity->compute(l, r, disparity);
 
 		// Send RGB+Depth images for local rendering
-		display.render(l, disparity32F);
+		display.render(l, disparity);
 
 		// streamer.send(l, disparity32F);
 	}
diff --git a/net/cpp/src/socket.cpp b/net/cpp/src/socket.cpp
index 3e4d18220..61ab82d72 100644
--- a/net/cpp/src/socket.cpp
+++ b/net/cpp/src/socket.cpp
@@ -49,16 +49,15 @@ using namespace std;
 
 int Socket::rpcid__ = 0;
 
+// TODO(nick) Move to tcp_internal.cpp
 static int tcpConnect(URI &uri) {
 	int rc;
 	sockaddr_in destAddr;
 
-	//std::cerr << "TCP Connect: " << uri.getHost() << " : " << uri.getPort() << std::endl;
-
 	#ifdef WIN32
 	WSAData wsaData;
 	if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
-		//ERROR
+		LOG(ERROR) << "Could not initiate sockets";
 		return INVALID_SOCKET;
 	}
 	#endif
@@ -67,6 +66,7 @@ static int tcpConnect(URI &uri) {
 	int csocket = socket(AF_INET, SOCK_STREAM, 0);
 
 	if (csocket == INVALID_SOCKET) {
+		LOG(ERROR) << "Unable to create TCP socket";
 		return INVALID_SOCKET;
 	}
 
@@ -84,7 +84,6 @@ static int tcpConnect(URI &uri) {
 		#endif
 
 		LOG(ERROR) << "Address not found : " << uri.getHost() << std::endl;
-
 		return INVALID_SOCKET;
 	}
 
@@ -119,8 +118,6 @@ static int tcpConnect(URI &uri) {
 	/*rg = fcntl(csocket, F_GETFL, NULL));
 	arg &= (~O_NONBLOCK);
 	fcntl(csocket, F_SETFL, arg) < 0)*/
-	
-	// Handshake??
 
 	return csocket;
 }
@@ -386,6 +383,7 @@ void Socket::_dispatchReturn(const std::string &d) {
 	Dispatcher::response_t the_result;
 	unpacked.get().convert(the_result);
 
+	// Msgpack stipulates that 1 means return message
 	if (std::get<0>(the_result) != 1) {
 		LOG(ERROR) << "Bad RPC return message";
 		return;
@@ -399,6 +397,8 @@ void Socket::_dispatchReturn(const std::string &d) {
 	
 	if (callbacks_.count(id) > 0) {
 		LOG(INFO) << "Received return RPC value";
+		
+		// Call the callback with unpacked return value
 		(*callbacks_[id])(res);
 		callbacks_.erase(id);
 	} else {
@@ -428,17 +428,23 @@ int Socket::_send() {
 		// Create a websocket header as well.
 		size_t len = 0;
 		char buf[20];  // TODO(nick) Should not be a stack buffer.
+		
+		// Calculate total size of message
 		for (auto v : send_vec_) {
 			len += v.iov_len;
 		}
+		
+		// Pack correct websocket header into buffer
 		int rc = ws_prepare(wsheader_type::BINARY_FRAME, false, len, buf, 20);
 		if (rc == -1) return -1;
+		
+		// Patch the first io vector to be ws header
 		send_vec_[0].iov_base = buf;
 		send_vec_[0].iov_len = rc;
 	}
 	
 #ifdef WIN32
-	// TODO(nick) Use WSASend instead
+	// TODO(nick) Use WSASend instead as equivalent to writev
 	int c = 0;
 	for (auto v : send_vec_) {
 		c += ftl::net::internal::send(sock_, (char*)v.iov_base, v.iov_len, 0);
-- 
GitLab