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