-
Notifications
You must be signed in to change notification settings - Fork 940
/
main.cc
55 lines (48 loc) · 1.56 KB
/
main.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
#include <evpp/udp/udp_server.h>
#include <evpp/udp/udp_message.h>
#include <evpp/event_loop.h>
#include <evpp/event_loop_thread_pool.h>
#ifdef _WIN32
#include "../../winmain-inl.h"
#endif
int main(int argc, char* argv[]) {
std::vector<int> ports = { 1053, 5353 };
int port = 29099;
int thread_num = 2;
if (argc > 1) {
if (std::string("-h") == argv[1] ||
std::string("--h") == argv[1] ||
std::string("-help") == argv[1] ||
std::string("--help") == argv[1]) {
std::cout << "usage : " << argv[0] << " <listen_port> <thread_num>\n";
std::cout << " e.g. : " << argv[0] << " 8080 24\n";
return 0;
}
}
if (argc == 2) {
port = atoi(argv[1]);
}
ports.push_back(port);
if (argc == 3) {
port = atoi(argv[1]);
thread_num = atoi(argv[2]);
}
evpp::udp::Server server;
server.SetThreadDispatchPolicy(evpp::ThreadDispatchPolicy::kIPAddressHashing);
server.SetMessageHandler([](evpp::EventLoop* loop, evpp::udp::MessagePtr& msg) {
std::stringstream oss;
oss << "func=" << __FUNCTION__ << " OK"
<< " body=" << std::string(msg->data(), msg->size()) << "\n";
evpp::udp::SendMessage(msg);
});
server.Init(ports);
server.Start();
evpp::EventLoop loop;
std::shared_ptr<evpp::EventLoopThreadPool> tpool(new evpp::EventLoopThreadPool(&loop, thread_num));
tpool->Start(true);
server.SetEventLoopThreadPool(tpool);
loop.Run();
server.Stop(true);
tpool->Stop(true);
return 0;
}