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_valid(), 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_valid(), 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::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 | err = _sock->bind(p_bind_address, p_port); |
111 | |
112 | if (err != OK) { |
113 | stop(); |
114 | return err; |
115 | } |
116 | return OK; |
117 | } |
118 | |
119 | int UDPServer::get_local_port() const { |
120 | uint16_t local_port; |
121 | _sock->get_socket_address(nullptr, &local_port); |
122 | return local_port; |
123 | } |
124 | |
125 | bool UDPServer::is_listening() const { |
126 | ERR_FAIL_COND_V(!_sock.is_valid(), false); |
127 | |
128 | return _sock->is_open(); |
129 | } |
130 | |
131 | bool UDPServer::is_connection_available() const { |
132 | ERR_FAIL_COND_V(!_sock.is_valid(), false); |
133 | |
134 | if (!_sock->is_open()) { |
135 | return false; |
136 | } |
137 | |
138 | return pending.size() > 0; |
139 | } |
140 | |
141 | void UDPServer::set_max_pending_connections(int p_max) { |
142 | ERR_FAIL_COND_MSG(p_max < 0, "Max pending connections value must be a positive number (0 means refuse new connections)." ); |
143 | max_pending_connections = p_max; |
144 | while (p_max > pending.size()) { |
145 | List<Peer>::Element *E = pending.back(); |
146 | if (!E) { |
147 | break; |
148 | } |
149 | memdelete(E->get().peer); |
150 | pending.erase(E); |
151 | } |
152 | } |
153 | |
154 | int UDPServer::get_max_pending_connections() const { |
155 | return max_pending_connections; |
156 | } |
157 | |
158 | Ref<PacketPeerUDP> UDPServer::take_connection() { |
159 | Ref<PacketPeerUDP> conn; |
160 | if (!is_connection_available()) { |
161 | return conn; |
162 | } |
163 | |
164 | Peer peer = pending[0]; |
165 | pending.pop_front(); |
166 | peers.push_back(peer); |
167 | return peer.peer; |
168 | } |
169 | |
170 | void UDPServer::remove_peer(IPAddress p_ip, int p_port) { |
171 | Peer peer; |
172 | peer.ip = p_ip; |
173 | peer.port = p_port; |
174 | List<Peer>::Element *E = peers.find(peer); |
175 | if (E) { |
176 | peers.erase(E); |
177 | } |
178 | } |
179 | |
180 | void UDPServer::stop() { |
181 | if (_sock.is_valid()) { |
182 | _sock->close(); |
183 | } |
184 | List<Peer>::Element *E = peers.front(); |
185 | while (E) { |
186 | E->get().peer->disconnect_shared_socket(); |
187 | E = E->next(); |
188 | } |
189 | E = pending.front(); |
190 | while (E) { |
191 | E->get().peer->disconnect_shared_socket(); |
192 | memdelete(E->get().peer); |
193 | E = E->next(); |
194 | } |
195 | peers.clear(); |
196 | pending.clear(); |
197 | } |
198 | |
199 | UDPServer::UDPServer() : |
200 | _sock(Ref<NetSocket>(NetSocket::create())) { |
201 | } |
202 | |
203 | UDPServer::~UDPServer() { |
204 | stop(); |
205 | } |
206 | |