-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_cli_main.cpp
55 lines (52 loc) · 1.66 KB
/
tcp_cli_main.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
#include <iostream>
#include <string>
#include <thread>
#include <unordered_map>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <chrono>
#include "protocol.h"
#include "nio.h"
using namespace ::droplet;
int run_get_device_list(int index) {
int conn_fd = Nio::connect_socket("127.0.0.1", 22222, 3000);
if (conn_fd <= 0) {
std::cout << "connect_socket Error. conn_fd: " << conn_fd << std::endl;
return conn_fd;
}
for (size_t i=0; i<100; i++) {
auto start_t = std::chrono::system_clock::now();
RpcMessage req_msg;
RpcMessage resp_data;
req_msg.head.msg_type = MsgType::shutdown;
int ret_code = req_msg.send_data(conn_fd);
if (0 != ret_code) {
std::cout << "send_tcp_msg ret_code: " << ret_code << std::endl;
close(conn_fd);
return 101;
}
ret_code = resp_data.recv_data(conn_fd);
if (0 != ret_code) {
std::cout << "recv_msg ret_code: " << ret_code << std::endl;
close(conn_fd);
return 102;
}
auto end_t = std::chrono::system_clock::now();
int diff_t = std::chrono::duration_cast<std::chrono::milliseconds>(end_t-start_t).count();
std::cout << std::to_string(i) + "," + std::to_string(resp_data.head.length) + "," + std::to_string(diff_t) << std::endl;
}
close(conn_fd);
return 0;
}
int main() {
int sum = 5;
std::vector<std::thread> thread_vec;
for (size_t i=0; i<sum; i++) {
thread_vec.emplace_back(std::thread(run_get_device_list, i));
}
for (size_t i=0; i<sum; i++) {
thread_vec[i].join();
}
return 0;
}