-
Notifications
You must be signed in to change notification settings - Fork 84
/
tcpserver.cc
96 lines (82 loc) · 2.48 KB
/
tcpserver.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
#include <bits/stdc++.h>
#include <netinet/in.h>
struct ServerConf
{
static const uint32_t RecvBufSize = 4096;
static const uint32_t MaxConns = 10;
static const uint32_t SendTimeoutSec = 0;
static const uint32_t RecvTimeoutSec = 10;
struct UserData
{
struct sockaddr_in addr;
};
};
#ifdef USE_TCPDIRECT
#include "../Tcpdirect.h"
using TcpServer = TcpdirectTcpServer<ServerConf>;
#else
#ifdef USE_EFVI
#include "../efvitcp/EfviTcp.h"
using TcpServer = EfviTcpServer<ServerConf>;
#else
#include "../Socket.h"
using TcpServer = SocketTcpServer<ServerConf>;
#endif
#endif
using namespace std;
volatile bool running = true;
void my_handler(int s) {
running = false;
}
TcpServer server;
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 (!server.init(interface, server_ip, 1234)) {
cout << server.getLastError() << endl;
return 1;
}
while (running) {
struct
{
void onTcpConnected(TcpServer::Conn& conn) {
conn.getPeername(conn.addr);
cout << "new connection from: " << inet_ntoa(conn.addr.sin_addr) << ":" << ntohs(conn.addr.sin_port)
<< ", total connections: " << server.getConnCnt() << endl;
/*
server.foreachConn([&](TcpServer::Conn& conn) {
cout << "current connection from: " << inet_ntoa(conn.addr.sin_addr) << ":" << ntohs(conn.addr.sin_port)
<< endl;
});
*/
}
void onSendTimeout(TcpServer::Conn& conn) {
cout << "onSendTimeout should not be called as SendTimeoutSec=0" << endl;
exit(1);
}
uint32_t onTcpData(TcpServer::Conn& conn, const uint8_t* data, uint32_t size) {
conn.writeNonblock(data, size);
return 0;
}
void onRecvTimeout(TcpServer::Conn& conn) {
cout << "onRecvTimeout" << endl;
conn.close("timeout");
}
void onTcpDisconnect(TcpServer::Conn& conn) {
cout << "client disconnected: " << inet_ntoa(conn.addr.sin_addr) << ":" << ntohs(conn.addr.sin_port)
<< ", reason: " << conn.getLastError() << ", total connections: " << server.getConnCnt() << endl;
}
} handler;
server.poll(handler);
}
return 0;
}