-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselector_context.cpp
More file actions
68 lines (52 loc) · 1.4 KB
/
selector_context.cpp
File metadata and controls
68 lines (52 loc) · 1.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
#include "selector_context.hpp"
#include <stdexcept>
#include <errno.h>
#include <string.h>
#include <unistd.h>
selector_context_t::selector_context_t(){
ep_fd = epoll_create(EPOLL_SIZE);
if (ep_fd == -1){
perror("creating epoll");
abort();
}
for(int i = 0; i < EPOLL_SIZE; i++){
events[i].events = 0;
events[i].data.fd = -1;
}
}
selector_context_t::~selector_context_t(){
close(ep_fd);
}
void selector_context_t::register_file_descriptor(int fd, uint32_t op) noexcept{
struct epoll_event event;
event.data.fd = fd;
event.events = 0;
if (op & READ){
event.events |= EPOLLIN;
}
if (op & WRITE){
event.events |= EPOLLOUT;
}
epoll_ctl(ep_fd, EPOLL_CTL_ADD, fd, &event);
}
void selector_context_t::change_descriptor_mode(int fd, uint32_t op) noexcept{
struct epoll_event event;
event.data.fd = fd;
event.events = 0;
if (op & READ){
event.events |= EPOLLIN;
}
if (op & WRITE){
event.events |= EPOLLOUT;
}
epoll_ctl(ep_fd, EPOLL_CTL_MOD, fd, &event);
}
void selector_context_t::unregister_file_descriptor(int fd) noexcept{
epoll_ctl(ep_fd, EPOLL_CTL_DEL, fd, NULL);
}
int selector_context_t::do_select() noexcept{
return epoll_wait(ep_fd, events, EPOLL_SIZE, -1);
}
void wait_context_t::notify() noexcept{
selector_context->change_descriptor_mode(fd, WRITE);
}