socket.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef SOCKET_HEADER
  17. #define SOCKET_HEADER
  18. #ifdef _WIN32
  19. #ifndef WIN32_LEAN_AND_MEAN
  20. #define WIN32_LEAN_AND_MEAN
  21. #endif
  22. #ifndef _WIN32_WINNT
  23. #define _WIN32_WINNT 0x0501
  24. #endif
  25. #include <windows.h>
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #else
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #endif
  32. #include <ostream>
  33. #include <string.h>
  34. #include "irrlichttypes.h"
  35. #include "exceptions.h"
  36. extern bool socket_enable_debug_output;
  37. class SocketException : public BaseException
  38. {
  39. public:
  40. SocketException(const char *s):
  41. BaseException(s)
  42. {
  43. }
  44. };
  45. class ResolveError : public BaseException
  46. {
  47. public:
  48. ResolveError(const char *s):
  49. BaseException(s)
  50. {
  51. }
  52. };
  53. class SendFailedException : public BaseException
  54. {
  55. public:
  56. SendFailedException(const char *s):
  57. BaseException(s)
  58. {
  59. }
  60. };
  61. void sockets_init();
  62. void sockets_cleanup();
  63. class IPv6AddressBytes
  64. {
  65. public:
  66. u8 bytes[16];
  67. IPv6AddressBytes() { memset(bytes, 0, 16); }
  68. };
  69. class Address
  70. {
  71. public:
  72. Address();
  73. Address(u32 address, u16 port);
  74. Address(u8 a, u8 b, u8 c, u8 d, u16 port);
  75. Address(const IPv6AddressBytes * ipv6_bytes, u16 port);
  76. bool operator==(Address &address);
  77. bool operator!=(Address &address);
  78. // Resolve() may throw ResolveError (address is unchanged in this case)
  79. void Resolve(const char *name);
  80. struct sockaddr_in getAddress() const;
  81. unsigned short getPort() const;
  82. void setAddress(u32 address);
  83. void setAddress(u8 a, u8 b, u8 c, u8 d);
  84. void setAddress(const IPv6AddressBytes * ipv6_bytes);
  85. struct sockaddr_in6 getAddress6() const;
  86. int getFamily() const;
  87. bool isIPv6() const;
  88. bool isZero() const;
  89. void setPort(unsigned short port);
  90. void print(std::ostream *s) const;
  91. std::string serializeString() const;
  92. private:
  93. unsigned int m_addr_family;
  94. union
  95. {
  96. struct sockaddr_in ipv4;
  97. struct sockaddr_in6 ipv6;
  98. } m_address;
  99. u16 m_port; // Port is separate from sockaddr structures
  100. };
  101. class UDPSocket
  102. {
  103. public:
  104. UDPSocket(bool ipv6);
  105. ~UDPSocket();
  106. void Bind(Address addr);
  107. //void Close();
  108. //bool IsOpen();
  109. void Send(const Address & destination, const void * data, int size);
  110. // Returns -1 if there is no data
  111. int Receive(Address & sender, void * data, int size);
  112. int GetHandle(); // For debugging purposes only
  113. void setTimeoutMs(int timeout_ms);
  114. // Returns true if there is data, false if timeout occurred
  115. bool WaitData(int timeout_ms);
  116. private:
  117. int m_handle;
  118. int m_timeout_ms;
  119. int m_addr_family;
  120. };
  121. #endif