-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechoClient.cpp
61 lines (50 loc) · 2.12 KB
/
echoClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <atomic>
#include <thread>
#include <cassert>
#include "polymorph/network/tcp/Client.hpp"
#include "MessageDto.hpp"
int main(int ac, char **av)
{
using namespace polymorph::network;
using namespace polymorph::network::tcp;
if (ac != 3) {
std::cerr << "Usage: " << av[0] << " <host> <port>" << std::endl;
return 1;
}
// we create a client and bind its connector
auto client = Client::create(av[1], std::stoi(av[2]));
// check variables for the example
std::atomic<bool> received(false);
std::atomic<bool> connected(false);
MessageDto dto { .message = "Hello World!" };
// we register a callback that will handle the received MessageDto
client->registerReceiveHandler<MessageDto>(MessageDto::opId, [&received](const PacketHeader &, const MessageDto &payload) {
received = true;
std::cout << "Received: " << payload.message << std::endl;
return true; // The received data is correct, we do not want to disconnect the client
});
// Then we connect to the server and pass a callback that will be called when the connection is established
client->connect([&connected](bool authorized, SessionId) {
if (authorized) {
std::cout << "Connected" << std::endl;
connected = true;
} else {
std::cout << "Connection failed" << std::endl;
}
});
// we wait for the connection to be established or the timeout to be reached (5 * 100ms)
int connected_max_tries = 5;
while (!connected && --connected_max_tries > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
assert(connected); // abort if we couldn't connect
// we send the MessageDto to the server
client->send<MessageDto>(MessageDto::opId, dto, [](const PacketHeader &header, const MessageDto &payload) {
std::cout << "Message has been sent" << std::endl;
});
// we wait the registered callback to be called
while(!received)
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // sleep until we received the echoed message
return 0;
}