Socket.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-11-16
  6. * Changed: 2022-12-16
  7. *
  8. * */
  9. #include <ls_std/network/socket/Socket.hpp>
  10. #include <ls_std/network/core/ProtocolFamilyMapper.hpp>
  11. #include <ls_std/network/core/ProtocolMapper.hpp>
  12. #include <ls_std/network/socket/ConvertedSocketAddress.hpp>
  13. #include <ls_std/network/socket/SocketAddressMapper.hpp>
  14. #include <ls_std/network/socket/MockPosixSocket.hpp>
  15. #include <ls_std/network/socket/PosixSocket.hpp>
  16. #include <ls_std/core/api/io/PosixReader.hpp>
  17. #include <ls_std/core/exception/WrongProtocolException.hpp>
  18. #include <memory>
  19. ls::std::network::Socket::Socket(ls::std::network::SocketParameter _parameter) : ls::std::core::Class("Socket"),
  20. parameter(::std::move(_parameter))
  21. {
  22. #if defined(unix) || defined(__APPLE__)
  23. this->unixDescriptor = ls::std::network::Socket::_initUnix();
  24. this->initialized = this->unixDescriptor != -1;
  25. #endif
  26. }
  27. ls::std::network::Socket::~Socket()
  28. {
  29. delete[] this->readBuffer;
  30. }
  31. ls::std::core::type::byte_field ls::std::network::Socket::read()
  32. {
  33. if (!this->readBufferSet)
  34. {
  35. this->_initReadBuffer(); // TODO: is this really wise to initialize once? what if reading second time wouldn't work?
  36. this->readBufferSet = true;
  37. }
  38. return this->_read();
  39. }
  40. bool ls::std::network::Socket::accept()
  41. {
  42. if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
  43. {
  44. throw ls::std::core::WrongProtocolException{};
  45. }
  46. #if defined(unix) || defined(__APPLE__)
  47. return ls::std::network::Socket::_acceptUnix();
  48. #endif
  49. }
  50. bool ls::std::network::Socket::bind()
  51. {
  52. #if defined(unix) || defined(__APPLE__)
  53. return ls::std::network::Socket::_bindUnix();
  54. #endif
  55. }
  56. bool ls::std::network::Socket::close()
  57. {
  58. #if defined(unix) || defined(__APPLE__)
  59. return ls::std::network::Socket::_closeUnix();
  60. #endif
  61. }
  62. bool ls::std::network::Socket::connect()
  63. {
  64. #if defined(unix) || defined(__APPLE__)
  65. return ls::std::network::Socket::_connectUnix();
  66. #endif
  67. }
  68. bool ls::std::network::Socket::isInitialized() const
  69. {
  70. return this->initialized;
  71. }
  72. bool ls::std::network::Socket::listen()
  73. {
  74. if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
  75. {
  76. throw ls::std::core::WrongProtocolException{};
  77. }
  78. #if defined(unix) || defined(__APPLE__)
  79. return ls::std::network::Socket::_listenUnix();
  80. #endif
  81. }
  82. #if defined(unix) || defined(__APPLE__)
  83. bool ls::std::network::Socket::_acceptUnix()
  84. {
  85. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  86. return this->parameter.posixSocket->accept(this->unixDescriptor, reinterpret_cast<sockaddr *>(&convertedSocketAddress.socketAddressUnix), &convertedSocketAddress.addressLength) >= 0;
  87. }
  88. bool ls::std::network::Socket::_bindUnix()
  89. {
  90. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  91. return this->parameter.posixSocket->bind(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&convertedSocketAddress.socketAddressUnix), convertedSocketAddress.addressLength) == 0;
  92. }
  93. bool ls::std::network::Socket::_closeUnix()
  94. {
  95. return this->parameter.posixSocket->close(this->unixDescriptor) == 0;
  96. }
  97. bool ls::std::network::Socket::_connectUnix()
  98. {
  99. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  100. return this->parameter.posixSocket->connect(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&convertedSocketAddress.socketAddressUnix), convertedSocketAddress.addressLength) == 0;
  101. }
  102. #endif
  103. ls::std::network::SocketAddressMapperParameter ls::std::network::Socket::_createSocketAddressMapperParameter() const
  104. {
  105. ls::std::network::SocketAddressMapperParameter mapperParameter{};
  106. mapperParameter.socketAddress = this->parameter.socketAddress;
  107. mapperParameter.protocolFamilyType = this->parameter.protocolFamilyType;
  108. return mapperParameter;
  109. }
  110. bool ls::std::network::Socket::_init()
  111. {
  112. #if defined(unix) || defined(__APPLE__)
  113. return this->_initUnix();
  114. #endif
  115. }
  116. void ls::std::network::Socket::_initReadBuffer()
  117. {
  118. if (this->parameter.readBufferSize <= 0)
  119. {
  120. throw ls::std::core::IllegalArgumentException{};
  121. }
  122. this->readBuffer = new ls::std::core::type::byte[this->parameter.readBufferSize];
  123. }
  124. #if defined(unix) || defined(__APPLE__)
  125. bool ls::std::network::Socket::_initUnix()
  126. {
  127. this->_setPosixReaderApi();
  128. this->_setUnixSocketApi(); // TODO: rename to _setPosixSocketApi
  129. ls::std::network::ConvertedProtocolFamily convertedProtocolFamily = ls::std::network::ProtocolFamilyMapper::from(this->parameter.protocolFamilyType);
  130. ls::std::network::Protocol protocol = ls::std::network::ProtocolMapper::from(this->parameter.socketAddress.protocolType);
  131. return this->parameter.posixSocket->create(convertedProtocolFamily.unixDomain, protocol.unixProtocol, 0) != -1;
  132. }
  133. bool ls::std::network::Socket::_listenUnix()
  134. {
  135. return this->parameter.posixSocket->listen(this->unixDescriptor, this->parameter.queueSize) == 0;
  136. }
  137. #endif
  138. ls::std::core::type::byte_field ls::std::network::Socket::_read()
  139. {
  140. #if defined(unix) || defined(__APPLE__)
  141. return this->_readUnix();
  142. #endif
  143. }
  144. #if defined(unix) || defined(__APPLE__)
  145. ls::std::core::type::byte_field ls::std::network::Socket::_readUnix()
  146. {
  147. size_t size = this->parameter.posixReader->read(this->unixDescriptor, this->readBuffer, this->parameter.readBufferSize);
  148. if (size == -1)
  149. {
  150. throw ls::std::core::FileOperationException{};
  151. }
  152. return ls::std::core::type::byte_field{this->readBuffer, size};
  153. }
  154. void ls::std::network::Socket::_setPosixReaderApi()
  155. {
  156. if (this->parameter.posixReader == nullptr)
  157. {
  158. this->parameter.posixReader = ::std::make_shared<ls::std::core::api::PosixReader>();
  159. }
  160. }
  161. void ls::std::network::Socket::_setUnixSocketApi()
  162. {
  163. if (this->parameter.posixSocket == nullptr)
  164. {
  165. this->parameter.posixSocket = ::std::make_shared<ls::std::network::PosixSocket>();
  166. }
  167. }
  168. #endif