-
Notifications
You must be signed in to change notification settings - Fork 240
/
PingPongClient.cpp
78 lines (67 loc) · 2.1 KB
/
PingPongClient.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
69
70
71
72
73
74
75
76
77
78
#include <brynet/base/AppStatus.hpp>
#include <brynet/net/AsyncConnector.hpp>
#include <brynet/net/TcpService.hpp>
#include <brynet/net/wrapper/ConnectionBuilder.hpp>
#include <iostream>
#include <string>
using namespace brynet;
using namespace brynet::net;
int main(int argc, char** argv)
{
if (argc != 6)
{
fprintf(stderr, "Usage: <host> <port> <net work thread num> <session num> <packet size> \n");
exit(-1);
}
std::string tmp(atoi(argv[5]), 'a');
auto service = IOThreadTcpService::Create();
service->startWorkerThread(atoi(argv[3]));
auto connector = AsyncConnector::Create();
connector->startWorkerThread();
auto enterCallback = [tmp](const TcpConnection::Ptr& session) {
session->setDataCallback([session](brynet::base::BasePacketReader& reader) {
session->send(reader.begin(), reader.size());
reader.consumeAll();
});
session->send(tmp);
};
auto failedCallback = []() {
std::cout << "connect failed" << std::endl;
};
wrapper::ConnectionBuilder connectionBuilder;
connectionBuilder
.WithService(service)
.WithConnector(connector)
.WithMaxRecvBufferSize(1024 * 1024)
.AddEnterCallback(enterCallback);
const auto num = std::atoi(argv[4]);
const auto ip = argv[1];
const auto port = std::atoi(argv[2]);
for (auto i = 0; i < num; i++)
{
try
{
connectionBuilder
.WithAddr(ip, port)
.WithTimeout(std::chrono::seconds(10))
.WithFailedCallback(failedCallback)
.AddSocketProcessCallback([](TcpSocket& socket) {
socket.setNodelay();
})
.asyncConnect();
}
catch (std::runtime_error& e)
{
std::cout << "error:" << e.what() << std::endl;
}
}
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
if (brynet::base::app_kbhit())
{
break;
}
}
return 0;
}