socket.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #pragma once
  17. #ifdef _WIN32
  18. #ifndef _WIN32_WINNT
  19. #define _WIN32_WINNT 0x0501
  20. #endif
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #include <ws2tcpip.h>
  24. #else
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #endif
  28. #include <ostream>
  29. #include <cstring>
  30. #include "address.h"
  31. #include "irrlichttypes.h"
  32. #include "networkexceptions.h"
  33. extern bool socket_enable_debug_output;
  34. void sockets_init();
  35. void sockets_cleanup();
  36. class UDPSocket
  37. {
  38. public:
  39. UDPSocket() = default;
  40. UDPSocket(bool ipv6);
  41. ~UDPSocket();
  42. void Bind(Address addr);
  43. bool init(bool ipv6, bool noExceptions = false);
  44. // void Close();
  45. // bool IsOpen();
  46. void Send(const Address &destination, const void *data, int size);
  47. // Returns -1 if there is no data
  48. int Receive(Address &sender, void *data, int size);
  49. int GetHandle(); // For debugging purposes only
  50. void setTimeoutMs(int timeout_ms);
  51. // Returns true if there is data, false if timeout occurred
  52. bool WaitData(int timeout_ms);
  53. private:
  54. int m_handle;
  55. int m_timeout_ms;
  56. int m_addr_family;
  57. };