-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleClient.cpp
80 lines (71 loc) · 2.14 KB
/
SimpleClient.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include "CustomClient.h"
int main()
{
CustomClient c;
c.Connect("127.0.0.1", 60000);
bool bQuit = false;
int option, old_option;
std::thread keyReader([&option, &bQuit]()
{
while(!bQuit)
{
std::cout << "[1].Ping\n"
"[2].MessageAll\n"
"[99].Quit\n";
std::cin >> option;
}
});
//No reason to wait for the thread to stop
keyReader.detach();
do
{
if (option == 1 && old_option == option)
{
c.PingServer();
option = 0;
}
else if (option == 2 && old_option == option)
{
c.MessageAll();
option = 0;
}
if (c.IsConnected())
{
if (!c.Incoming().empty())
{
auto msg = c.Incoming().pop_front().msg;
switch (msg.header.id)
{
case CustomMsgTypes::ServerAccept:
{
std::cout << "Server Acepted Connection\n";
break;
}
case CustomMsgTypes::ServerPing:
{
std::chrono::system_clock::time_point timeNow = std::chrono::system_clock::now();
std::chrono::system_clock::time_point timeThen;
msg >> timeThen;
std::cout << "Ping: " << std::chrono::duration<double>(timeNow - timeThen).count() << "\n";
break;
}
case CustomMsgTypes::ServerMessage:
{
uint32_t clientID;
msg >> clientID;
std::cout << "Message from: " << clientID << "\n";
break;
}
}
}
}
else
{
std::cout << "The Server was thunderstruck!\n";
option = 99;
}
old_option = option;
} while (option != 99);
bQuit = true;
}