-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_epoll_reactor.cpp
100 lines (84 loc) · 2.76 KB
/
simple_epoll_reactor.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <sys/epoll.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <cstring>
#include <functional>
using namespace std;
#include "lib/tcp_listener.h"
const int MAX_EVENTS = 128;
struct Channel {
int fd;
function<void(int, int)> read_callback;
void on_event(uint32_t events, int efd) {
if ((events & EPOLLIN) && (read_callback != nullptr)) {
read_callback(fd, efd);
}
}
};
void read_callback(int conn_fd, int efd) {
TcpConnection conn{conn_fd};
string msg;
if (!(msg = conn.blocking_receive_line()).empty()) {
cout << "server received: " << msg;
conn.blocking_send(msg);
} else {
struct epoll_event event{};
epoll_ctl(efd, EPOLL_CTL_DEL, conn_fd, &event);
conn.close();
cout << "server closed conn_fd: " << conn_fd << endl;
}
}
void accept_callback(int listen_fd, int efd) {
TcpListener listener{listen_fd};
TcpConnection conn = listener.accept();
conn.set_nonblocking();
int conn_fd = conn.conn_fd();
struct epoll_event event{};
event.events = EPOLLIN | EPOLLET;
struct Channel *channel = new Channel{conn_fd, read_callback};
event.data.ptr = channel;
if (epoll_ctl(efd, EPOLL_CTL_ADD, conn_fd, &event) == -1) {
cerr << "epoll_ctl add failed: " << strerror(errno) << endl;
exit(1);
}
}
int main() {
int efd;
struct epoll_event event{};
int n;
vector<struct epoll_event> events(MAX_EVENTS);
TcpListener listener;
listener.listen(8888, true);
efd = epoll_create1(EPOLL_CLOEXEC);
if (efd == -1) {
cerr << "epoll create failed: " << strerror(errno) << endl;
exit(1);
}
event.events = EPOLLIN | EPOLLET;
struct Channel *acceptor = new Channel{listener.listen_fd(), accept_callback};
event.data.ptr = acceptor;
if (epoll_ctl(efd, EPOLL_CTL_ADD, listener.listen_fd(), &event) == -1) {
cerr << "epoll_ctl add failed: " << strerror(errno) << endl;
exit(1);
}
while (true) {
n = epoll_wait(efd, events.data(), MAX_EVENTS, -1);
for (int i = 0; i < n; i++) {
if (events[i].events & (EPOLLERR | EPOLLHUP) ||
!(events[i].events & EPOLLIN)) {
cerr << "epoll error" << endl;
struct Channel *channel = (Channel *) events[i].data.ptr;
epoll_ctl(efd, EPOLL_CTL_DEL, channel->fd, &events[i]);
close(channel->fd);
delete channel;
} else {
if (events[i].events & EPOLLIN) {
cout << "epoll in\n";
}
struct Channel *channel = (Channel *) events[i].data.ptr;
channel->on_event(events[i].events, efd);
}
}
}
}