Socket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-06
  6. * Changed: 2020-12-21
  7. *
  8. * */
  9. #include <ls_std/network/Socket.hpp>
  10. #include <ls_std/base/Types.hpp>
  11. #if defined(unix) || defined(__APPLE__)
  12. #include <sys/socket.h>
  13. #include <resolv.h>
  14. #include <unistd.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <http.h>
  18. #include <iostream>
  19. #include <ls_std/utils/WindowsUtils.hpp>
  20. #endif
  21. ls_std::Socket::Socket() : ls_std::Class("Socket")
  22. {}
  23. ls_std::Socket::~Socket()
  24. {
  25. this->_close();
  26. }
  27. bool ls_std::Socket::close()
  28. {
  29. bool closed;
  30. #if defined(unix) || defined(__APPLE__)
  31. int result = ::close(this->descriptor);
  32. closed = result == 0;
  33. #endif
  34. #ifdef _WIN32
  35. closed = this->_close();
  36. #endif
  37. return closed;
  38. }
  39. bool ls_std::Socket::create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  40. {
  41. bool created;
  42. #if defined(unix) || defined(__APPLE__)
  43. this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _networkProtocol);
  44. created = this->descriptor >= 0;
  45. #endif
  46. #ifdef _WIN32
  47. this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _networkProtocol);
  48. created = this->descriptor >= 0;
  49. #endif
  50. return created;
  51. }
  52. bool ls_std::Socket::_close()
  53. {
  54. bool closed;
  55. #ifdef _WIN32
  56. closed = this->_closeOnWindows();
  57. #endif
  58. return closed;
  59. }
  60. #ifdef _WIN32
  61. bool ls_std::Socket::_closeOnWindows() const
  62. {
  63. int result = closesocket(this->descriptor);
  64. bool closed = result == 0;
  65. if(!closed) {
  66. std::cout << ls_std::WindowsUtils::getMessageFromErrorCode(WSAGetLastError()) << std::endl; // TODO: use logger
  67. }
  68. return closed;
  69. }
  70. #endif
  71. int ls_std::Socket::_convertAddressFamily(ls_std::AddressFamily _addressFamily)
  72. {
  73. int addressFamily;
  74. switch(_addressFamily)
  75. {
  76. case ls_std::ADDRESS_FAMILY_IP_V4:
  77. {
  78. addressFamily = AF_INET;
  79. } break;
  80. case ls_std::ADDRESS_FAMILY_IP_V6:
  81. {
  82. addressFamily = AF_INET6;
  83. } break;
  84. }
  85. return addressFamily;
  86. }
  87. int ls_std::Socket::_convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol)
  88. {
  89. int networkProtocol;
  90. switch(_networkProtocol)
  91. {
  92. case ls_std::NETWORK_PROTOCOL_TCP_IP:
  93. {
  94. networkProtocol = IPPROTO_TCP;
  95. } break;
  96. case ls_std::NETWORK_PROTOCOL_UDP_IP:
  97. {
  98. networkProtocol = IPPROTO_UDP;
  99. } break;
  100. }
  101. return networkProtocol;
  102. }
  103. int ls_std::Socket::_convertSocketType(ls_std::SocketType _socketType)
  104. {
  105. int socketType;
  106. switch(_socketType)
  107. {
  108. case ls_std::SOCKET_TYPE_STREAM:
  109. {
  110. socketType = SOCK_STREAM;
  111. } break;
  112. case ls_std::SOCKET_TYPE_DATAGRAM:
  113. {
  114. socketType = SOCK_DGRAM;
  115. } break;
  116. }
  117. return socketType;
  118. }
  119. #if defined(unix) || defined(__APPLE__)
  120. int ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  121. {
  122. int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
  123. int socketType = ls_std::Socket::_convertSocketType(_socketType);
  124. int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
  125. return socket(addressFamily, socketType, networkProtocol);
  126. }
  127. #endif
  128. #ifdef _WIN32
  129. SOCKET ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  130. {
  131. int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
  132. int socketType = ls_std::Socket::_convertSocketType(_socketType);
  133. int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
  134. return socket(addressFamily, socketType, networkProtocol);
  135. }
  136. #endif