-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cc
56 lines (42 loc) · 1.19 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
56
#include <signal.h>
#include <memory>
#include "dispatcher.h"
#include "event2/event.h"
#include "event2/thread.h"
#include "handler.h"
#include "listener.h"
#include "spdlog/spdlog.h"
#include "thread_pool.h"
#define MAX_IO_THREAD_COUNT 4
int main() {
signal(SIGPIPE, SIG_IGN);
evthread_use_pthreads();
spdlog::set_pattern("[%Y-%m-%dT%H:%M:%S.%e%z] [%l] [%!(%s#%#)] %v");
spdlog::set_level(spdlog::level::trace);
SPDLOG_INFO("starting...");
tl::Dispatcher* disps = new tl::Dispatcher[MAX_IO_THREAD_COUNT];
tl::ThreadPool* thread_pool(new tl::ThreadPool(MAX_IO_THREAD_COUNT));
std::unique_ptr<tl::Listener> ls[4];
for (int i = 0; i < MAX_IO_THREAD_COUNT; i++) {
SPDLOG_INFO("staring thread {}", i);
ls[i].reset(new tl::Listener("0.0.0.0", 2200));
ls[i]->open(&disps[i], [](tl::Dispatcher* d, int fd) {
tl::Handler* h = nullptr;
try {
h = new tl::Handler(d, fd);
} catch (...) {
if (h) delete h;
}
});
thread_pool->post(
[](tl::Dispatcher* disp) {
SPDLOG_TRACE("dispatch()");
disp->dispatch();
},
&disps[i]);
}
// wait join
delete thread_pool;
delete[] disps;
return 0;
}