-
Notifications
You must be signed in to change notification settings - Fork 84
/
tcpclient.cc
97 lines (83 loc) · 2.33 KB
/
tcpclient.cc
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
struct ClientConf
{
static const uint32_t RecvBufSize = 4096;
static const uint32_t ConnRetrySec = 5;
static const uint32_t SendTimeoutSec = 1;
static const uint32_t RecvTimeoutSec = 3;
struct UserData
{
};
};
#ifdef USE_TCPDIRECT
#include "../Tcpdirect.h"
using TcpClient = TcpdirectTcpClient<ClientConf>;
#else
#ifdef USE_EFVI
#include "../efvitcp/EfviTcp.h"
using TcpClient = EfviTcpClient<ClientConf>;
#else
#include "../Socket.h"
using TcpClient = SocketTcpClient<ClientConf>;
#endif
#endif
#include "timestamp.h"
using namespace std;
struct Packet
{
uint64_t ts = 0;
uint64_t val = 0;
};
volatile bool running = true;
void my_handler(int s) {
running = false;
}
TcpClient client;
Packet pack;
int main(int argc, char** argv) {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = my_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
if (argc < 3) {
cout << "usage: " << argv[0] << " interface server_ip" << endl;
exit(1);
}
const char* interface = argv[1];
const char* server_ip = argv[2];
if (!client.init(interface, server_ip, 1234)) {
cout << client.getLastError() << endl;
exit(1);
}
while (running) {
struct
{
void onTcpConnectFailed() { cout << "onTcpConnectFailed, " << client.getLastError() << endl; }
void onTcpConnected(TcpClient::Conn& conn) { cout << "onTcpConnected" << endl; }
void onSendTimeout(TcpClient::Conn& conn) {
pack.val++;
pack.ts = getns();
conn.writeNonblock(&pack, sizeof(pack));
}
uint32_t onTcpData(TcpClient::Conn& conn, const uint8_t* data, uint32_t size) {
auto now = getns();
while (size >= sizeof(Packet)) {
const Packet& recv_pack = *(const Packet*)data;
auto lat = now - recv_pack.ts;
cout << "recv val: " << recv_pack.val << " latency: " << lat << endl;
data += sizeof(Packet);
size -= sizeof(Packet);
}
return size;
}
void onRecvTimeout(TcpClient::Conn& conn) {
cout << "onRecvTimeout" << endl;
conn.close("timeout");
}
void onTcpDisconnect(TcpClient::Conn& conn) { cout << "onDisconnect: " << conn.getLastError() << endl; }
} handler;
client.poll(handler);
}
return 0;
}