Socket.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-12
  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/exception/WrongProtocolException.hpp>
  17. #include <memory>
  18. #include <utility>
  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. bool ls::std::network::Socket::accept()
  28. {
  29. if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
  30. {
  31. throw ls::std::core::WrongProtocolException{};
  32. }
  33. #if defined(unix) || defined(__APPLE__)
  34. return ls::std::network::Socket::_acceptUnix();
  35. #endif
  36. }
  37. bool ls::std::network::Socket::bind()
  38. {
  39. #if defined(unix) || defined(__APPLE__)
  40. return ls::std::network::Socket::_bindUnix();
  41. #endif
  42. }
  43. bool ls::std::network::Socket::close()
  44. {
  45. #if defined(unix) || defined(__APPLE__)
  46. return ls::std::network::Socket::_closeUnix();
  47. #endif
  48. }
  49. bool ls::std::network::Socket::connect()
  50. {
  51. #if defined(unix) || defined(__APPLE__)
  52. return ls::std::network::Socket::_connectUnix();
  53. #endif
  54. }
  55. bool ls::std::network::Socket::isInitialized() const
  56. {
  57. return this->initialized;
  58. }
  59. bool ls::std::network::Socket::listen()
  60. {
  61. if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
  62. {
  63. throw ls::std::core::WrongProtocolException{};
  64. }
  65. #if defined(unix) || defined(__APPLE__)
  66. return ls::std::network::Socket::_listenUnix();
  67. #endif
  68. }
  69. #if defined(unix) || defined(__APPLE__)
  70. bool ls::std::network::Socket::_acceptUnix()
  71. {
  72. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  73. return this->parameter.posixSocket->accept(this->unixDescriptor, reinterpret_cast<sockaddr *>(&convertedSocketAddress.socketAddressUnix), &convertedSocketAddress.addressLength) >= 0;
  74. }
  75. bool ls::std::network::Socket::_bindUnix()
  76. {
  77. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  78. return this->parameter.posixSocket->bind(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&convertedSocketAddress.socketAddressUnix), convertedSocketAddress.addressLength) == 0;
  79. }
  80. bool ls::std::network::Socket::_closeUnix()
  81. {
  82. return this->parameter.posixSocket->close(this->unixDescriptor) == 0;
  83. }
  84. bool ls::std::network::Socket::_connectUnix()
  85. {
  86. ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
  87. return this->parameter.posixSocket->connect(this->unixDescriptor, reinterpret_cast<const sockaddr *>(&convertedSocketAddress.socketAddressUnix), convertedSocketAddress.addressLength) == 0;
  88. }
  89. #endif
  90. ls::std::network::SocketAddressMapperParameter ls::std::network::Socket::_createSocketAddressMapperParameter() const
  91. {
  92. ls::std::network::SocketAddressMapperParameter mapperParameter{};
  93. mapperParameter.socketAddress = this->parameter.socketAddress;
  94. mapperParameter.protocolFamilyType = this->parameter.protocolFamilyType;
  95. return mapperParameter;
  96. }
  97. bool ls::std::network::Socket::_init()
  98. {
  99. #if defined(unix) || defined(__APPLE__)
  100. return this->_initUnix();
  101. #endif
  102. }
  103. #if defined(unix) || defined(__APPLE__)
  104. bool ls::std::network::Socket::_initUnix()
  105. {
  106. this->_setUnixSocketApi();
  107. ls::std::network::ConvertedProtocolFamily convertedProtocolFamily = ls::std::network::ProtocolFamilyMapper::from(this->parameter.protocolFamilyType);
  108. ls::std::network::Protocol protocol = ls::std::network::ProtocolMapper::from(this->parameter.socketAddress.protocolType);
  109. return this->parameter.posixSocket->create(convertedProtocolFamily.unixDomain, protocol.unixProtocol, 0) != -1;
  110. }
  111. bool ls::std::network::Socket::_listenUnix()
  112. {
  113. return this->parameter.posixSocket->listen(this->unixDescriptor, this->parameter.queueSize) == 0;
  114. }
  115. void ls::std::network::Socket::_setUnixSocketApi()
  116. {
  117. if (this->parameter.posixSocket == nullptr)
  118. {
  119. this->parameter.posixSocket = ::std::make_shared<ls::std::network::PosixSocket>();
  120. }
  121. }
  122. #endif