1 /*************************************************************************/ 2 /* packet_peer_udp.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ 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 31 #ifndef PACKET_PEER_UDP_H 32 #define PACKET_PEER_UDP_H 33 34 #include "core/io/ip.h" 35 #include "core/io/net_socket.h" 36 #include "core/io/packet_peer.h" 37 38 class UDPServer; 39 40 class PacketPeerUDP : public PacketPeer { 41 GDCLASS(PacketPeerUDP, PacketPeer); 42 43 protected: 44 enum { 45 PACKET_BUFFER_SIZE = 65536 46 }; 47 48 RingBuffer<uint8_t> rb; 49 uint8_t recv_buffer[PACKET_BUFFER_SIZE]; 50 uint8_t packet_buffer[PACKET_BUFFER_SIZE]; 51 IP_Address packet_ip; 52 int packet_port; 53 int queue_count; 54 55 IP_Address peer_addr; 56 int peer_port; 57 bool connected; 58 bool blocking; 59 bool broadcast; 60 UDPServer *udp_server; 61 Ref<NetSocket> _sock; 62 63 static void _bind_methods(); 64 65 String _get_packet_ip() const; 66 67 Error _set_dest_address(const String &p_address, int p_port); 68 Error _poll(); 69 70 public: 71 void set_blocking_mode(bool p_enable); 72 73 Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536); 74 void close(); 75 Error wait(); 76 bool is_listening() const; 77 78 Error connect_shared_socket(Ref<NetSocket> p_sock, IP_Address p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer 79 void disconnect_shared_socket(); // Used by UDPServer 80 Error store_packet(IP_Address p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); // Used internally and by UDPServer 81 Error connect_to_host(const IP_Address &p_host, int p_port); 82 bool is_connected_to_host() const; 83 84 IP_Address get_packet_address() const; 85 int get_packet_port() const; 86 void set_dest_address(const IP_Address &p_address, int p_port); 87 88 Error put_packet(const uint8_t *p_buffer, int p_buffer_size); 89 Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); 90 int get_available_packet_count() const; 91 int get_max_packet_size() const; 92 void set_broadcast_enabled(bool p_enabled); 93 Error join_multicast_group(IP_Address p_multi_address, String p_if_name); 94 Error leave_multicast_group(IP_Address p_multi_address, String p_if_name); 95 96 PacketPeerUDP(); 97 ~PacketPeerUDP(); 98 }; 99 100 #endif // PACKET_PEER_UDP_H 101