Socket.cpp 4.1 KB

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