-
Notifications
You must be signed in to change notification settings - Fork 7
/
sync_client.cpp
68 lines (56 loc) · 1.96 KB
/
sync_client.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
#include "cecho_conn.h"
#include <knet/kconnector.h>
#include <knet/kworker.h>
#include <knet/kutils.h>
int main(int argc, char** argv)
{
// parse command line
const char* ip = argc > 1 ? argv[1] : "localhost";
const char* port = argc > 2 ? argv[2] : "8888";
const auto client_num = argc > 3 ? std::atoi(argv[3]) : 1;
const auto timeout_ms = argc > 4 ? std::atoi(argv[4]) : -1;
// log parameter info
std::cout << "Hi, KNet(Sync Client)" << std::endl
<< "ip:" << ip << std::endl
<< "port: " << port << std::endl
<< "client_num: " << client_num << std::endl
<< "timeout_ms: " << timeout_ms << std::endl;
// parse ip address
address addr;
if (!address::resolve_one(ip, port, family_t::Ipv4, addr)) {
std::cerr << "resolve address " << ip << ":" << port << " failed!" << std::endl;
return -1;
}
// create worker
echo_conn_factory<cecho_conn> cf;
worker wkr(cf);
// create connector
connector cnctor(wkr);
// check console input
mgr.is_server = false;
mgr.can_log = true;
mgr.check_console_input();
auto last_ms = now_ms();
while (true) {
const auto beg_ms = now_ms();
const auto delta_ms = (beg_ms > last_ms ? beg_ms - last_ms : 0);
last_ms = beg_ms;
wkr.update();
const auto inst_num = mgr.inst_num.load();
if (mgr.disconnect_all) {
if (0 == inst_num) {
break;
}
} else if (inst_num < client_num) {
if (!cnctor.connect(addr, timeout_ms)) {
std::cerr << "connect failed! address: " << addr << std::endl;
}
}
mgr.update(delta_ms);
const auto end_ms = now_ms();
const auto cost_ms = end_ms > beg_ms ? end_ms - beg_ms : 0;
constexpr int64_t min_interval_ms = 50;
sleep_ms(cost_ms < min_interval_ms ? min_interval_ms - cost_ms : 1);
}
return 0;
}