packet_peer_udp.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**************************************************************************/
  2. /* packet_peer_udp.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PACKET_PEER_UDP_H
  31. #define PACKET_PEER_UDP_H
  32. #include "core/io/ip.h"
  33. #include "core/io/net_socket.h"
  34. #include "core/io/packet_peer.h"
  35. class UDPServer;
  36. class PacketPeerUDP : public PacketPeer {
  37. GDCLASS(PacketPeerUDP, PacketPeer);
  38. protected:
  39. enum {
  40. PACKET_BUFFER_SIZE = 65536
  41. };
  42. RingBuffer<uint8_t> rb;
  43. uint8_t recv_buffer[PACKET_BUFFER_SIZE];
  44. uint8_t packet_buffer[PACKET_BUFFER_SIZE];
  45. IPAddress packet_ip;
  46. int packet_port = 0;
  47. int queue_count = 0;
  48. IPAddress peer_addr;
  49. int peer_port = 0;
  50. bool connected = false;
  51. bool blocking = true;
  52. bool broadcast = false;
  53. UDPServer *udp_server = nullptr;
  54. Ref<NetSocket> _sock;
  55. static void _bind_methods();
  56. String _get_packet_ip() const;
  57. Error _set_dest_address(const String &p_address, int p_port);
  58. Error _poll();
  59. public:
  60. void set_blocking_mode(bool p_enable);
  61. Error bind(int p_port, const IPAddress &p_bind_address = IPAddress("*"), int p_recv_buffer_size = 65536);
  62. void close();
  63. Error wait();
  64. bool is_bound() const;
  65. Error connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer
  66. void disconnect_shared_socket(); // Used by UDPServer
  67. Error store_packet(IPAddress p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); // Used internally and by UDPServer
  68. Error connect_to_host(const IPAddress &p_host, int p_port);
  69. bool is_socket_connected() const;
  70. IPAddress get_packet_address() const;
  71. int get_packet_port() const;
  72. int get_local_port() const;
  73. void set_dest_address(const IPAddress &p_address, int p_port);
  74. Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  75. Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  76. int get_available_packet_count() const override;
  77. int get_max_packet_size() const override;
  78. void set_broadcast_enabled(bool p_enabled);
  79. Error join_multicast_group(IPAddress p_multi_address, String p_if_name);
  80. Error leave_multicast_group(IPAddress p_multi_address, String p_if_name);
  81. PacketPeerUDP();
  82. ~PacketPeerUDP();
  83. };
  84. #endif // PACKET_PEER_UDP_H