Debian Codegraph @

core/io/udp_server.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* udp_server.cpp */
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 #include "udp_server.h"
32
33 void UDPServer::_bind_methods() {
34 ClassDB::bind_method(D_METHOD("listen", "port", "bind_address"), &UDPServer::listen, DEFVAL("*"));
35 ClassDB::bind_method(D_METHOD("poll"), &UDPServer::poll);
36 ClassDB::bind_method(D_METHOD("is_connection_available"), &UDPServer::is_connection_available);
37 ClassDB::bind_method(D_METHOD("get_local_port"), &UDPServer::get_local_port);
38 ClassDB::bind_method(D_METHOD("is_listening"), &UDPServer::is_listening);
39 ClassDB::bind_method(D_METHOD("take_connection"), &UDPServer::take_connection);
40 ClassDB::bind_method(D_METHOD("stop"), &UDPServer::stop);
41 ClassDB::bind_method(D_METHOD("set_max_pending_connections", "max_pending_connections"), &UDPServer::set_max_pending_connections);
42 ClassDB::bind_method(D_METHOD("get_max_pending_connections"), &UDPServer::get_max_pending_connections);
43 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_pending_connections", PROPERTY_HINT_RANGE, "0,256,1"), "set_max_pending_connections", "get_max_pending_connections");
44 }
45
46 Error UDPServer::poll() {
47 ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
48 if (!_sock->is_open()) {
49 return ERR_UNCONFIGURED;
50 }
51 Error err;
52 int read;
53 IPAddress ip;
54 uint16_t port;
55 while (true) {
56 err = _sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, ip, port);
57 if (err != OK) {
58 if (err == ERR_BUSY) {
59 break;
60 }
61 return FAILED;
62 }
63 Peer p;
64 p.ip = ip;
65 p.port = port;
66 List<Peer>::Element *E = peers.find(p);
67 if (!E) {
68 E = pending.find(p);
69 }
70 if (E) {
71 E->get().peer->store_packet(ip, port, recv_buffer, read);
72 } else {
73 if (pending.size() >= max_pending_connections) {
74 // Drop connection.
75 continue;
76 }
77 // It's a new peer, add it to the pending list.
78 Peer peer;
79 peer.ip = ip;
80 peer.port = port;
81 peer.peer = memnew(PacketPeerUDP);
82 peer.peer->connect_shared_socket(_sock, ip, port, this);
83 peer.peer->store_packet(ip, port, recv_buffer, read);
84 pending.push_back(peer);
85 }
86 }
87 return OK;
88 }
89
90 Error UDPServer::listen(uint16_t p_port, const IPAddress &p_bind_address) {
91 ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
92 ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
93 ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
94
95 Error err;
96 IP::Type ip_type = IP::TYPE_ANY;
97
98 if (p_bind_address.is_valid()) {
99 ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
100 }
101
102 err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type);
103
104 if (err != OK) {
105 return ERR_CANT_CREATE;
106 }
107
108 _sock->set_blocking_enabled(false);
109 _sock->set_reuse_address_enabled(true);
110 NetSocket::Address addr(p_bind_address, p_port);
111 err = _sock->bind(addr);
112
113 if (err != OK) {
114 stop();
115 return err;
116 }
117 return OK;
118 }
119
120 int UDPServer::get_local_port() const {
121 NetSocket::Address addr;
122 _sock->get_socket_address(&addr);
123 return addr.port();
124 }
125
126 bool UDPServer::is_listening() const {
127 ERR_FAIL_COND_V(_sock.is_null(), false);
128
129 return _sock->is_open();
130 }
131
132 bool UDPServer::is_connection_available() const {
133 ERR_FAIL_COND_V(_sock.is_null(), false);
134
135 if (!_sock->is_open()) {
136 return false;
137 }
138
139 return pending.size() > 0;
140 }
141
142 void UDPServer::set_max_pending_connections(int p_max) {
143 ERR_FAIL_COND_MSG(p_max < 0, "Max pending connections value must be a positive number (0 means refuse new connections).");
144 max_pending_connections = p_max;
145 while (p_max > pending.size()) {
146 List<Peer>::Element *E = pending.back();
147 if (!E) {
148 break;
149 }
150 memdelete(E->get().peer);
151 pending.erase(E);
152 }
153 }
154
155 int UDPServer::get_max_pending_connections() const {
156 return max_pending_connections;
157 }
158
159 Ref<PacketPeerUDP> UDPServer::take_connection() {
160 Ref<PacketPeerUDP> conn;
161 if (!is_connection_available()) {
162 return conn;
163 }
164
165 Peer peer = pending.front()->get();
166 pending.pop_front();
167 peers.push_back(peer);
168 return peer.peer;
169 }
170
171 void UDPServer::remove_peer(IPAddress p_ip, int p_port) {
172 Peer peer;
173 peer.ip = p_ip;
174 peer.port = p_port;
175 List<Peer>::Element *E = peers.find(peer);
176 if (E) {
177 peers.erase(E);
178 }
179 }
180
181 void UDPServer::stop() {
182 if (_sock.is_valid()) {
183 _sock->close();
184 }
185 List<Peer>::Element *E = peers.front();
186 while (E) {
187 E->get().peer->disconnect_shared_socket();
188 E = E->next();
189 }
190 E = pending.front();
191 while (E) {
192 E->get().peer->disconnect_shared_socket();
193 memdelete(E->get().peer);
194 E = E->next();
195 }
196 peers.clear();
197 pending.clear();
198 }
199
200 UDPServer::UDPServer() :
201 _sock(Ref<NetSocket>(NetSocket::create())) {
202 }
203
204 UDPServer::~UDPServer() {
205 stop();
206 }