Răsfoiți Sursa

Add close method to Socket class

Patrick-Christopher Mattulat 1 an în urmă
părinte
comite
f43a8283ba

+ 1 - 0
include/ls_std/core/interface/IPosixSocket.hpp

@@ -21,6 +21,7 @@ namespace ls::std::core::interface_type
       virtual ~IPosixSocket() = default;
 
       virtual int bind(int _socketFileDescriptor, const struct sockaddr* _address, socklen_t _addressLength) = 0;
+      virtual int close(int _socketFileDescriptor) = 0;
       virtual int connect(int _socketFileDescriptor, const struct sockaddr* _address, socklen_t _addressLength) = 0;
       virtual int create(int _domain, int _type, int _protocol) = 0; // constructor
       virtual int listen(int _socketFileDescriptor, int _backlog) = 0;

+ 1 - 0
include/ls_std/network/socket/MockPosixSocket.hpp

@@ -23,6 +23,7 @@ namespace ls_std_network_test
       ~MockPosixSocket() override = default;
 
       MOCK_METHOD(int, bind, (int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength), (override));
+      MOCK_METHOD(int, close, (int _socketFileDescriptor), (override));
       MOCK_METHOD(int, connect, (int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength), (override));
       MOCK_METHOD(int, create, (int _domain, int _type, int _protocol), (override));
       MOCK_METHOD(int, listen, (int _socketFileDescriptor, int _backlog), (override));

+ 1 - 0
include/ls_std/network/socket/PosixSocket.hpp

@@ -22,6 +22,7 @@ namespace ls::std::network
       ~PosixSocket() override = default;
 
       int bind(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength) override;
+      int close(int _socketFileDescriptor) override;
       int connect(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength) override;
       int create(int _domain, int _type, int _protocol) override;
       int listen(int _socketFileDescriptor, int _backlog) override;

+ 2 - 0
include/ls_std/network/socket/Socket.hpp

@@ -29,6 +29,7 @@ namespace ls::std::network
       ~Socket() override = default;
 
       [[nodiscard]] bool bind();
+      [[nodiscard]] bool close();
       [[nodiscard]] bool connect();
       [[nodiscard]] bool isInitialized() const;
       [[nodiscard]] bool listen();
@@ -43,6 +44,7 @@ namespace ls::std::network
 
       #if defined(unix) || defined(__APPLE__)
       [[nodiscard]] bool _bindUnix();
+      [[nodiscard]] bool _closeUnix();
       [[nodiscard]] bool _connectUnix();
       #endif
       [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;

+ 6 - 0
source/ls_std/network/socket/PosixSocket.cpp

@@ -9,12 +9,18 @@
 
 #include <ls_std/network/socket/PosixSocket.hpp>
 #include <sys/socket.h>
+#include <unistd.h>
 
 int ls::std::network::PosixSocket::bind(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength)
 {
   return ::bind(_socketFileDescriptor, _address, _addressLength);
 }
 
+int ls::std::network::PosixSocket::close(int _socketFileDescriptor)
+{
+  return ::close(_socketFileDescriptor);
+}
+
 int ls::std::network::PosixSocket::connect(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength)
 {
   return ::connect(_socketFileDescriptor, _address, _addressLength);

+ 12 - 0
source/ls_std/network/socket/Socket.cpp

@@ -34,6 +34,13 @@ bool ls::std::network::Socket::bind()
   #endif
 }
 
+bool ls::std::network::Socket::close()
+{
+  #if defined(unix) || defined(__APPLE__)
+  return ls::std::network::Socket::_closeUnix();
+  #endif
+}
+
 bool ls::std::network::Socket::connect()
 {
   #if defined(unix) || defined(__APPLE__)
@@ -65,6 +72,11 @@ bool ls::std::network::Socket::_bindUnix()
   return this->parameter.posixSocket->bind(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&convertedSocketAddress.socketAddressUnix), sizeof(convertedSocketAddress.socketAddressUnix)) == 0;
 }
 
+bool ls::std::network::Socket::_closeUnix()
+{
+  return this->parameter.posixSocket->close(this->unixDescriptor) == 0;
+}
+
 bool ls::std::network::Socket::_connectUnix()
 {
   ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());

+ 18 - 0
test/cases/network/socket/SocketTest.cpp

@@ -69,6 +69,24 @@ namespace
     ASSERT_TRUE(socket.bind());
   }
 
+  TEST_F(SocketTest, close)
+  {
+    SocketParameter parameter = generateSocketParameter();
+
+    #if defined(unix) || defined(__APPLE__)
+    shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
+    parameter.posixSocket = mockSocket;
+
+    EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
+    ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
+    EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
+    ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
+    #endif
+
+    Socket socket{parameter};
+    ASSERT_TRUE(socket.close());
+  }
+
   TEST_F(SocketTest, connect)
   {
     SocketParameter parameter = generateSocketParameter();