forked from IronsDu/brynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingPongServer.cpp
More file actions
79 lines (68 loc) · 2.4 KB
/
PingPongServer.cpp
File metadata and controls
79 lines (68 loc) · 2.4 KB
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
#include <iostream>
#include <mutex>
#include <atomic>
#include <brynet/net/EventLoop.h>
#include <brynet/net/TCPService.h>
#include <brynet/net/Wrapper.h>
using namespace brynet;
using namespace brynet::net;
std::atomic_llong TotalRecvSize = ATOMIC_VAR_INIT(0);
std::atomic_llong total_client_num = ATOMIC_VAR_INIT(0);
std::atomic_llong total_packet_num = ATOMIC_VAR_INIT(0);
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: <listen port> <net work thread num>\n");
exit(-1);
}
auto service = TcpService::Create();
service->startWorkerThread(atoi(argv[2]));
auto enterCallback = [](const TcpConnection::Ptr& session) {
total_client_num++;
session->setDataCallback([session](const char* buffer, size_t len) {
session->send(buffer, len);
TotalRecvSize += len;
total_packet_num++;
return len;
});
session->setDisConnectCallback([](const TcpConnection::Ptr& session) {
total_client_num--;
});
};
wrapper::ListenerBuilder listener;
listener.configureService(service)
.configureSocketOptions({
[](TcpSocket& socket) {
socket.setNodelay();
}
})
.configureConnectionOptions({
brynet::net::TcpService::AddSocketOption::WithMaxRecvBufferSize(1024 * 1024),
brynet::net::TcpService::AddSocketOption::AddEnterCallback(enterCallback)
})
.configureListen([=](wrapper::BuildListenConfig config) {
config.setAddr(false, "0.0.0.0", atoi(argv[1]));
})
.asyncRun();
EventLoop mainLoop;
while (true)
{
mainLoop.loop(1000);
if (TotalRecvSize / 1024 == 0)
{
std::cout << "total recv : " << TotalRecvSize << " bytes/s, of client num:" << total_client_num << std::endl;
}
else if ((TotalRecvSize / 1024) / 1024 == 0)
{
std::cout << "total recv : " << TotalRecvSize / 1024 << " K/s, of client num:" << total_client_num << std::endl;
}
else
{
std::cout << "total recv : " << (TotalRecvSize / 1024) / 1024 << " M/s, of client num:" << total_client_num << std::endl;
}
std::cout << "packet num:" << total_packet_num << std::endl;
total_packet_num = 0;
TotalRecvSize = 0;
}
}