From fc35a765be54552cec9352d70eb2999120015e89 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 1 Apr 2019 15:22:32 +0300
Subject: [PATCH] Get msgpack working in Windows and fix network code to
 compile in windows.

---
 CMakeLists.txt                       | 10 +++++++++-
 net/cpp/include/ftl/net/listener.hpp |  2 +-
 net/cpp/include/ftl/net/socket.hpp   |  4 +++-
 net/cpp/src/dispatcher.cpp           |  1 +
 net/cpp/src/listener.cpp             |  4 +++-
 net/cpp/src/net.cpp                  |  5 +++++
 net/cpp/src/protocol.cpp             |  1 +
 net/cpp/src/socket.cpp               | 21 +++++++++++++++++----
 8 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bc2818910..eca831d06 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,7 +16,7 @@ find_package( glog REQUIRED )
 find_package( OpenCV REQUIRED )
 find_package( Threads REQUIRED )
 find_package( URIParser REQUIRED )
-find_package( MsgPack )
+find_package( MsgPack REQUIRED )
 find_package( LibSGM )
 
 check_language(CUDA)
@@ -35,6 +35,14 @@ if (NOT MsgPack_FOUND)
 		message(ERROR "Msgpack is required")
 	endif()
 else()
+	if(WIN32)
+		# Find include
+		find_path(MSGPACK_INCLUDE_DIRS
+		    NAMES msgpack.hpp
+		    PATHS "C:/Program Files/msgpack" "C:/Program Files (x86)/msgpack"
+		    PATH_SUFFIXES include
+		)
+	endif()
 	include_directories(${MSGPACK_INCLUDE_DIRS})
 endif()
 
diff --git a/net/cpp/include/ftl/net/listener.hpp b/net/cpp/include/ftl/net/listener.hpp
index 4d86df5c2..09c1f68f0 100644
--- a/net/cpp/include/ftl/net/listener.hpp
+++ b/net/cpp/include/ftl/net/listener.hpp
@@ -7,7 +7,7 @@
 
 #ifdef WIN32
 //#include <windows.h>
-#include <winsock.h>
+#include <winsock2.h>
 #endif
 
 #include <ftl/net/handlers.hpp>
diff --git a/net/cpp/include/ftl/net/socket.hpp b/net/cpp/include/ftl/net/socket.hpp
index f87f4c782..a1c062a37 100644
--- a/net/cpp/include/ftl/net/socket.hpp
+++ b/net/cpp/include/ftl/net/socket.hpp
@@ -1,6 +1,7 @@
 #ifndef _FTL_NET_SOCKET_HPP_
 #define _FTL_NET_SOCKET_HPP_
 
+#define GLOG_NO_ABBREVIATED_SEVERITIES
 #include <glog/logging.h>
 #include <ftl/net.hpp>
 #include <ftl/net/protocol.hpp>
@@ -12,7 +13,8 @@
 
 #ifdef WIN32
 //#include <windows.h>
-#include <winsock.h>
+//#include <winsock.h>
+#include <winsock2.h>
 #endif
 
 #include <iostream>
diff --git a/net/cpp/src/dispatcher.cpp b/net/cpp/src/dispatcher.cpp
index 32239bd2b..0d95a2a55 100644
--- a/net/cpp/src/dispatcher.cpp
+++ b/net/cpp/src/dispatcher.cpp
@@ -1,3 +1,4 @@
+#define GLOG_NO_ABBREVIATED_SEVERITIES
 #include <glog/logging.h>
 #include <ftl/net/dispatcher.hpp>
 #include <ftl/net/socket.hpp>
diff --git a/net/cpp/src/listener.cpp b/net/cpp/src/listener.cpp
index 0338db14a..dcfe31bf0 100644
--- a/net/cpp/src/listener.cpp
+++ b/net/cpp/src/listener.cpp
@@ -1,3 +1,4 @@
+#define GLOG_NO_ABBREVIATED_SEVERITIES
 #include <glog/logging.h>
 
 #include <ftl/uri.hpp>
@@ -27,6 +28,7 @@ typedef int socklen_t;
 using namespace ftl;
 using ftl::net::Listener;
 using std::shared_ptr;
+using ftl::net::Socket;
 
 int tcpListen(URI &uri) {
 	int ssock;
@@ -45,7 +47,7 @@ int tcpListen(URI &uri) {
 	}
 	
 	int enable = 1;
-	if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
+	if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, (char*)(&enable), sizeof(int)) < 0)
 		LOG(ERROR) << "setsockopt(SO_REUSEADDR) failed";
 
 	//Specify listen port and address
diff --git a/net/cpp/src/net.cpp b/net/cpp/src/net.cpp
index 478f336ee..3ea57c1c4 100644
--- a/net/cpp/src/net.cpp
+++ b/net/cpp/src/net.cpp
@@ -2,6 +2,10 @@
 #include <ftl/net/listener.hpp>
 #include <ftl/net/socket.hpp>
 
+#ifdef WIN32
+#include <Ws2tcpip.h>
+#endif
+
 #include <vector>
 #include <iostream>
 #include <chrono>
@@ -135,6 +139,7 @@ bool _run(bool blocking, bool nodelay) {
 				if (FD_ISSET(l->_socket(), &sfdread)) {
 					int rsize = sizeof(sockaddr_storage);
 					sockaddr_storage addr;
+
 					//int freeclient = freeSocket();
 
 					//if (freeclient >= 0) {
diff --git a/net/cpp/src/protocol.cpp b/net/cpp/src/protocol.cpp
index 125477376..27f1146d6 100644
--- a/net/cpp/src/protocol.cpp
+++ b/net/cpp/src/protocol.cpp
@@ -1,3 +1,4 @@
+#define GLOG_NO_ABBREVIATED_SEVERITIES
 #include <glog/logging.h>
 #include <ftl/net/socket.hpp>
 #include <ftl/net/protocol.hpp>
diff --git a/net/cpp/src/socket.cpp b/net/cpp/src/socket.cpp
index 1d1cadbf3..a992cf055 100644
--- a/net/cpp/src/socket.cpp
+++ b/net/cpp/src/socket.cpp
@@ -1,3 +1,4 @@
+#define GLOG_NO_ABBREVIATED_SEVERITIES
 #include <glog/logging.h>
 
 #include <ftl/uri.hpp>
@@ -16,9 +17,8 @@
 
 #ifdef WIN32
 #include <windows.h>
-#include <winsock.h>
-typedef int socklen_t;
-#define MSG_WAITALL 0
+#include <winsock2.h>
+#include <Ws2tcpip.h>
 #endif
 
 #include <iostream>
@@ -27,6 +27,7 @@ typedef int socklen_t;
 
 using namespace ftl;
 using ftl::net::Socket;
+using ftl::net::Protocol;
 using namespace std;
 
 /*static std::string hexStr(const std::string &s)
@@ -233,8 +234,12 @@ void Socket::setProtocol(Protocol *p) {
 
 void Socket::error() {
 	int err;
+#ifdef WIN32
+	int optlen = sizeof(err);
+#else
 	uint32_t optlen = sizeof(err);
-	getsockopt(sock_, SOL_SOCKET, SO_ERROR, &err, &optlen);
+#endif
+	getsockopt(sock_, SOL_SOCKET, SO_ERROR, (char*)&err, &optlen);
 	LOG(ERROR) << "Socket: " << uri_ << " - error " << err;
 }
 
@@ -390,7 +395,15 @@ void Socket::_connected() {
 }
 
 int Socket::_send() {
+#ifdef WIN32
+	// TODO(nick) Use WSASend instead
+	int c = 0;
+	for (auto v : send_vec_) {
+		c += ::send(sock_, (char*)v.iov_base, v.iov_len, 0);
+	}
+#else
 	int c = ::writev(sock_, send_vec_.data(), send_vec_.size());
+#endif
 	send_vec_.clear();
 	return c;
 }
-- 
GitLab