Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_tcp_server.cpp
45997 views
1
/**************************************************************************/
2
/* test_tcp_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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_tcp_server)
34
35
#include "core/config/project_settings.h"
36
#include "core/io/stream_peer_tcp.h"
37
#include "core/io/tcp_server.h"
38
#include "core/os/os.h"
39
40
#include <functional>
41
42
namespace TestTCPServer {
43
44
const int PORT = 12345;
45
const IPAddress LOCALHOST("127.0.0.1");
46
const uint32_t SLEEP_DURATION = 1000;
47
const uint64_t MAX_WAIT_USEC = 2000000;
48
49
void wait_for_condition(std::function<bool()> f_test) {
50
const uint64_t time = OS::get_singleton()->get_ticks_usec();
51
while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
52
OS::get_singleton()->delay_usec(SLEEP_DURATION);
53
}
54
}
55
56
Ref<TCPServer> create_server(const IPAddress &p_address, int p_port) {
57
Ref<TCPServer> server;
58
server.instantiate();
59
60
REQUIRE_EQ(server->listen(PORT, LOCALHOST), Error::OK);
61
REQUIRE(server->is_listening());
62
CHECK_FALSE(server->is_connection_available());
63
64
return server;
65
}
66
67
Ref<StreamPeerTCP> create_client(const IPAddress &p_address, int p_port) {
68
Ref<StreamPeerTCP> client;
69
client.instantiate();
70
71
REQUIRE_EQ(client->connect_to_host(LOCALHOST, PORT), Error::OK);
72
CHECK_EQ(client->get_connected_host(), LOCALHOST);
73
CHECK_EQ(client->get_connected_port(), PORT);
74
CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTING);
75
76
return client;
77
}
78
79
Ref<StreamPeerTCP> accept_connection(Ref<TCPServer> &p_server) {
80
wait_for_condition([&]() {
81
return p_server->is_connection_available();
82
});
83
84
REQUIRE(p_server->is_connection_available());
85
Ref<StreamPeerTCP> client_from_server = p_server->take_connection();
86
REQUIRE(client_from_server.is_valid());
87
CHECK_EQ(client_from_server->get_connected_host(), LOCALHOST);
88
CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_CONNECTED);
89
90
return client_from_server;
91
}
92
93
TEST_CASE("[TCPServer] Instantiation") {
94
Ref<TCPServer> server;
95
server.instantiate();
96
97
REQUIRE(server.is_valid());
98
CHECK_EQ(false, server->is_listening());
99
}
100
101
TEST_CASE("[TCPServer] Accept a connection and receive/send data") {
102
Ref<TCPServer> server = create_server(LOCALHOST, PORT);
103
Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
104
Ref<StreamPeerTCP> client_from_server = accept_connection(server);
105
106
wait_for_condition([&]() {
107
return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
108
});
109
110
CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
111
112
// Sending data from client to server.
113
const String hello_world = "Hello World!";
114
client->put_string(hello_world);
115
CHECK_EQ(client_from_server->get_string(), hello_world);
116
117
// Sending data from server to client.
118
const float pi = 3.1415;
119
client_from_server->put_float(pi);
120
CHECK_EQ(client->get_float(), pi);
121
122
client->disconnect_from_host();
123
server->stop();
124
CHECK_FALSE(server->is_listening());
125
}
126
127
TEST_CASE("[TCPServer] Handle multiple clients at the same time") {
128
Ref<TCPServer> server = create_server(LOCALHOST, PORT);
129
130
Vector<Ref<StreamPeerTCP>> clients;
131
for (int i = 0; i < 5; i++) {
132
clients.push_back(create_client(LOCALHOST, PORT));
133
}
134
135
Vector<Ref<StreamPeerTCP>> clients_from_server;
136
for (int i = 0; i < clients.size(); i++) {
137
clients_from_server.push_back(accept_connection(server));
138
}
139
140
wait_for_condition([&]() {
141
bool should_exit = true;
142
for (Ref<StreamPeerTCP> &c : clients) {
143
if (c->poll() != Error::OK) {
144
return true;
145
}
146
StreamPeerTCP::Status status = c->get_status();
147
if (status != StreamPeerTCP::STATUS_CONNECTED && status != StreamPeerTCP::STATUS_CONNECTING) {
148
return true;
149
}
150
if (status != StreamPeerTCP::STATUS_CONNECTED) {
151
should_exit = false;
152
}
153
}
154
return should_exit;
155
});
156
157
for (Ref<StreamPeerTCP> &c : clients) {
158
REQUIRE_EQ(c->get_status(), StreamPeerTCP::STATUS_CONNECTED);
159
}
160
161
// Sending data from each client to server.
162
for (int i = 0; i < clients.size(); i++) {
163
String hello_client = "Hello " + itos(i);
164
clients[i]->put_string(hello_client);
165
CHECK_EQ(clients_from_server[i]->get_string(), hello_client);
166
}
167
168
for (Ref<StreamPeerTCP> &c : clients) {
169
c->disconnect_from_host();
170
}
171
server->stop();
172
}
173
174
TEST_CASE("[TCPServer] When stopped shouldn't accept new connections") {
175
Ref<TCPServer> server = create_server(LOCALHOST, PORT);
176
Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
177
Ref<StreamPeerTCP> client_from_server = accept_connection(server);
178
179
wait_for_condition([&]() {
180
return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
181
});
182
183
CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
184
185
// Sending data from client to server.
186
const String hello_world = "Hello World!";
187
client->put_string(hello_world);
188
CHECK_EQ(client_from_server->get_string(), hello_world);
189
190
client->disconnect_from_host();
191
server->stop();
192
CHECK_FALSE(server->is_listening());
193
194
// Make sure the client times out in less than the wait time.
195
int timeout = ProjectSettings::get_singleton()->get_setting("network/limits/tcp/connect_timeout_seconds");
196
ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", 1);
197
198
Ref<StreamPeerTCP> new_client = create_client(LOCALHOST, PORT);
199
200
// Reset the timeout setting.
201
ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", timeout);
202
203
CHECK_FALSE(server->is_connection_available());
204
205
wait_for_condition([&]() {
206
return new_client->poll() != Error::OK || new_client->get_status() == StreamPeerTCP::STATUS_ERROR;
207
});
208
209
CHECK_FALSE(server->is_connection_available());
210
211
CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_ERROR);
212
new_client->disconnect_from_host();
213
CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_NONE);
214
}
215
216
TEST_CASE("[TCPServer] Should disconnect client") {
217
Ref<TCPServer> server = create_server(LOCALHOST, PORT);
218
Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
219
Ref<StreamPeerTCP> client_from_server = accept_connection(server);
220
221
wait_for_condition([&]() {
222
return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
223
});
224
225
CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
226
227
// Sending data from client to server.
228
const String hello_world = "Hello World!";
229
client->put_string(hello_world);
230
CHECK_EQ(client_from_server->get_string(), hello_world);
231
232
client_from_server->disconnect_from_host();
233
server->stop();
234
CHECK_FALSE(server->is_listening());
235
236
// Wait for disconnection
237
wait_for_condition([&]() {
238
return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_NONE;
239
});
240
241
// Wait for disconnection
242
wait_for_condition([&]() {
243
return client_from_server->poll() != Error::OK || client_from_server->get_status() == StreamPeerTCP::STATUS_NONE;
244
});
245
246
CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_NONE);
247
CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_NONE);
248
249
ERR_PRINT_OFF;
250
CHECK_EQ(client->get_string(), String());
251
CHECK_EQ(client_from_server->get_string(), String());
252
ERR_PRINT_ON;
253
}
254
255
} // namespace TestTCPServer
256
257