浏览代码

Add Socket class (incomplete)

- add create functionality for unix like systems
- add AddressFamily enum
- add NetworkProtocol enum
- add SocketType enum
- add test for Socket class
Patrick-Christopher Mattulat 3 年之前
父节点
当前提交
aa7a9c68f3

+ 5 - 3
CMakeLists.txt

@@ -9,7 +9,7 @@ project(ls_std VERSION 2020.3.1)
 # Options
 ##########################################################
 
-option(LS_STD_BUILD_WITH_TESTS "Build project with tests..." OFF)
+option(LS_STD_BUILD_WITH_TESTS "Build project with tests..." ON)
 option(LS_STD_BUILD_STATIC "Build ls_std static library..." ON)
 option(LS_STD_BUILD_SHARED "Build ls_std shared library..." OFF)
 option(LS_STD_BUILD_MODULE "Build ls_std module library..." OFF)
@@ -91,7 +91,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/xml/XMLParser.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/Event.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/EventHandler.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/EventManager.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/EventManager.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/network/Socket.cpp)
 
 if (${LS_STD_BUILD_WITH_TESTS})
     set(TEST_FILES
@@ -145,7 +146,8 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/GossipNewsEvent.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/GossipNewsAgency.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventHandlerTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventManagerTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventManagerTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/network/SocketTest.cpp)
 endif ()
 
 ##########################################################

+ 3 - 1
include/ls_std/ls_std.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-10-29
- * Changed:         2020-11-27
+ * Changed:         2020-12-06
  *
  * */
 
@@ -77,4 +77,6 @@
 #include "event/IEventSubscriber.hpp"
 #include "event/EventManager.hpp"
 
+#include "network/Socket.hpp"
+
 #endif

+ 20 - 0
include/ls_std/network/AddressFamily.hpp

@@ -0,0 +1,20 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#ifndef LS_STD_ADDRESS_FAMILY_HPP
+#define LS_STD_ADDRESS_FAMILY_HPP
+
+namespace ls_std {
+  enum AddressFamily {
+    ADDRESS_FAMILY_IP_V4 = 0,
+    ADDRESS_FAMILY_IP_V6
+  };
+}
+
+#endif

+ 20 - 0
include/ls_std/network/NetworkProtocol.hpp

@@ -0,0 +1,20 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#ifndef LS_STD_NETWORK_PROTOCOL_HPP
+#define LS_STD_NETWORK_PROTOCOL_HPP
+
+namespace ls_std {
+  enum NetworkProtocol {
+    NETWORK_PROTOCOL_TCP_IP = 0,
+    NETWORK_PROTOCOL_UDP_IP
+  };
+}
+
+#endif

+ 48 - 0
include/ls_std/network/Socket.hpp

@@ -0,0 +1,48 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#ifndef LS_STD_SOCKET_HPP
+#define LS_STD_SOCKET_HPP
+
+#include <ls_std/base/Class.hpp>
+#include "AddressFamily.hpp"
+#include "SocketType.hpp"
+#include "NetworkProtocol.hpp"
+
+#if defined(unix) || defined(__APPLE__)
+#include <sys/types.h>
+#include <sys/socket.h>
+#endif
+
+namespace ls_std {
+  class Socket : public ls_std::Class {
+    public:
+
+      Socket();
+      ~Socket() override = default;
+
+      bool create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _protocol);
+
+    private:
+
+      #if defined(unix) || defined(__APPLE__)
+        int descriptor {};
+      #endif
+
+      static int _convertAddressFamily(ls_std::AddressFamily _addressFamily);
+      static int _convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol);
+      static int _convertSocketType(ls_std::SocketType _socketType);
+
+      #if defined(unix) || defined(__APPLE__)
+        static int _create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol);
+      #endif
+  };
+}
+
+#endif

+ 20 - 0
include/ls_std/network/SocketType.hpp

@@ -0,0 +1,20 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#ifndef LS_STD_SOCKET_TYPE_HPP
+#define LS_STD_SOCKET_TYPE_HPP
+
+namespace ls_std {
+  enum SocketType {
+    SOCKET_TYPE_STREAM = 0,
+    SOCKET_TYPE_DATAGRAM
+  };
+}
+
+#endif

+ 98 - 0
source/ls_std/network/Socket.cpp

@@ -0,0 +1,98 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#include <ls_std/network/Socket.hpp>
+
+#if defined(unix) || defined(__APPLE__)
+#include <sys/socket.h>
+#include <resolv.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 created;
+
+  #if defined(unix) || defined(__APPLE__)
+    this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _protocol);
+    created = this->descriptor >= 0;
+  #endif
+
+  return created;
+}
+
+int ls_std::Socket::_convertAddressFamily(ls_std::AddressFamily _addressFamily)
+{
+  int addressFamily;
+
+  switch(_addressFamily)
+  {
+    case ls_std::ADDRESS_FAMILY_IP_V4:
+    {
+      addressFamily = AF_INET;
+    } break;
+    case ls_std::ADDRESS_FAMILY_IP_V6:
+    {
+      addressFamily = AF_INET6;
+    } break;
+  }
+
+  return addressFamily;
+}
+
+int ls_std::Socket::_convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol)
+{
+  int networkProtocol;
+
+  switch(_networkProtocol)
+  {
+    case ls_std::NETWORK_PROTOCOL_TCP_IP:
+    {
+      networkProtocol = IPPROTO_TCP;
+    } break;
+    case ls_std::NETWORK_PROTOCOL_UDP_IP:
+    {
+      networkProtocol = IPPROTO_UDP;
+    } break;
+  }
+
+  return networkProtocol;
+}
+
+int ls_std::Socket::_convertSocketType(ls_std::SocketType _socketType)
+{
+  int socketType;
+
+  switch(_socketType)
+  {
+    case ls_std::SOCKET_TYPE_STREAM:
+    {
+      socketType = SOCK_STREAM;
+    } break;
+    case ls_std::SOCKET_TYPE_DATAGRAM:
+    {
+      socketType = SOCK_DGRAM;
+    } break;
+  }
+
+  return socketType;
+}
+
+#if defined(unix) || defined(__APPLE__)
+  int 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

+ 29 - 0
test/cases/network/SocketTest.cpp

@@ -0,0 +1,29 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-06
+ * Changed:         2020-12-06
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std.hpp>
+
+namespace {
+  class SocketTest : public ::testing::Test {
+    protected:
+
+      SocketTest() = default;
+      ~SocketTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SocketTest, create)
+  {
+    ls_std::Socket socket {};
+    ASSERT_TRUE(socket.create(ls_std::ADDRESS_FAMILY_IP_V4, ls_std::SOCKET_TYPE_STREAM, ls_std::NETWORK_PROTOCOL_TCP_IP));
+  }
+}