Parcourir la source

Add POSIX reader reference to Socket class (unstable)

- replaced hard coded POSIX read function with API interface call
- test is missing
Patrick-Christopher Mattulat il y a 1 an
Parent
commit
f9ae4946c6

+ 2 - 2
include/ls_std/network/socket/MockPosixSocket.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-12-09
- * Changed:         2022-12-12
+ * Changed:         2022-12-15
  *
  * */
 
@@ -13,7 +13,7 @@
 #include <ls_std/ls_std_core.hpp>
 #include <gmock/gmock.h>
 
-namespace ls_std_network_test
+namespace ls_std_network_test // TODO: challenge location
 {
   class MockPosixSocket : public ls::std::core::interface_type::IPosixSocket
   {

+ 19 - 3
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-12-11
+ * Changed:         2022-12-16
  *
  * */
 
@@ -18,15 +18,23 @@
 #include "SocketParameter.hpp"
 #include "SocketAddressMapperParameter.hpp"
 #include <memory>
+#include <ls_std/core/types/Types.hpp>
+#include <ls_std/core/interface/IReader.hpp>
 
 namespace ls::std::network
 {
-  class LS_STD_DYNAMIC_GOAL Socket : public ls::std::core::Class
+  class LS_STD_DYNAMIC_GOAL Socket : public ls::std::core::Class, public ls::std::core::interface_type::IReader
   {
     public:
 
       explicit Socket(ls::std::network::SocketParameter _parameter);
-      ~Socket() override = default;
+      ~Socket() override;
+
+      // implementation
+
+      ls::std::core::type::byte_field read() override;
+
+      // other functionalities
 
       [[nodiscard]] bool accept();
       [[nodiscard]] bool bind();
@@ -39,6 +47,8 @@ namespace ls::std::network
 
       bool initialized{};
       ls::std::network::SocketParameter parameter{};
+      ls::std::core::type::byte* readBuffer{};
+      bool readBufferSet{};
       #if defined(unix) || defined(__APPLE__)
       int unixDescriptor{};
       #endif
@@ -51,9 +61,15 @@ namespace ls::std::network
       #endif
       [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;
       [[nodiscard]] bool _init();
+      void _initReadBuffer();
       #if defined(unix) || defined(__APPLE__)
       [[nodiscard]] bool _initUnix();
       [[nodiscard]] bool _listenUnix();
+      #endif
+      ls::std::core::type::byte_field _read();
+      #if defined(unix) || defined(__APPLE__)
+      ls::std::core::type::byte_field _readUnix();
+      void _setPosixReaderApi();
       void _setUnixSocketApi();
       #endif
   };

+ 6 - 2
include/ls_std/network/socket/SocketParameter.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-12-12
+ * Changed:         2022-12-16
  *
  * */
 
@@ -11,6 +11,8 @@
 #define LS_STD_SOCKET_PARAMETER_HPP
 
 #include <ls_std/network/core/ProtocolFamilyType.hpp>
+#include <ls_std/core/interface/IPosixSocket.hpp>
+#include <ls_std/core/interface/IPosixReader.hpp>
 #include "SocketAddress.hpp"
 #include <memory>
 
@@ -18,10 +20,12 @@ namespace ls::std::network
 {
   struct SocketParameter
   {
+    ::std::shared_ptr<ls::std::core::interface_type::IPosixReader> posixReader{};
     ::std::shared_ptr<ls::std::core::interface_type::IPosixSocket> posixSocket{};
     ls::std::network::ProtocolFamilyType protocolFamilyType{};
-    ls::std::network::SocketAddress socketAddress{};
     int queueSize{};
+    size_t readBufferSize{};
+    ls::std::network::SocketAddress socketAddress{};
   };
 }
 

+ 59 - 3
source/ls_std/network/socket/Socket.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-12-12
+ * Changed:         2022-12-16
  *
  * */
 
@@ -14,9 +14,9 @@
 #include <ls_std/network/socket/SocketAddressMapper.hpp>
 #include <ls_std/network/socket/MockPosixSocket.hpp>
 #include <ls_std/network/socket/PosixSocket.hpp>
+#include <ls_std/core/api/io/PosixReader.hpp>
 #include <ls_std/core/exception/WrongProtocolException.hpp>
 #include <memory>
-#include <utility>
 
 ls::std::network::Socket::Socket(ls::std::network::SocketParameter _parameter) : ls::std::core::Class("Socket"),
 parameter(::std::move(_parameter))
@@ -27,6 +27,22 @@ parameter(::std::move(_parameter))
   #endif
 }
 
+ls::std::network::Socket::~Socket()
+{
+  delete[] this->readBuffer;
+}
+
+ls::std::core::type::byte_field ls::std::network::Socket::read()
+{
+  if (!this->readBufferSet)
+  {
+    this->_initReadBuffer(); // TODO: is this really wise to initialize once? what if reading second time wouldn't work?
+    this->readBufferSet = true;
+  }
+
+  return this->_read();
+}
+
 bool ls::std::network::Socket::accept()
 {
   if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
@@ -119,10 +135,21 @@ bool ls::std::network::Socket::_init()
   #endif
 }
 
+void ls::std::network::Socket::_initReadBuffer()
+{
+  if (this->parameter.readBufferSize <= 0)
+  {
+    throw ls::std::core::IllegalArgumentException{};
+  }
+
+  this->readBuffer = new ls::std::core::type::byte[this->parameter.readBufferSize];
+}
+
 #if defined(unix) || defined(__APPLE__)
 bool ls::std::network::Socket::_initUnix()
 {
-  this->_setUnixSocketApi();
+  this->_setPosixReaderApi();
+  this->_setUnixSocketApi(); // TODO: rename to _setPosixSocketApi
   ls::std::network::ConvertedProtocolFamily convertedProtocolFamily = ls::std::network::ProtocolFamilyMapper::from(this->parameter.protocolFamilyType);
   ls::std::network::Protocol protocol = ls::std::network::ProtocolMapper::from(this->parameter.socketAddress.protocolType);
 
@@ -133,6 +160,35 @@ bool ls::std::network::Socket::_listenUnix()
 {
   return this->parameter.posixSocket->listen(this->unixDescriptor, this->parameter.queueSize) == 0;
 }
+#endif
+
+ls::std::core::type::byte_field ls::std::network::Socket::_read()
+{
+  #if defined(unix) || defined(__APPLE__)
+  return this->_readUnix();
+  #endif
+}
+
+#if defined(unix) || defined(__APPLE__)
+ls::std::core::type::byte_field ls::std::network::Socket::_readUnix()
+{
+  size_t size = this->parameter.posixReader->read(this->unixDescriptor, this->readBuffer, this->parameter.readBufferSize);
+
+  if (size == -1)
+  {
+    throw ls::std::core::FileOperationException{};
+  }
+
+  return ls::std::core::type::byte_field{this->readBuffer, size};
+}
+
+void ls::std::network::Socket::_setPosixReaderApi()
+{
+  if (this->parameter.posixReader == nullptr)
+  {
+    this->parameter.posixReader = ::std::make_shared<ls::std::core::api::PosixReader>();
+  }
+}
 
 void ls::std::network::Socket::_setUnixSocketApi()
 {