Browse Source

Extend Socket class

- add create functionality for Windows OS
- adjust CMakeLists.txt file
Patrick-Christopher Mattulat 3 years ago
parent
commit
a27b613ada
4 changed files with 51 additions and 4 deletions
  1. 16 1
      CMakeLists.txt
  2. 4 0
      include/ls_std/ls_std.hpp
  3. 11 1
      include/ls_std/network/Socket.hpp
  4. 20 2
      source/ls_std/network/Socket.cpp

+ 16 - 1
CMakeLists.txt

@@ -186,5 +186,20 @@ endif ()
 
 if (${LS_STD_BUILD_WITH_TESTS})
     message("${PROJECT_NAME}: Linking libraries for test application...")
-    target_link_libraries(${PROJECT_NAME}_test gtest gtest_main "${PROJECT_NAME}_${PROJECT_VERSION}_static")
+
+    if (WIN32)
+        SET(CMAKE_FIND_LIBRARY_PREFIXES "")
+        SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll")
+
+        find_library(WSOCK32_LIBRARY wsock32)
+        find_library(WS2_32_LIBRARY ws2_32)
+        message("${PROJECT_NAME}: library search - ${WSOCK32_LIBRARY}...")
+        message("${PROJECT_NAME}: library search - ${WS2_32_LIBRARY}...")
+
+        target_link_libraries(${PROJECT_NAME}_test gtest gtest_main "${WSOCK32_LIBRARY}" "${WS2_32_LIBRARY}" "${PROJECT_NAME}_${PROJECT_VERSION}_static")
+    endif ()
+
+    if (UNIX)
+        target_link_libraries(${PROJECT_NAME}_test gtest gtest_main "${PROJECT_NAME}_${PROJECT_VERSION}_static")
+    endif ()
 endif ()

+ 4 - 0
include/ls_std/ls_std.hpp

@@ -10,6 +10,10 @@
 #ifndef LS_STD_LS_STD_HPP
 #define LS_STD_LS_STD_HPP
 
+#if _WIN32
+#include <winsock2.h>
+#endif
+
 #include "base/Class.hpp"
 #include "base/Types.hpp"
 #include "base/Version.hpp"

+ 11 - 1
include/ls_std/network/Socket.hpp

@@ -20,6 +20,10 @@
 #include <sys/socket.h>
 #endif
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
 namespace ls_std {
   class Socket : public ls_std::Class {
     public:
@@ -27,13 +31,16 @@ namespace ls_std {
       Socket();
       ~Socket() override = default;
 
-      bool create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _protocol);
+      bool create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol);
 
     private:
 
       #if defined(unix) || defined(__APPLE__)
         int descriptor {};
       #endif
+      #ifdef _WIN32
+        SOCKET descriptor {};
+      #endif
 
       static int _convertAddressFamily(ls_std::AddressFamily _addressFamily);
       static int _convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol);
@@ -42,6 +49,9 @@ namespace ls_std {
       #if defined(unix) || defined(__APPLE__)
         static int _create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol);
       #endif
+      #ifdef _WIN32
+        static SOCKET _create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol);
+      #endif
   };
 }
 

+ 20 - 2
source/ls_std/network/Socket.cpp

@@ -13,16 +13,23 @@
 #include <sys/socket.h>
 #include <resolv.h>
 #endif
+#ifdef _WIN32
+#include <http.h>
+#endif
 
 ls_std::Socket::Socket() : ls_std::Class("Socket")
 {}
 
-bool ls_std::Socket::create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _protocol)
+bool ls_std::Socket::create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
 {
   bool created;
 
   #if defined(unix) || defined(__APPLE__)
-    this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _protocol);
+    this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _networkProtocol);
+    created = this->descriptor >= 0;
+  #endif
+  #ifdef _WIN32
+    this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _networkProtocol);
     created = this->descriptor >= 0;
   #endif
 
@@ -96,3 +103,14 @@ int ls_std::Socket::_convertSocketType(ls_std::SocketType _socketType)
     return socket(addressFamily, socketType, networkProtocol);
   }
 #endif
+
+#ifdef _WIN32
+  SOCKET ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
+  {
+    int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
+    int socketType = ls_std::Socket::_convertSocketType(_socketType);
+    int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
+
+    return socket(addressFamily, socketType, networkProtocol);
+  }
+#endif