Socket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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-06
  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. bool ls_std::Socket::_closeOnWindows() const
  61. {
  62. int result = closesocket(this->descriptor);
  63. bool closed = result == 0;
  64. if(!closed) {
  65. std::cout << ls_std::WindowsUtils::getMessageFromErrorCode(WSAGetLastError()) << std::endl; // TODO: use logger
  66. }
  67. return closed;
  68. }
  69. int ls_std::Socket::_convertAddressFamily(ls_std::AddressFamily _addressFamily)
  70. {
  71. int addressFamily;
  72. switch(_addressFamily)
  73. {
  74. case ls_std::ADDRESS_FAMILY_IP_V4:
  75. {
  76. addressFamily = AF_INET;
  77. } break;
  78. case ls_std::ADDRESS_FAMILY_IP_V6:
  79. {
  80. addressFamily = AF_INET6;
  81. } break;
  82. }
  83. return addressFamily;
  84. }
  85. int ls_std::Socket::_convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol)
  86. {
  87. int networkProtocol;
  88. switch(_networkProtocol)
  89. {
  90. case ls_std::NETWORK_PROTOCOL_TCP_IP:
  91. {
  92. networkProtocol = IPPROTO_TCP;
  93. } break;
  94. case ls_std::NETWORK_PROTOCOL_UDP_IP:
  95. {
  96. networkProtocol = IPPROTO_UDP;
  97. } break;
  98. }
  99. return networkProtocol;
  100. }
  101. int ls_std::Socket::_convertSocketType(ls_std::SocketType _socketType)
  102. {
  103. int socketType;
  104. switch(_socketType)
  105. {
  106. case ls_std::SOCKET_TYPE_STREAM:
  107. {
  108. socketType = SOCK_STREAM;
  109. } break;
  110. case ls_std::SOCKET_TYPE_DATAGRAM:
  111. {
  112. socketType = SOCK_DGRAM;
  113. } break;
  114. }
  115. return socketType;
  116. }
  117. #if defined(unix) || defined(__APPLE__)
  118. int ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  119. {
  120. int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
  121. int socketType = ls_std::Socket::_convertSocketType(_socketType);
  122. int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
  123. return socket(addressFamily, socketType, networkProtocol);
  124. }
  125. #endif
  126. #ifdef _WIN32
  127. SOCKET ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  128. {
  129. int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
  130. int socketType = ls_std::Socket::_convertSocketType(_socketType);
  131. int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
  132. return socket(addressFamily, socketType, networkProtocol);
  133. }
  134. #endif