-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.cpp
333 lines (258 loc) · 8.08 KB
/
server.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "kv.pb.h"
#include "log.h"
#include "protocol.h"
#include "rpc.h"
#include <array>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <sstream>
#include <string>
#include <unordered_map>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
static_assert(EAGAIN == EWOULDBLOCK);
using namespace NLogging;
using namespace NProtocol;
using namespace NRpc;
namespace {
////////////////////////////////////////////////////////////////////////////////
constexpr int max_events = 32;
////////////////////////////////////////////////////////////////////////////////
auto create_and_bind(std::string const& port)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* TCP */
hints.ai_flags = AI_PASSIVE; /* All interfaces */
struct addrinfo* result;
int sockt = getaddrinfo(nullptr, port.c_str(), &hints, &result);
if (sockt != 0) {
LOG_ERROR("getaddrinfo failed");
return -1;
}
struct addrinfo* rp = nullptr;
int socketfd = 0;
for (rp = result; rp != nullptr; rp = rp->ai_next) {
socketfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (socketfd == -1) {
continue;
}
sockt = bind(socketfd, rp->ai_addr, rp->ai_addrlen);
if (sockt == 0) {
break;
}
close(socketfd);
}
if (rp == nullptr) {
LOG_ERROR("bind failed");
return -1;
}
freeaddrinfo(result);
return socketfd;
}
////////////////////////////////////////////////////////////////////////////////
auto make_socket_nonblocking(int socketfd)
{
int flags = fcntl(socketfd, F_GETFL, 0);
if (flags == -1) {
LOG_ERROR("fcntl failed (F_GETFL)");
return false;
}
flags |= O_NONBLOCK;
int s = fcntl(socketfd, F_SETFL, flags);
if (s == -1) {
LOG_ERROR("fcntl failed (F_SETFL)");
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
SocketStatePtr invalid_state()
{
return std::make_shared<SocketState>();
}
SocketStatePtr accept_connection(
int socketfd,
struct epoll_event& event,
int epollfd)
{
struct sockaddr in_addr;
socklen_t in_len = sizeof(in_addr);
int infd = accept(socketfd, &in_addr, &in_len);
if (infd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return nullptr;
} else {
LOG_PERROR("accept failed with error");
return invalid_state();
}
}
std::string hbuf(NI_MAXHOST, '\0');
std::string sbuf(NI_MAXSERV, '\0');
auto ret = getnameinfo(
&in_addr, in_len,
const_cast<char*>(hbuf.data()), hbuf.size(),
const_cast<char*>(sbuf.data()), sbuf.size(),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ret == 0) {
LOG_INFO_S("accepted connection on fd " << infd
<< "(host=" << hbuf << ", port=" << sbuf << ")");
}
if (!make_socket_nonblocking(infd)) {
LOG_PERROR("make_socket_nonblocking failed");
return invalid_state();
}
event.data.fd = infd;
event.events = EPOLLIN | EPOLLOUT | EPOLLET;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, infd, &event) == -1) {
LOG_PERROR("epoll_ctl failed");
return invalid_state();
}
auto state = std::make_shared<SocketState>();
state->fd = infd;
return state;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
int main(int argc, const char** argv)
{
if (argc < 2) {
return 1;
}
/*
* socket creation and epoll boilerplate
* TODO extract into struct Bootstrap
*/
auto socketfd = ::create_and_bind(argv[1]);
if (socketfd == -1) {
return 1;
}
if (!::make_socket_nonblocking(socketfd)) {
return 1;
}
if (listen(socketfd, SOMAXCONN) == -1) {
LOG_ERROR("listen failed");
return 1;
}
int epollfd = epoll_create1(0);
if (epollfd == -1) {
LOG_ERROR("epoll_create1 failed");
return 1;
}
struct epoll_event event;
event.data.fd = socketfd;
event.events = EPOLLIN | EPOLLET;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socketfd, &event) == -1) {
LOG_ERROR("epoll_ctl failed");
return 1;
}
/*
* handler function
*/
// TODO on-disk storage
std::unordered_map<std::string, uint64_t> storage;
auto handle_get = [&] (const std::string& request) {
NProto::TGetRequest get_request;
if (!get_request.ParseFromArray(request.data(), request.size())) {
// TODO proper handling
abort();
}
LOG_DEBUG_S("get_request: " << get_request.ShortDebugString());
NProto::TGetResponse get_response;
get_response.set_request_id(get_request.request_id());
auto it = storage.find(get_request.key());
if (it != storage.end()) {
get_response.set_offset(it->second);
}
std::stringstream response;
serialize_header(GET_RESPONSE, get_response.ByteSizeLong(), response);
get_response.SerializeToOstream(&response);
return response.str();
};
auto handle_put = [&] (const std::string& request) {
NProto::TPutRequest put_request;
if (!put_request.ParseFromArray(request.data(), request.size())) {
// TODO proper handling
abort();
}
LOG_DEBUG_S("put_request: " << put_request.ShortDebugString());
storage[put_request.key()] = put_request.offset();
NProto::TPutResponse put_response;
put_response.set_request_id(put_request.request_id());
std::stringstream response;
serialize_header(PUT_RESPONSE, put_response.ByteSizeLong(), response);
put_response.SerializeToOstream(&response);
return response.str();
};
Handler handler = [&] (char request_type, const std::string& request) {
switch (request_type) {
case PUT_REQUEST: return handle_put(request);
case GET_REQUEST: return handle_get(request);
}
// TODO proper handling
abort();
return std::string();
};
/*
* rpc state and event loop
* TODO extract into struct Rpc
*/
std::array<struct epoll_event, ::max_events> events;
std::unordered_map<int, SocketStatePtr> states;
auto finalize = [&] (int fd) {
LOG_INFO_S("close " << fd);
close(fd);
states.erase(fd);
};
while (true) {
const auto n = epoll_wait(epollfd, events.data(), ::max_events, -1);
{
LOG_INFO_S("got " << n << " events");
}
for (int i = 0; i < n; ++i) {
const auto fd = events[i].data.fd;
if (events[i].events & EPOLLERR
|| events[i].events & EPOLLHUP
|| !(events[i].events & (EPOLLIN | EPOLLOUT)))
{
LOG_ERROR_S("epoll event error on fd " << fd);
finalize(fd);
continue;
}
if (socketfd == fd) {
while (true) {
auto state = ::accept_connection(socketfd, event, epollfd);
if (!state) {
break;
}
states[state->fd] = state;
}
continue;
}
bool closed = false;
if (events[i].events & EPOLLIN) {
auto state = states.at(fd);
if (!process_input(*state, handler)) {
finalize(fd);
closed = true;
}
}
if (events[i].events & EPOLLOUT && !closed) {
auto state = states.at(fd);
if (!process_output(*state)) {
finalize(fd);
}
}
}
}
LOG_INFO("exiting");
close(socketfd);
return 0;
}