-
Notifications
You must be signed in to change notification settings - Fork 0
/
__web-serverNEW.h
307 lines (244 loc) · 7.28 KB
/
__web-serverNEW.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Based on https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/
* https://stackoverflow.com/questions/6954489/c-whats-the-way-to-make-a-poolthread-with-pthreads - thread pool
*/
#ifndef HIGHLOAD_WEB_SERVER_H
#define HIGHLOAD_WEB_SERVER_H
#include <unistd.h>
#include <sstream>
#include <utility>
#include <vector>
#include <sys/epoll.h>
#include <iostream>
#include <functional>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <cerrno>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <queue>
#include "logger.h"
#include "state.h"
using namespace std;
#define M_FLAGS (EPOLLIN | EPOLLET)
#define M_EPOLL_MAX_EVENTS 64
#define M_LISTEN_MAX_CONN SOMAXCONN
#define M_THREADS_COUNT 4
class WebServer;
class WebServerWorker
{
WebServer *_server = NULL;
public:
int efd;
bool is_master;
explicit WebServerWorker(WebServer *server, bool is_master);
void do_work();
};
class WebServer
{
typedef int (*Handler)(int);
uint16_t _port;
Handler _handler;
WebServerWorker* workers[M_THREADS_COUNT];
int sfd;
int _create_and_bind()
{
addrinfo hints{};
addrinfo *result, *rp;
int sfd = -1;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; // Return IPv4 and IPv6 choices
hints.ai_socktype = SOCK_STREAM; // We want a TCP socket
hints.ai_flags = AI_PASSIVE; // All interfaces
char port[6];
sprintf(port, "%d", (int) this->_port);
int s = getaddrinfo(NULL, port, &hints, &result);
if (s != 0)
{
M_ERROR("getaddrinfo: " << gai_strerror(s));
abort();
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close(sfd);
}
if (rp == NULL)
{
perror("Could not bind");
abort();
}
freeaddrinfo(result);
return sfd;
}
static int _make_socket_nodelay(int sfd)
{
int flags = 1;
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(int)) < 0)
{
perror("setsockopt TCP_NODELAY");
abort();
}
if (setsockopt(sfd, IPPROTO_TCP, TCP_QUICKACK, &flags, sizeof(int)) < 0)
{
perror("setsockopt TCP_QUICKACK");
abort();
}
// int prior = 6;
// if (setsockopt(sfd, SOL_SOCKET, SO_PRIORITY, &prior, sizeof(int)) < 0)
// {
// perror("setsockopt SO_PRIORITY");
// abort();
// }
// int bfsz = 128;
// if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bfsz, sizeof(int)) < 0)
// {
// perror("setsockopt SO_RCVBUF");
// abort();
// }
// if (setsockopt(sfd, SOL_SOCKET, SO_DONTROUTE, (char*) &flags, sizeof(int)) < 0)
// {
// perror("setsockopt SO_DONTROUTE");
// abort();
// }
}
static void _make_socket_non_blocking(int sfd)
{
int flags = fcntl(sfd, F_GETFL, 0);
if (flags == -1)
{
perror("fcntl F_GETFL");
abort();
}
flags |= O_NONBLOCK;
if (fcntl(sfd, F_SETFL, flags) == -1)
{
perror("fcntl F_SETFL");
abort();
}
}
static void* _do_work(void* context)
{
((WebServerWorker*) context)->do_work();
return NULL;
}
public:
explicit WebServer(uint16_t port, Handler handler)
{
_port = port;
_handler = handler;
}
int start()
{
sfd = _create_and_bind();
_make_socket_non_blocking(sfd);
_make_socket_nodelay(sfd);
if (listen(sfd, M_LISTEN_MAX_CONN) == -1)
{
perror("listen");
abort();
}
for(int i = 0; i < M_THREADS_COUNT; i++)
{
workers[i] = new WebServerWorker(this, i == 0);
}
epoll_event event{};
event.data.fd = sfd;
event.events = M_FLAGS;
if (epoll_ctl(workers[0]->efd, EPOLL_CTL_ADD, sfd, &event) == -1)
{
perror("epoll_ctl");
abort();
}
pthread_t threads[M_THREADS_COUNT];
for (int i = 0; i < M_THREADS_COUNT; i++)
pthread_create(&threads[i], NULL, &WebServer::_do_work, workers[i]);
for (int i = 0; i < M_THREADS_COUNT; i++)
pthread_join(threads[i], NULL);
close(sfd);
}
friend class WebServerWorker;
};
WebServerWorker::WebServerWorker(WebServer* server, bool is_master)
{
this->_server = server;
this->is_master = is_master;
efd = epoll_create1(0);
if (efd == -1)
{
perror("epoll_create");
abort();
}
}
void WebServerWorker::do_work()
{
auto sfd = _server->sfd;
epoll_event event{};
epoll_event events[M_EPOLL_MAX_EVENTS];
while (1)
{
int n = epoll_wait(efd, events, M_EPOLL_MAX_EVENTS, 0);
if (n > 0)
M_DEBUG_LOG((long long) pthread_self() << "| wake ");
if (n < 0)
{
perror("epoll_wait");
//abort();
}
if (n > 1)
M_DEBUG_LOG((long long) pthread_self() << "| " << n << " in queue");
for (int i = 0; i < n; i++)
{
auto socket_fd = events[i].data.fd;
M_DEBUG_LOG((long long) pthread_self() << "| sock in: " << socket_fd);
if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (events[i].events & EPOLLRDHUP) || (!(events[i].events & EPOLLIN)))
{
M_ERROR("epoll error: " << events[i].events);
M_LOG("Query idx: " << state.query_id);
close(events[i].data.fd);
}
else if (sfd == socket_fd)
{
assert(is_master);
while (1)
{
sockaddr in_addr{};
socklen_t in_len;
in_len = sizeof in_addr;
int infd = accept4(sfd, &in_addr, &in_len, SOCK_NONBLOCK);
if (infd == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
perror("accept");
break;
}
M_DEBUG_LOG((long long) pthread_self() << "| accepted: " << infd);
WebServer::_make_socket_nodelay(infd);
event.data.fd = infd;
event.events = M_FLAGS;
auto target_efd = _server->workers[infd % M_THREADS_COUNT]->efd;
M_DEBUG_LOG(infd << " -> " << target_efd);
if (epoll_ctl(target_efd, EPOLL_CTL_ADD, infd, &event) == -1)
{
perror("epoll_ctl add");
abort();
}
}
}
else
{
_server->_handler(socket_fd);
}
}
}
}
#endif //HIGHLOAD_WEB_SERVER_H