udp_server.cpp 6.6 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) 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. #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. Error err;
  91. IP::Type ip_type = IP::TYPE_ANY;
  92. if (p_bind_address.is_valid()) {
  93. ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  94. }
  95. err = _sock->open(NetSocket::TYPE_UDP, ip_type);
  96. if (err != OK) {
  97. return ERR_CANT_CREATE;
  98. }
  99. _sock->set_blocking_enabled(false);
  100. _sock->set_reuse_address_enabled(true);
  101. err = _sock->bind(p_bind_address, p_port);
  102. if (err != OK) {
  103. stop();
  104. return err;
  105. }
  106. return OK;
  107. }
  108. int UDPServer::get_local_port() const {
  109. uint16_t local_port;
  110. _sock->get_socket_address(nullptr, &local_port);
  111. return local_port;
  112. }
  113. bool UDPServer::is_listening() const {
  114. ERR_FAIL_COND_V(!_sock.is_valid(), false);
  115. return _sock->is_open();
  116. }
  117. bool UDPServer::is_connection_available() const {
  118. ERR_FAIL_COND_V(!_sock.is_valid(), false);
  119. if (!_sock->is_open()) {
  120. return false;
  121. }
  122. return pending.size() > 0;
  123. }
  124. void UDPServer::set_max_pending_connections(int p_max) {
  125. ERR_FAIL_COND_MSG(p_max < 0, "Max pending connections value must be a positive number (0 means refuse new connections).");
  126. max_pending_connections = p_max;
  127. while (p_max > pending.size()) {
  128. List<Peer>::Element *E = pending.back();
  129. if (!E) {
  130. break;
  131. }
  132. memdelete(E->get().peer);
  133. pending.erase(E);
  134. }
  135. }
  136. int UDPServer::get_max_pending_connections() const {
  137. return max_pending_connections;
  138. }
  139. Ref<PacketPeerUDP> UDPServer::take_connection() {
  140. Ref<PacketPeerUDP> conn;
  141. if (!is_connection_available()) {
  142. return conn;
  143. }
  144. Peer peer = pending.front()->get();
  145. pending.pop_front();
  146. peers.push_back(peer);
  147. return peer.peer;
  148. }
  149. void UDPServer::remove_peer(IPAddress p_ip, int p_port) {
  150. Peer peer;
  151. peer.ip = p_ip;
  152. peer.port = p_port;
  153. List<Peer>::Element *E = peers.find(peer);
  154. if (E) {
  155. peers.erase(E);
  156. }
  157. }
  158. void UDPServer::stop() {
  159. if (_sock.is_valid()) {
  160. _sock->close();
  161. }
  162. List<Peer>::Element *E = peers.front();
  163. while (E) {
  164. E->get().peer->disconnect_shared_socket();
  165. E = E->next();
  166. }
  167. E = pending.front();
  168. while (E) {
  169. E->get().peer->disconnect_shared_socket();
  170. memdelete(E->get().peer);
  171. E = E->next();
  172. }
  173. peers.clear();
  174. pending.clear();
  175. }
  176. UDPServer::UDPServer() :
  177. _sock(Ref<NetSocket>(NetSocket::create())) {
  178. }
  179. UDPServer::~UDPServer() {
  180. stop();
  181. }