test_udp_server.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* test_udp_server.h */
  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. #ifndef TEST_UDP_SERVER_H
  31. #define TEST_UDP_SERVER_H
  32. #include "core/io/packet_peer_udp.h"
  33. #include "core/io/udp_server.h"
  34. #include "tests/test_macros.h"
  35. namespace TestUDPServer {
  36. const int PORT = 12345;
  37. const IPAddress LOCALHOST("127.0.0.1");
  38. const uint32_t SLEEP_DURATION = 1000;
  39. const uint64_t MAX_WAIT_USEC = 100000;
  40. void wait_for_condition(std::function<bool()> f_test) {
  41. const uint64_t time = OS::get_singleton()->get_ticks_usec();
  42. while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  43. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  44. }
  45. }
  46. Ref<UDPServer> create_server(const IPAddress &p_address, int p_port) {
  47. Ref<UDPServer> server;
  48. server.instantiate();
  49. Error err = server->listen(PORT, LOCALHOST);
  50. REQUIRE_EQ(Error::OK, err);
  51. REQUIRE(server->is_listening());
  52. CHECK_FALSE(server->is_connection_available());
  53. CHECK_EQ(server->get_max_pending_connections(), 16);
  54. return server;
  55. }
  56. Ref<PacketPeerUDP> create_client(const IPAddress &p_address, int p_port) {
  57. Ref<PacketPeerUDP> client;
  58. client.instantiate();
  59. Error err = client->connect_to_host(LOCALHOST, PORT);
  60. REQUIRE_EQ(Error::OK, err);
  61. CHECK(client->is_bound());
  62. CHECK(client->is_socket_connected());
  63. return client;
  64. }
  65. Ref<PacketPeerUDP> accept_connection(Ref<UDPServer> &p_server) {
  66. wait_for_condition([&]() {
  67. return p_server->poll() != Error::OK || p_server->is_connection_available();
  68. });
  69. CHECK_EQ(p_server->poll(), Error::OK);
  70. REQUIRE(p_server->is_connection_available());
  71. Ref<PacketPeerUDP> client_from_server = p_server->take_connection();
  72. REQUIRE(client_from_server.is_valid());
  73. CHECK(client_from_server->is_bound());
  74. CHECK(client_from_server->is_socket_connected());
  75. return client_from_server;
  76. }
  77. TEST_CASE("[UDPServer] Instantiation") {
  78. Ref<UDPServer> server;
  79. server.instantiate();
  80. REQUIRE(server.is_valid());
  81. CHECK_EQ(false, server->is_listening());
  82. }
  83. TEST_CASE("[UDPServer] Accept a connection and receive/send data") {
  84. Ref<UDPServer> server = create_server(LOCALHOST, PORT);
  85. Ref<PacketPeerUDP> client = create_client(LOCALHOST, PORT);
  86. // Sending data from client to server.
  87. const String hello_world = "Hello World!";
  88. CHECK_EQ(client->put_var(hello_world), Error::OK);
  89. Variant hello_world_received;
  90. Ref<PacketPeerUDP> client_from_server = accept_connection(server);
  91. CHECK_EQ(client_from_server->get_var(hello_world_received), Error::OK);
  92. CHECK_EQ(String(hello_world_received), hello_world);
  93. // Sending data from server to client.
  94. const Variant pi = 3.1415;
  95. CHECK_EQ(client_from_server->put_var(pi), Error::OK);
  96. wait_for_condition([&]() {
  97. return client->get_available_packet_count() > 0;
  98. });
  99. CHECK_GT(client->get_available_packet_count(), 0);
  100. Variant pi_received;
  101. CHECK_EQ(client->get_var(pi_received), Error::OK);
  102. CHECK_EQ(pi_received, pi);
  103. client->close();
  104. server->stop();
  105. CHECK_FALSE(server->is_listening());
  106. }
  107. TEST_CASE("[UDPServer] Handle multiple clients at the same time") {
  108. Ref<UDPServer> server = create_server(LOCALHOST, PORT);
  109. Vector<Ref<PacketPeerUDP>> clients;
  110. for (int i = 0; i < 5; i++) {
  111. Ref<PacketPeerUDP> c = create_client(LOCALHOST, PORT);
  112. // Sending data from client to server.
  113. const String hello_client = itos(i);
  114. CHECK_EQ(c->put_var(hello_client), Error::OK);
  115. clients.push_back(c);
  116. }
  117. Array packets;
  118. for (int i = 0; i < clients.size(); i++) {
  119. Ref<PacketPeerUDP> cfs = accept_connection(server);
  120. Variant received_var;
  121. CHECK_EQ(cfs->get_var(received_var), Error::OK);
  122. CHECK_EQ(received_var.get_type(), Variant::STRING);
  123. packets.push_back(received_var);
  124. // Sending data from server to client.
  125. const float sent_float = 3.1415 + received_var.operator String().to_float();
  126. CHECK_EQ(cfs->put_var(sent_float), Error::OK);
  127. }
  128. CHECK_EQ(packets.size(), clients.size());
  129. packets.sort();
  130. for (int i = 0; i < clients.size(); i++) {
  131. CHECK_EQ(packets[i].operator String(), itos(i));
  132. }
  133. wait_for_condition([&]() {
  134. bool should_exit = true;
  135. for (Ref<PacketPeerUDP> &c : clients) {
  136. int count = c->get_available_packet_count();
  137. if (count < 0) {
  138. return true;
  139. }
  140. if (count == 0) {
  141. should_exit = false;
  142. }
  143. }
  144. return should_exit;
  145. });
  146. for (int i = 0; i < clients.size(); i++) {
  147. CHECK_GT(clients[i]->get_available_packet_count(), 0);
  148. Variant received_var;
  149. const float expected = 3.1415 + i;
  150. CHECK_EQ(clients[i]->get_var(received_var), Error::OK);
  151. CHECK_EQ(received_var.get_type(), Variant::FLOAT);
  152. CHECK_EQ(received_var.operator float(), expected);
  153. }
  154. for (Ref<PacketPeerUDP> &c : clients) {
  155. c->close();
  156. }
  157. server->stop();
  158. }
  159. TEST_CASE("[UDPServer] Should not accept new connections after stop") {
  160. Ref<UDPServer> server = create_server(LOCALHOST, PORT);
  161. Ref<PacketPeerUDP> client = create_client(LOCALHOST, PORT);
  162. // Sending data from client to server.
  163. const String hello_world = "Hello World!";
  164. CHECK_EQ(client->put_var(hello_world), Error::OK);
  165. wait_for_condition([&]() {
  166. return server->poll() != Error::OK || server->is_connection_available();
  167. });
  168. REQUIRE(server->is_connection_available());
  169. server->stop();
  170. CHECK_FALSE(server->is_listening());
  171. CHECK_FALSE(server->is_connection_available());
  172. }
  173. } // namespace TestUDPServer
  174. #endif // TEST_UDP_SERVER_H