Эх сурвалжийг харах

Add connect functionality to Socket class

Please note, that with this commit test is failing.
Patrick-Christopher Mattulat 2 жил өмнө
parent
commit
da93fa45ce

+ 8 - 1
include/ls_std/network/socket/Socket.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-11-17
+ * Changed:         2022-11-18
  *
  * */
 
@@ -23,13 +23,20 @@ namespace ls::std::network
       explicit Socket(const ls::std::network::SocketParameter& _parameter);
       ~Socket() override = default;
 
+      [[nodiscard]] bool connect();
       [[nodiscard]] bool isInitialized() const;
 
     private:
 
       bool initialized{};
       ls::std::network::SocketParameter parameter{};
+      #if defined(unix) || defined(__APPLE__)
+      int unixDescriptor{};
+      #endif
 
+      #if defined(unix) || defined(__APPLE__)
+      [[nodiscard]] bool _connectUnix() const;
+      #endif
       [[nodiscard]] static bool _init(const ls::std::network::SocketParameter& _parameter);
       #if defined(unix) || defined(__APPLE__)
       [[nodiscard]] static bool _initUnix(const ls::std::network::SocketParameter& _parameter);

+ 28 - 2
source/ls_std/network/socket/Socket.cpp

@@ -3,13 +3,15 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-11-17
+ * Changed:         2022-11-18
  *
  * */
 
 #include <ls_std/network/socket/Socket.hpp>
 #if defined(unix) || defined(__APPLE__)
 #include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #endif
 #include <ls_std/network/core/ProtocolFamilyMapper.hpp>
 #include <ls_std/network/core/ProtocolMapper.hpp>
@@ -17,7 +19,17 @@
 ls::std::network::Socket::Socket(const ls::std::network::SocketParameter& _parameter) : ls::std::core::Class("Socket"),
 parameter(_parameter)
 {
-  this->initialized = ls::std::network::Socket::_initUnix(_parameter);
+  #if defined(unix) || defined(__APPLE__)
+  this->unixDescriptor = ls::std::network::Socket::_initUnix(_parameter);
+  this->initialized = this->unixDescriptor != -1;
+  #endif
+}
+
+bool ls::std::network::Socket::connect()
+{
+  #if defined(unix) || defined(__APPLE__)
+  return ls::std::network::Socket::_connectUnix();
+  #endif
 }
 
 bool ls::std::network::Socket::isInitialized() const
@@ -25,6 +37,20 @@ bool ls::std::network::Socket::isInitialized() const
   return this->initialized;
 }
 
+#if defined(unix) || defined(__APPLE__)
+bool ls::std::network::Socket::_connectUnix() const
+{
+  sockaddr_in socketAddressUnix{}; // TODO: put everything inside a dedicated mapper
+  socketAddressUnix.sin_port = htons(this->parameter.socketAddress.port);
+
+  ls::std::network::ProtocolFamily protocolFamily = ls::std::network::ProtocolFamilyMapper::from(this->parameter.protocolFamilyType);
+  socketAddressUnix.sin_family = protocolFamily.unixDomain;
+  inet_aton(this->parameter.socketAddress.ipAddress.c_str(), &socketAddressUnix.sin_addr);
+
+  return ::connect(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&socketAddressUnix), sizeof(socketAddressUnix)) == 0;
+}
+#endif
+
 bool ls::std::network::Socket::_init(const ls::std::network::SocketParameter &_parameter)
 {
   #if defined(unix) || defined(__APPLE__)

+ 9 - 1
test/cases/network/socket/SocketTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-16
- * Changed:         2022-11-17
+ * Changed:         2022-11-18
  *
  * */
 
@@ -34,6 +34,8 @@ namespace
 
         SocketAddress socketAddress{};
         socketAddress.protocolType = ProtocolType::PROTOCOL_TYPE_TCP;
+        socketAddress.ipAddress = "127.0.0.1";
+        socketAddress.port = 2220;
         socketParameter.socketAddress = socketAddress;
 
         return socketParameter;
@@ -45,6 +47,12 @@ namespace
     ASSERT_STREQ("Socket", Socket{generateSocketParameter()}.getClassName().c_str());
   }
 
+  TEST_F(SocketTest, connect)
+  {
+    Socket socket{generateSocketParameter()};
+    ASSERT_TRUE(socket.connect());
+  }
+
   TEST_F(SocketTest, isInitialized)
   {
     Socket socket{generateSocketParameter()};