Debian Codegraph @

core/io/packet_peer_udp.h

raw symbols SCIP
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 #pragma once
32
33 #include "core/io/ip.h"
34 #include "core/io/net_socket.h"
35 #include "core/io/packet_peer.h"
36
37 class UDPServer;
38
39 class PacketPeerUDP : public PacketPeer {
40 GDCLASS(PacketPeerUDP, PacketPeer);
41
42 protected:
43 enum {
44 PACKET_BUFFER_SIZE = 65536
45 };
46
47 RingBuffer<uint8_t> rb;
48 uint8_t recv_buffer[PACKET_BUFFER_SIZE];
49 uint8_t packet_buffer[PACKET_BUFFER_SIZE];
50 IPAddress packet_ip;
51 int packet_port = 0;
52 int queue_count = 0;
53
54 IPAddress peer_addr;
55 int peer_port = 0;
56 bool connected = false;
57 bool blocking = true;
58 bool broadcast = false;
59 UDPServer *udp_server = nullptr;
60 Ref<NetSocket> _sock;
61
62 static void _bind_methods();
63
64 String _get_packet_ip() const;
65
66 Error _set_dest_address(const String &p_address, int p_port);
67 Error _poll();
68
69 public:
70 void set_blocking_mode(bool p_enable);
71
72 Error bind(int p_port, const IPAddress &p_bind_address = IPAddress("*"), int p_recv_buffer_size = 65536);
73 void close();
74 Error wait();
75 bool is_bound() const;
76
77 Error connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer
78 void disconnect_shared_socket(); // Used by UDPServer
79 Error store_packet(IPAddress p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); // Used internally and by UDPServer
80 Error connect_to_host(const IPAddress &p_host, int p_port);
81 bool is_socket_connected() const;
82
83 IPAddress get_packet_address() const;
84 int get_packet_port() const;
85 int get_local_port() const;
86 void set_dest_address(const IPAddress &p_address, int p_port);
87
88 Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
89 Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
90 int get_available_packet_count() const override;
91 int get_max_packet_size() const override;
92 void set_broadcast_enabled(bool p_enabled);
93 Error join_multicast_group(IPAddress p_multi_address, const String &p_if_name);
94 Error leave_multicast_group(IPAddress p_multi_address, const String &p_if_name);
95
96 PacketPeerUDP();
97 ~PacketPeerUDP();
98 };