Socket.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if defined(unix) || defined(__APPLE__)
  11. #include <sys/socket.h>
  12. #include <resolv.h>
  13. #endif
  14. ls_std::Socket::Socket() : ls_std::Class("Socket")
  15. {}
  16. bool ls_std::Socket::create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _protocol)
  17. {
  18. bool created;
  19. #if defined(unix) || defined(__APPLE__)
  20. this->descriptor = ls_std::Socket::_create(_addressFamily, _socketType, _protocol);
  21. created = this->descriptor >= 0;
  22. #endif
  23. return created;
  24. }
  25. int ls_std::Socket::_convertAddressFamily(ls_std::AddressFamily _addressFamily)
  26. {
  27. int addressFamily;
  28. switch(_addressFamily)
  29. {
  30. case ls_std::ADDRESS_FAMILY_IP_V4:
  31. {
  32. addressFamily = AF_INET;
  33. } break;
  34. case ls_std::ADDRESS_FAMILY_IP_V6:
  35. {
  36. addressFamily = AF_INET6;
  37. } break;
  38. }
  39. return addressFamily;
  40. }
  41. int ls_std::Socket::_convertNetworkProtocol(ls_std::NetworkProtocol _networkProtocol)
  42. {
  43. int networkProtocol;
  44. switch(_networkProtocol)
  45. {
  46. case ls_std::NETWORK_PROTOCOL_TCP_IP:
  47. {
  48. networkProtocol = IPPROTO_TCP;
  49. } break;
  50. case ls_std::NETWORK_PROTOCOL_UDP_IP:
  51. {
  52. networkProtocol = IPPROTO_UDP;
  53. } break;
  54. }
  55. return networkProtocol;
  56. }
  57. int ls_std::Socket::_convertSocketType(ls_std::SocketType _socketType)
  58. {
  59. int socketType;
  60. switch(_socketType)
  61. {
  62. case ls_std::SOCKET_TYPE_STREAM:
  63. {
  64. socketType = SOCK_STREAM;
  65. } break;
  66. case ls_std::SOCKET_TYPE_DATAGRAM:
  67. {
  68. socketType = SOCK_DGRAM;
  69. } break;
  70. }
  71. return socketType;
  72. }
  73. #if defined(unix) || defined(__APPLE__)
  74. int ls_std::Socket::_create(ls_std::AddressFamily _addressFamily, ls_std::SocketType _socketType, ls_std::NetworkProtocol _networkProtocol)
  75. {
  76. int addressFamily = ls_std::Socket::_convertAddressFamily(_addressFamily);
  77. int socketType = ls_std::Socket::_convertSocketType(_socketType);
  78. int networkProtocol = ls_std::Socket::_convertNetworkProtocol(_networkProtocol);
  79. return socket(addressFamily, socketType, networkProtocol);
  80. }
  81. #endif