Socket.cpp 6.1 KB

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