From 8ca2528e1271951d485f2f484f5f805c53964a2d Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 9 Apr 2019 12:41:28 +0300
Subject: [PATCH] Fixes for VS compilation and cmake in windows

---
 CMakeLists.txt                   |  1 +
 cmake/Findglog.cmake             |  7 +++++++
 net/cpp/include/ftl/net/peer.hpp | 14 +++++++-------
 net/cpp/src/peer.cpp             | 17 +++++++++--------
 net/cpp/src/universe.cpp         |  5 +++++
 net/cpp/test/peer_unit.cpp       |  4 ++++
 6 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 74ae1598f..969acb22d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,7 @@ enable_testing()
 set(THREADS_PREFER_PTHREAD_FLAG ON)
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
 
+#include(Findglog)
 find_package( glog REQUIRED )
 find_package( OpenCV REQUIRED )
 find_package( Threads REQUIRED )
diff --git a/cmake/Findglog.cmake b/cmake/Findglog.cmake
index b7ea41f92..9079190dc 100644
--- a/cmake/Findglog.cmake
+++ b/cmake/Findglog.cmake
@@ -5,6 +5,13 @@
 if(WIN32)
 find_path(glog_DIR NAMES include/glog/logging.h PATHS "C:/Program Files/glog" "C:/Program Files/google-glog" "C:/Program Files (x86)/google-glog")
 set(glog_DIR ${glog_DIR})
+
+find_package(glog QUIET PATHS "${glog_DIR}/lib/cmake" NO_DEFAULT_PATH)
+
+if (GLOG_FOUND)
+return()
+endif()
+
 else()
 set(glog_DIR "")
 endif()
diff --git a/net/cpp/include/ftl/net/peer.hpp b/net/cpp/include/ftl/net/peer.hpp
index 0de0b4ea2..16cad5138 100644
--- a/net/cpp/include/ftl/net/peer.hpp
+++ b/net/cpp/include/ftl/net/peer.hpp
@@ -1,13 +1,6 @@
 #ifndef _FTL_NET_PEER_HPP_
 #define _FTL_NET_PEER_HPP_
 
-#define GLOG_NO_ABBREVIATED_SEVERITIES
-#include <glog/logging.h>
-#include <ftl/net/protocol.hpp>
-#include <ftl/net/dispatcher.hpp>
-#include <ftl/uri.hpp>
-#include <ftl/uuid.hpp>
-
 #ifndef WIN32
 #define INVALID_SOCKET -1
 #include <netinet/in.h>
@@ -15,6 +8,13 @@
 #include <winsock2.h>
 #endif
 
+#define GLOG_NO_ABBREVIATED_SEVERITIES
+#include <glog/logging.h>
+#include <ftl/net/protocol.hpp>
+#include <ftl/net/dispatcher.hpp>
+#include <ftl/uri.hpp>
+#include <ftl/uuid.hpp>
+
 #include <iostream>
 #include <sstream>
 #include <tuple>
diff --git a/net/cpp/src/peer.cpp b/net/cpp/src/peer.cpp
index b01ac81d9..868aa168d 100644
--- a/net/cpp/src/peer.cpp
+++ b/net/cpp/src/peer.cpp
@@ -2,6 +2,11 @@
 #include <glog/logging.h>
 
 #include <fcntl.h>
+#ifdef WIN32
+#include <winsock2.h>
+#include <Ws2tcpip.h>
+#include <windows.h>
+#endif
 
 #include <ftl/uri.hpp>
 #include <ftl/net/peer.hpp>
@@ -20,12 +25,6 @@
 #define SOCKET_ERROR -1
 #endif
 
-#ifdef WIN32
-#include <winsock2.h>
-#include <Ws2tcpip.h>
-#include <windows.h>
-#endif
-
 #include <iostream>
 #include <memory>
 #include <algorithm>
@@ -502,9 +501,11 @@ int Peer::_send() {
 	
 #ifdef WIN32
 	// TODO(nick) Use WSASend instead as equivalent to writev
+	auto send_vec = send_buf_.vector();
+	auto send_size = send_buf_.vector_size();
 	int c = 0;
-	for (auto v : send_vec_) {
-		c += ftl::net::internal::send(sock_, (char*)v.iov_base, v.iov_len, 0);
+	for (int i = 0; i < send_size; i++) {
+		c += ftl::net::internal::send(sock_, (char*)send_vec[i].iov_base, send_vec[i].iov_len, 0);
 	}
 #else
 	int c = ftl::net::internal::writev(sock_, send_buf_.vector(), send_buf_.vector_size());
diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp
index 2e4659a7e..ea8f3159f 100644
--- a/net/cpp/src/universe.cpp
+++ b/net/cpp/src/universe.cpp
@@ -1,5 +1,10 @@
 #include <ftl/net/universe.hpp>
 
+#ifdef WIN32
+#include <Ws2tcpip.h>
+#pragma comment(lib, "Rpcrt4.lib")
+#endif
+
 using std::string;
 using std::vector;
 using std::thread;
diff --git a/net/cpp/test/peer_unit.cpp b/net/cpp/test/peer_unit.cpp
index a6507d615..32dddd01e 100644
--- a/net/cpp/test/peer_unit.cpp
+++ b/net/cpp/test/peer_unit.cpp
@@ -8,6 +8,10 @@
 #include <ftl/net/protocol.hpp>
 #include <ftl/config.h>
 
+#ifdef WIN32
+#pragma comment(lib, "Rpcrt4.lib")
+#endif
+
 /* Allow socket functions to be mocked */
 #define TEST_MOCKS
 #include "../src/net_internal.hpp"
-- 
GitLab