udp_server.cpp 6.7 KB

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