| 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 | |
| 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 | IPAddress packet_ip; |
| 52 | int packet_port = 0; |
| 53 | int queue_count = 0; |
| 54 | |
| 55 | IPAddress peer_addr; |
| 56 | int peer_port = 0; |
| 57 | bool connected = false; |
| 58 | bool blocking = true; |
| 59 | bool broadcast = false; |
| 60 | UDPServer *udp_server = nullptr; |
| 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 bind(int p_port, const IPAddress &p_bind_address = IPAddress("*" ), int p_recv_buffer_size = 65536); |
| 74 | void close(); |
| 75 | Error wait(); |
| 76 | bool is_bound() const; |
| 77 | |
| 78 | Error connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer |
| 79 | void disconnect_shared_socket(); // Used by UDPServer |
| 80 | Error store_packet(IPAddress 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 IPAddress &p_host, int p_port); |
| 82 | bool is_socket_connected() const; |
| 83 | |
| 84 | IPAddress get_packet_address() const; |
| 85 | int get_packet_port() const; |
| 86 | int get_local_port() const; |
| 87 | void set_dest_address(const IPAddress &p_address, int p_port); |
| 88 | |
| 89 | Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; |
| 90 | Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; |
| 91 | int get_available_packet_count() const override; |
| 92 | int get_max_packet_size() const override; |
| 93 | void set_broadcast_enabled(bool p_enabled); |
| 94 | Error join_multicast_group(IPAddress p_multi_address, String p_if_name); |
| 95 | Error leave_multicast_group(IPAddress p_multi_address, String p_if_name); |
| 96 | |
| 97 | PacketPeerUDP(); |
| 98 | ~PacketPeerUDP(); |
| 99 | }; |
| 100 | |
| 101 | #endif // PACKET_PEER_UDP_H |
| 102 | |