From b2cb93cb500316952e5bcb986c274a33cd952b41 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Sun, 25 Mar 2018 20:10:57 +0200
Subject: [PATCH] adjust for macos

---
 3d_party/rapidjson/include/rapidjson/reader.h |  6 +-
 common/cpp/include/common/error.h             | 10 ++-
 common/cpp/include/system_wrappers/io.h       | 82 +++++++++++++++----
 .../cpp/include/system_wrappers/system_io.h   |  2 +-
 common/cpp/include/unittests/MockIO.h         |  2 +-
 common/cpp/src/system_io/system_io.cpp        |  9 +-
 common/cpp/src/system_io/system_io_linux.cpp  | 12 ++-
 .../cpp/src/system_io/system_io_windows.cpp   |  2 +-
 producer/api/src/producer_impl.cpp            |  2 +-
 receiver/src/receiver_error.h                 |  4 +-
 10 files changed, 95 insertions(+), 36 deletions(-)

diff --git a/3d_party/rapidjson/include/rapidjson/reader.h b/3d_party/rapidjson/include/rapidjson/reader.h
index 206b26141..9b28b2685 100644
--- a/3d_party/rapidjson/include/rapidjson/reader.h
+++ b/3d_party/rapidjson/include/rapidjson/reader.h
@@ -1223,8 +1223,7 @@ class GenericReader {
                     }
                     i = i * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                }
-            else
+                } else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295
                         if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) {
@@ -1265,8 +1264,7 @@ class GenericReader {
                         }
                     i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                }
-            else
+                } else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615
                         if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) {
diff --git a/common/cpp/include/common/error.h b/common/cpp/include/common/error.h
index 11a984176..d3245c683 100644
--- a/common/cpp/include/common/error.h
+++ b/common/cpp/include/common/error.h
@@ -113,7 +113,7 @@ class SimpleErrorTemplate : public ErrorTemplateInterface {
 
     }
 
-    virtual std::string Text() const noexcept {
+    virtual std::string Text() const noexcept override {
         return error_;
     }
 
@@ -144,8 +144,12 @@ inline Error TextErrorWithType(const std::string& error, ErrorType error_type) {
 }
 
 namespace ErrorTemplates {
-auto const kMemoryAllocationError = SimpleErrorTemplate{"kMemoryAllocationError", ErrorType::kMemoryAllocationError};
-auto const kEndOfFile = SimpleErrorTemplate{"End of file", ErrorType::kEndOfFile};
+auto const kMemoryAllocationError = SimpleErrorTemplate {
+                                        "kMemoryAllocationError", ErrorType::kMemoryAllocationError
+                                    };
+auto const kEndOfFile = SimpleErrorTemplate {
+                            "End of file", ErrorType::kEndOfFile
+                        };
 
 }
 
diff --git a/common/cpp/include/system_wrappers/io.h b/common/cpp/include/system_wrappers/io.h
index 39dd23ccb..ebb62dfa5 100644
--- a/common/cpp/include/system_wrappers/io.h
+++ b/common/cpp/include/system_wrappers/io.h
@@ -32,6 +32,9 @@ enum class IOErrorType {
     kSocketOperationOnNonSocket,
     kInvalidMemoryAddress,
     kUnableToResolveHostname,
+    kSocketOperationUnknownAtLevel,
+    kSocketOperationValueOutOfBound
+
 };
 
 class IOError : public SimpleError {
@@ -75,24 +78,67 @@ static inline std::ostream& operator<<(std::ostream& os, const IOErrorTemplate&
 
 
 namespace IOErrorTemplates {
-auto const kUnknownIOError = IOErrorTemplate{"Unknown Error", IOErrorType::kUnknownIOError};
-
-auto const kFileNotFound = IOErrorTemplate{"No such file or directory", IOErrorType::kFileNotFound};
-auto const kReadError = IOErrorTemplate{"Read error", IOErrorType::kReadError};
-auto const kBadFileNumber = IOErrorTemplate{"Bad file number", IOErrorType::kBadFileNumber};
-auto const kResourceTemporarilyUnavailable = IOErrorTemplate{"Resource temporarily unavailable", IOErrorType::kResourceTemporarilyUnavailable};
-auto const kPermissionDenied = IOErrorTemplate{"Permission denied", IOErrorType::kPermissionDenied};
-auto const kUnsupportedAddressFamily = IOErrorTemplate{"Unsupported address family", IOErrorType::kUnsupportedAddressFamily};
-auto const kInvalidAddressFormat = IOErrorTemplate{"Invalid address format", IOErrorType::kInvalidAddressFormat};
-auto const kAddressAlreadyInUse = IOErrorTemplate{"Address already in use", IOErrorType::kAddressAlreadyInUse};
-auto const kConnectionRefused = IOErrorTemplate{"Connection refused", IOErrorType::kConnectionRefused};
-auto const kConnectionResetByPeer = IOErrorTemplate{"kConnectionResetByPeer", IOErrorType::kConnectionResetByPeer};
-auto const kTimeout = IOErrorTemplate{"kTimeout", IOErrorType::kTimeout};
-auto const kFileAlreadyExists = IOErrorTemplate{"kFileAlreadyExists", IOErrorType::kFileAlreadyExists};
-auto const kNoSpaceLeft = IOErrorTemplate{"kNoSpaceLeft", IOErrorType::kNoSpaceLeft};
-auto const kSocketOperationOnNonSocket = IOErrorTemplate{"kSocketOperationOnNonSocket", IOErrorType::kSocketOperationOnNonSocket};
-auto const kInvalidMemoryAddress = IOErrorTemplate{"kInvalidMemoryAddress", IOErrorType::kInvalidMemoryAddress};
-auto const kUnableToResolveHostname = IOErrorTemplate{"kUnableToResolveHostname", IOErrorType::kUnableToResolveHostname};
+auto const kUnknownIOError = IOErrorTemplate {
+                                 "Unknown Error", IOErrorType::kUnknownIOError
+                             };
+
+auto const kFileNotFound = IOErrorTemplate {
+                               "No such file or directory", IOErrorType::kFileNotFound
+                           };
+auto const kReadError = IOErrorTemplate {
+                            "Read error", IOErrorType::kReadError
+                        };
+auto const kBadFileNumber = IOErrorTemplate {
+                                "Bad file number", IOErrorType::kBadFileNumber
+                            };
+auto const kResourceTemporarilyUnavailable = IOErrorTemplate {
+                                                 "Resource temporarily unavailable", IOErrorType::kResourceTemporarilyUnavailable
+                                             };
+auto const kPermissionDenied = IOErrorTemplate {
+                                   "Permission denied", IOErrorType::kPermissionDenied
+                               };
+auto const kUnsupportedAddressFamily = IOErrorTemplate {
+                                           "Unsupported address family", IOErrorType::kUnsupportedAddressFamily
+                                       };
+auto const kInvalidAddressFormat = IOErrorTemplate {
+                                       "Invalid address format", IOErrorType::kInvalidAddressFormat
+                                   };
+auto const kAddressAlreadyInUse = IOErrorTemplate {
+                                      "Address already in use", IOErrorType::kAddressAlreadyInUse
+                                  };
+auto const kConnectionRefused = IOErrorTemplate {
+                                    "Connection refused", IOErrorType::kConnectionRefused
+                                };
+auto const kConnectionResetByPeer = IOErrorTemplate {
+                                        "kConnectionResetByPeer", IOErrorType::kConnectionResetByPeer
+                                    };
+auto const kTimeout = IOErrorTemplate {
+                          "kTimeout", IOErrorType::kTimeout
+                      };
+auto const kFileAlreadyExists = IOErrorTemplate {
+                                    "kFileAlreadyExists", IOErrorType::kFileAlreadyExists
+                                };
+auto const kNoSpaceLeft = IOErrorTemplate {
+                              "kNoSpaceLeft", IOErrorType::kNoSpaceLeft
+                          };
+auto const kSocketOperationOnNonSocket = IOErrorTemplate {
+                                             "kSocketOperationOnNonSocket", IOErrorType::kSocketOperationOnNonSocket
+                                         };
+auto const kInvalidMemoryAddress = IOErrorTemplate {
+                                       "kInvalidMemoryAddress", IOErrorType::kInvalidMemoryAddress
+                                   };
+auto const kUnableToResolveHostname = IOErrorTemplate {
+                                          "kUnableToResolveHostname", IOErrorType::kUnableToResolveHostname
+                                      };
+auto const kSocketOperationUnknownAtLevel =  IOErrorTemplate {
+                                                 "kSocketOperationUnknownAtLevel", IOErrorType::kSocketOperationUnknownAtLevel
+                                             };
+
+auto const kSocketOperationValueOutOfBound =  IOErrorTemplate {
+                                                  "kSocketOperationValueOutOfBound", IOErrorType::kSocketOperationValueOutOfBound
+                                              };
+
+
 }
 
 //Need to be "enum" since multiple flags are allowed
diff --git a/common/cpp/include/system_wrappers/system_io.h b/common/cpp/include/system_wrappers/system_io.h
index c979c7e5e..52b7332bb 100644
--- a/common/cpp/include/system_wrappers/system_io.h
+++ b/common/cpp/include/system_wrappers/system_io.h
@@ -11,7 +11,7 @@
 typedef SSIZE_T ssize_t;
 #endif
 
-#ifdef __linux__
+#if defined(__linux__) || defined (__APPLE__)
 #include <netinet/in.h>
 #endif
 
diff --git a/common/cpp/include/unittests/MockIO.h b/common/cpp/include/unittests/MockIO.h
index cdd267c79..1ce54ffed 100644
--- a/common/cpp/include/unittests/MockIO.h
+++ b/common/cpp/include/unittests/MockIO.h
@@ -49,7 +49,7 @@ class MockIO : public IO {
 
 
     std::unique_ptr<std::tuple<std::string, SocketDescriptor>> InetAcceptConnection(SocketDescriptor socket_fd,
-    Error* err) const {
+    Error* err) const override {
         ErrorInterface* error = nullptr;
         auto data = InetAcceptConnection_t(socket_fd, &error);
         err->reset(error);
diff --git a/common/cpp/src/system_io/system_io.cpp b/common/cpp/src/system_io/system_io.cpp
index 0a8106a5a..5b023f3ec 100644
--- a/common/cpp/src/system_io/system_io.cpp
+++ b/common/cpp/src/system_io/system_io.cpp
@@ -8,16 +8,21 @@
 
 #include <system_wrappers/system_io.h>
 
-#ifdef __linux__
+#if defined(__linux__) || defined (__APPLE__)
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #endif
 
+#ifdef __APPLE__
+#include <sys/select.h>
+#endif
+
+
 namespace hidra2 {
 
-const int SystemIO::kNetBufferSize = 1024 * 1024 * 50; //MiByte
+const int SystemIO::kNetBufferSize = 1024 * 1024 ; //MiByte
 
 /*******************************************************************************
  *                              system_io.cpp                                  *
diff --git a/common/cpp/src/system_io/system_io_linux.cpp b/common/cpp/src/system_io/system_io_linux.cpp
index 4ee197806..4bbb6cac0 100644
--- a/common/cpp/src/system_io/system_io_linux.cpp
+++ b/common/cpp/src/system_io/system_io_linux.cpp
@@ -16,6 +16,7 @@ using std::string;
 using std::vector;
 using std::chrono::system_clock;
 
+
 namespace hidra2 {
 
 /**
@@ -51,6 +52,11 @@ Error GetLastErrorFromErrno() {
         return IOErrorTemplates::kConnectionResetByPeer.Generate();
     case ENOTSOCK:
         return IOErrorTemplates::kSocketOperationOnNonSocket.Generate();
+    case ENOPROTOOPT:
+        return IOErrorTemplates::kSocketOperationUnknownAtLevel.Generate();
+    case EDOM:
+        return IOErrorTemplates::kSocketOperationValueOutOfBound.Generate();
+
     default:
         std::cout << "[IOErrorsFromErrno] Unknown error code: " << errno << std::endl;
         Error err = IOErrorTemplates::kUnknownIOError.Generate();
@@ -170,7 +176,7 @@ void SystemIO::CollectFileInformationRecursively(const std::string& path,
 void SystemIO::ApplyNetworkOptions(SocketDescriptor socket_fd, Error* err) const {
     //TODO: Need to change network layer code, so everything can be NonBlocking
     // in use and one have to wait for some time until the system cleans up the stuff
-    //int flags;
+    int flag;
     if (
         /*(flags = fcntl(socket_fd, F_GETFL, 0)) == -1
         ||
@@ -178,9 +184,7 @@ void SystemIO::ApplyNetworkOptions(SocketDescriptor socket_fd, Error* err) const
         ||*/
         setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, (char*)&kNetBufferSize, sizeof(kNetBufferSize)) != 0
         ||
-        setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&kNetBufferSize, sizeof(kNetBufferSize)) != 0
-        ||
-        setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, (char*)&kNetBufferSize, sizeof(kNetBufferSize)) != 0
+        setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) != 0
     ) {
         *err = GetLastError();
     }
diff --git a/common/cpp/src/system_io/system_io_windows.cpp b/common/cpp/src/system_io/system_io_windows.cpp
index 59b591cc4..483956f00 100644
--- a/common/cpp/src/system_io/system_io_windows.cpp
+++ b/common/cpp/src/system_io/system_io_windows.cpp
@@ -124,7 +124,7 @@ FileInfo GetFileInfo_win(const WIN32_FIND_DATA& f, const string& name, Error* er
 FileInfo SystemIO::GetFileInfo(const std::string& name, Error* err) const {
     WIN32_FIND_DATA f;
 
-    auto hFind = FindFirstFile(name.c_str() , &f);
+    auto hFind = FindFirstFile(name.c_str(), &f);
     if (hFind == INVALID_HANDLE_VALUE) {
         *err = IOErrorFromGetLastError();
         (*err)->Append(name);
diff --git a/producer/api/src/producer_impl.cpp b/producer/api/src/producer_impl.cpp
index 378c8aa87..25e88a5a3 100644
--- a/producer/api/src/producer_impl.cpp
+++ b/producer/api/src/producer_impl.cpp
@@ -104,7 +104,7 @@ hidra2::ProducerError hidra2::ProducerImpl::Send(uint64_t file_id, const void* d
         if(sendDataResponse.error_code == kNetErrorFileIdAlreadyInUse) {
             return hidra2::ProducerError::kFileIdAlreadyInUse;
         }
-        std::cerr << "Server reported an error. NetErrorCode: " << sendDataResponse.error_code << std::endl;
+        std::cerr << "Server reported an error. NetErrorCode: " << int(sendDataResponse.error_code) << std::endl;
         return ProducerError::kUnknownServerError;
     }
 
diff --git a/receiver/src/receiver_error.h b/receiver/src/receiver_error.h
index 2e74565b3..b82705288 100644
--- a/receiver/src/receiver_error.h
+++ b/receiver/src/receiver_error.h
@@ -48,7 +48,9 @@ class ReceiverErrorTemplate : public SimpleErrorTemplate {
 };
 
 namespace ReceiverErrorTemplates {
-auto const kInvalidOpCode = ReceiverErrorTemplate{"Invalid Opcode", ReceiverErrorType::kInvalidOpCode};
+auto const kInvalidOpCode = ReceiverErrorTemplate {
+                                "Invalid Opcode", ReceiverErrorType::kInvalidOpCode
+                            };
 };
 }
 
-- 
GitLab