-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.cpp
180 lines (157 loc) · 4.46 KB
/
event.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "event.h"
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <cstdio>
#include <map>
#include <queue>
#include <tuple>
#include <vector>
#include "client.h"
#include "openflow.h"
static uint64_t current_time_ms(void);
static void nonblock(int);
void Server::open(uint16_t port) {
int sock, one = 1;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
signal(SIGPIPE, SIG_IGN);
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(-1);
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)) < 0) {
perror("setsockopt");
close(sock);
exit(-1);
}
nonblock(sock);
if (bind(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) <
0) {
perror("bind");
close(sock);
exit(-1);
}
if (listen(sock, 1)) {
perror("listen");
close(sock);
exit(-1);
}
/* Set up server */
fd = sock;
}
/* Return the number of milliseconds since EPOCH */
uint64_t current_time_ms(void) {
struct timeval tv;
gettimeofday(&tv, nullptr);
return static_cast<uint64_t>(tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
void Server::listen_and_serve() {
#define MAX_EVENTS 10
struct epoll_event ev, events[MAX_EVENTS];
int clientfd, ep, nfds, timeout;
if ((ep = epoll_create1(0)) < 0) {
perror("epoll_create1");
exit(-1);
}
/* Silence valgrind */
memset(&ev.data, 0, sizeof(ev.data));
ev.events = EPOLLIN;
ev.data.fd = fd;
if (epoll_ctl(ep, EPOLL_CTL_ADD, fd, &ev) < 0) {
perror("epoll_ctl: listen");
exit(-1);
}
for (;;) {
// Time until next time-based event fires
timeout = -1;
if (!time_events.empty()) {
uint64_t now = current_time_ms();
Event next_event = time_events.top();
if (now < next_event.when) {
timeout = static_cast<int>(next_event.when - now);
} else {
timeout = 0;
}
}
if ((nfds = epoll_wait(ep, events, MAX_EVENTS, timeout)) < 0) {
perror("epoll_wait");
exit(-1);
}
handle_time_events();
int ndx;
for (ndx = 0; ndx < nfds; ndx++) {
if (events[ndx].data.fd == fd) {
if ((clientfd = accept(fd, nullptr, nullptr)) < 0) {
perror("accept");
}
nonblock(clientfd);
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.fd = clientfd;
if (epoll_ctl(ep, EPOLL_CTL_ADD, clientfd, &ev) < 0) {
perror("epoll_ctl: accept");
exit(-1);
}
Client* c = new Client(clientfd, this);
clients.insert(std::pair<int, Client*>(clientfd, c));
c->init();
} else {
Client* c = clients.find(events[ndx].data.fd)->second;
if (events[ndx].events & EPOLLIN) {
c->handle_read_event();
}
if (events[ndx].events & EPOLLOUT) {
c->canwrite = 1;
}
c->flush_write_queue();
}
}
}
}
Event::Event(event_handler_t h, void* a, uint64_t w) {
handler = h;
arg = a;
when = w;
}
void Server::schedule_event(uint64_t when_ms, event_handler_t handler,
void* arg) {
time_events.push(Event(handler, arg, current_time_ms() + when_ms));
}
void Server::handle_time_events() {
uint64_t now = current_time_ms();
while (!time_events.empty()) {
Event event = time_events.top();
if (event.when > now) {
break;
}
time_events.pop();
event_handler_t handler = event.handler;
void* arg = event.arg;
handler(arg);
}
}
void Server::close_server() {
if (close(fd) < 0) {
perror("close");
}
}
void nonblock(int fd) {
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
perror("fcntl(F_GETFL)");
return;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
perror("fcntl(O_NONBLOCK)");
}
}