Skip to content

Commit b59d236

Browse files
committed
async connect
1 parent d62479c commit b59d236

File tree

1 file changed

+17
-31
lines changed
  • examples/clientServerLifeSimulator_004/client

1 file changed

+17
-31
lines changed

examples/clientServerLifeSimulator_004/client/main.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
153153
"waiting for server", "default", fge::Vector2f{}, 15);
154154
latencyText->setFillColor(fge::Color::Black);
155155

156-
bool connectionValid = false;
157-
158156
//Lambda that create a GUI window for connection
159157
auto createConnectionWindow = [&]() {
160158
//First check if the window already exists
@@ -203,27 +201,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
203201

204202
isConnecting = true;
205203
futureConnect = server.connect();
206-
futureConnect.wait(); //TODO: remove this wait
207-
isConnecting = false;
208-
209-
if (!futureConnect.get())
210-
{
211-
server.stop();
212-
std::cout << "can't connect to the server !" << std::endl;
213-
return;
214-
}
215-
216-
std::cout << "connection ok" << std::endl;
217-
218-
server.enableReturnPacket(true);
219-
220-
auto transmissionPacket = fge::net::CreatePacket(ls::LS_PROTOCOL_C_PLEASE_CONNECT_ME);
221-
transmissionPacket->doNotDiscard().doNotReorder() << LIFESIM_CONNECTION_TEXT1 << LIFESIM_CONNECTION_TEXT2;
222-
223-
//Ask the server thread to automatically update the timestamp just before sending it
224-
server._client._latencyPlanner.pack(transmissionPacket);
225-
226-
server._client.pushPacket(std::move(transmissionPacket));
227204
});
228205
};
229206

@@ -240,8 +217,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
240217
server._onClientTimeout.addLambda([&]([[maybe_unused]] fge::net::ClientSideNetUdp& client) {
241218
std::cout << "connection lost ! (timeout)" << std::endl;
242219

243-
connectionValid = false;
244-
245220
server.stop();
246221
mainScene->delAllObject(true);
247222
createConnectionWindow();
@@ -275,17 +250,30 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
275250
//Check if the connection is unsuccessful
276251
if (isConnecting && futureConnect.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready)
277252
{
253+
isConnecting = false;
254+
278255
if (!futureConnect.get())
279256
{
280-
std::cout << "no response (timeout) !" << std::endl;
281-
282-
connectionValid = false;
257+
std::cout << "can't connect to the server !" << std::endl;
283258

284259
server.stop();
285260
mainScene->delAllObject(true);
286261
createConnectionWindow();
262+
}
263+
else
264+
{
265+
std::cout << "connection ok" << std::endl;
266+
267+
server.enableReturnPacket(true);
287268

288-
isConnecting = false;
269+
auto transmissionPacket = fge::net::CreatePacket(ls::LS_PROTOCOL_C_PLEASE_CONNECT_ME);
270+
transmissionPacket->doNotDiscard().doNotReorder()
271+
<< LIFESIM_CONNECTION_TEXT1 << LIFESIM_CONNECTION_TEXT2;
272+
273+
//Ask the server thread to automatically update the timestamp just before sending it
274+
server._client._latencyPlanner.pack(transmissionPacket);
275+
276+
server._client.pushPacket(std::move(transmissionPacket));
289277
}
290278
}
291279

@@ -330,8 +318,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
330318
{
331319
mainScene->delObject(windowObject->getSid());
332320
}
333-
connectionValid = true;
334-
335321
std::cout << "connected to server !" << std::endl;
336322
}
337323
else

0 commit comments

Comments
 (0)