-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
51 lines (45 loc) · 1.17 KB
/
utils.h
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
#pragma once
#include<string>
using std::string;
/*
void error_handling(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
int setnonblocking(int fd) {
int old_option = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, old_option | O_NONBLOCK);
return old_option;
}
void addfd(int epollfd, int fd, bool oneshot = true) {
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
if (oneshot) {
event.events |= EPOLLONESHOT;
}
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
void removefd(int epollfd, int fd) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
}
void modfd(int epollfd, int fd, int ev) {
epoll_event event;
event.data.fd = fd;
event.events = EPOLLRDHUP | EPOLLET | EPOLLONESHOT | ev;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
}
void addSig(int sig, void(handler)(int), bool restart = true) {
struct sigaction sa;
memset(&sa, '\0', sizeof(sa));
sa.sa_handler = handler;
if (restart) {
sa.sa_flags |= SA_RESTART;
}
sigfillset(&sa.sa_mask);
assert(sigaction(sig, &sa, NULL) != -1);
}
*/