-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
468 lines (381 loc) · 11.9 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <vector>
#include <map>
// Constants
const size_t k_max_msg = 4096; // Maximum message size
const size_t k_max_args = 1024; // Maximum number of arguments
// Connection states
enum {
STATE_REQ = 0, // Request
STATE_RES = 1, // Response
STATE_END = 2 // End, mark the connection for deletion
};
// Response codes
enum {
RES_OK = 0, // Success
RES_ERR = 1, // Error
RES_NX = 2 // Not Found
};
// Utility functions for error logging and error handling
static void msg(const char *msg) {
fprintf(stderr, "%s\n", msg);
}
static void die(const char *msg) {
int err = errno;
fprintf(stderr, "[%d] %s\n", err, msg);
abort();
}
// Set a file descriptor to non-blocking mode
static void fd_set_nb(int fd) {
errno = 0;
int flags = fcntl(fd, F_GETFL, 0); // Get current fd file status flag
if (errno) {
die("fcntl error");
return;
}
flags |= O_NONBLOCK; // Add argument to a flag (non-blocking mode)
errno = 0;
static_cast<void>(fcntl(fd, F_SETFL, flags)); // Set modified flag on the fd
if(errno){
die("fcntl error");
}
}
// Connection structure representing a single client connection
struct Conn {
int fd = -1;
uint32_t state = 0; // Either STATE_REQ or STATE_RES
// buffer for reading
size_t rbuf_size = 0;
uint8_t rbuf[4 + k_max_msg];
// buffer for writing
size_t wbuf_size = 0;
size_t wbuf_sent = 0;
uint8_t wbuf[4 + k_max_msg];
};
// Global key-value store using a map
static std::map<std::string, std::string> g_map;
// Associate a connection object with a file descriptor in a vector
static void conn_put(std::vector<Conn *> &fd2conn, struct Conn *conn) {
if (fd2conn.size() <= static_cast<size_t>(conn->fd)) {
fd2conn.resize(conn->fd + 1);
}
fd2conn[conn->fd] = conn;
}
// Accept new connections on a listening socket
static int32_t accept_new_conn(std::vector<Conn *> &fd2conn, int fd) {
// Accept
struct sockaddr_in client_addr = {};
socklen_t socklen = sizeof(client_addr);
int connfd = accept(fd, (struct sockaddr *)&client_addr, &socklen);
if (connfd < 0) {
msg("accept() error");
return -1; // error
}
// Set the new connection fd to nonblocking mode
fd_set_nb(connfd);
// Creating the struct Conn
struct Conn *conn = (struct Conn *)malloc(sizeof(struct Conn));
if (!conn) {
close(connfd);
return -1;
}
conn->fd = connfd;
conn->state = STATE_REQ;
conn->rbuf_size = 0;
conn->wbuf_size = 0;
conn->wbuf_sent = 0;
conn_put(fd2conn, conn);
return 0;
}
// Prototypes for state handling functions
static void state_req(Conn *conn);
static void state_res(Conn *conn);
// Parses a request and extracts strings based on prefixed lengths
static int32_t parseRequest(const uint8_t *data, size_t len, std::vector<std::string> &out){
// First 4 bytes are the number of strings to be extracted
if (len < 4) {
return -1; // Not enough data to read the initial cound of strings
}
uint32_t numStrings = 0;
memcpy(&numStrings, &data[0], 4);
if (numStrings > k_max_args) {
return -1; // More strings than allowed
}
size_t pos = 4;
while (numStrings--) {
if (pos + 4 > len) {
return -1; // Not enough data to read the size of the next sting
}
uint32_t sz = 0;
memcpy(&sz, &data[pos], 4);
if (pos + 4 + sz > len) {
return -1;
}
out.push_back(std::string((char *)&data[pos + 4], sz));
pos += 4 + sz;
}
if (pos != len) {
return -1; // Trailing garbage
}
return 0;
}
// Get the value associated with a key from the g_map
static uint32_t do_get(const std::vector<std::string> &cmd, uint8_t *res, uint32_t *reslen) {
if (!g_map.count(cmd[1])) {
return RES_NX;
}
std::string &val = g_map[cmd[1]];
assert(val.size() <= k_max_msg);
memcpy(res, val.data(), val.size());
*reslen = static_cast<uint32_t>(val.size());
return RES_OK;
}
// Insert a new key-value pair or updates an existing one
static uint32_t do_set(const std::vector<std::string> &cmd, uint8_t *res, uint32_t *reslen) {
static_cast<void>(res);
static_cast<void>(reslen);
g_map[cmd[1]] = cmd[2];
return RES_OK;
}
// Remove a key and its value from map
static uint32_t do_del(const std::vector<std::string> &cmd, uint8_t *res, uint32_t *reslen) {
static_cast<void>(res);
static_cast<void>(reslen);
g_map.erase((cmd[1]));
return RES_OK;
}
static bool cmd_is(const std::string &word, const char *cmd) {
return 0 == strcasecmp(word.c_str(), cmd);
}
// Handles a request by determining which command to execute (get, set, del)
static int32_t do_request(
const uint8_t *req, uint32_t reqlen,
uint32_t *rescode, uint8_t *res, uint32_t *reslen)
{
std::vector<std::string> cmd;
if(0 != parseRequest(req, reqlen, cmd)) {
msg("bad request");
return -1;
}
if (cmd.size() == 2 && cmd_is(cmd[0], "get")) {
*rescode = do_get(cmd, res, reslen);
} else if (cmd.size() == 3 && cmd_is(cmd[0], "set")) {
*rescode = do_set(cmd, res, reslen);
} else if (cmd.size() == 2 && cmd_is(cmd[0], "del")) {
*rescode = do_del(cmd, res, reslen);
} else {
// Cmd is not recognized
*rescode = RES_ERR;
const char *msg = "Unknown cmd";
strcpy((char*)res, msg);
*reslen = strlen(msg);
return 0;
}
return 0;
}
// Process a single request from the connection buffer
static bool try_one_request(Conn *conn) {
// Try to parse a request from the buffer
if (conn->rbuf_size < 4) {
// Not enoough data in the buffer. Will retry in the next iteration.
return false;
}
uint32_t len = 0;
memcpy(&len, &conn->rbuf[0], 4);
if (len > k_max_msg){
msg("too long");
conn->state = STATE_END;
return false;
}
if (4 + len > conn->rbuf_size) {
// no enough data in the buffer. Will retry in the next iteration
return false;
}
// Got one request, generate the response
uint32_t rescode = 0;
uint32_t wlen = 0;
int32_t err = do_request(&conn->rbuf[4], len, &rescode, &conn->wbuf[4+4], &wlen);
if(err) {
conn->state = STATE_END;
return false;
}
// Generation echoing response
wlen += 4;
memcpy(&conn->wbuf[0], &wlen, 4);
memcpy(&conn->wbuf[4], &rescode, 4);
conn->wbuf_size = 4 + wlen;
// Remove the request from the buffer
size_t remain = conn->rbuf_size - 4 - len;
if (remain) {
memmove(conn->rbuf, &conn->rbuf[4 + len], remain);
}
conn->rbuf_size = remain;
// Change state
conn->state = STATE_RES;
state_res(conn);
// Continuse the outer loop if the request was fully processed
return (conn->state == STATE_REQ);
}
// Attempt to fill the conneciton's read buffer
static bool try_fill_buffer(Conn *conn){
// Try to fil buffer
assert(conn->rbuf_size < sizeof(conn->rbuf));
ssize_t rv = 0;
do {
size_t cap = sizeof(conn->rbuf) - conn->rbuf_size;
rv = read(conn->fd, &conn->rbuf[conn->rbuf_size], cap);
} while (rv < 0 && errno == EINTR);
if (rv < 0 && errno == EAGAIN) {
// got EAGAIN stop.
return false;
}
if (rv < 0) {
msg("reaD() error");
conn->state = STATE_END;
return false;
}
if (rv == 0) {
if (conn->rbuf_size > 0){
msg("unexpected EOF");
} else {
msg("EOF");
}
conn->state = STATE_END;
return false;
}
conn->rbuf_size += static_cast<size_t>(rv);
assert(conn->rbuf_size < sizeof(conn->rbuf));
// Try to process requests one by one.
// Why is there a loop? Please read the explanation of "pielining"
while (try_one_request(conn)) {}
return (conn->state == STATE_REQ);
}
// Handles state for waiting for request
static void state_req(Conn *conn) {
while (try_fill_buffer(conn)) {}
}
// Attempt to flush the connection's write buffer
static bool try_flush_buffer(Conn *conn) {
ssize_t rv = 0;
do {
size_t remain = conn->wbuf_size - conn->wbuf_sent;
rv = write(conn->fd, &conn->wbuf[conn->wbuf_sent], remain);
} while (rv < 0 && errno == EINTR);
if (rv < 0 && errno == EAGAIN) {
// got EAGAIN, stop.
return false;
}
if (rv < 0) {
msg("write() error");
conn->state = STATE_END;
return false;
}
conn->wbuf_sent += static_cast<size_t>(rv);
assert(conn->wbuf_sent <= conn->wbuf_size);
if (conn->wbuf_sent <= conn->wbuf_size) {
// Response was fully sent, change state back
conn->state = STATE_REQ;
conn->wbuf_sent = 0;
conn->wbuf_size = 0;
return false;
}
// Still got some data in wbuf, could try to write again
return true;
}
// Handles state for sending responses
static void state_res(Conn *conn) {
while (try_flush_buffer(conn)) {}
}
// Main IO handling logic for connection based on state
static void connection_io(Conn *conn) {
if (conn->state == STATE_REQ) {
state_req(conn);
} else if (conn->state == STATE_RES) {
state_res(conn);
} else {
assert(0); // Unexpected state
}
}
int main() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
die("socket()");
}
// Set SO_REUSEADDR to quickly restart the server after a crash or shutdown
int val = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
// Binding socket to port 1234
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = ntohs(1234);
addr.sin_addr.s_addr = ntohl(0); // wildcard address 0.0.0.0
int rv = bind(fd, (const sockaddr *)&addr, sizeof(addr));
if (rv) {
die("bind()");
}
// listen
rv = listen(fd, SOMAXCONN);
if (rv) {
die("listen()");
}
// A map of all clients connections, keyed by fd
std::vector<Conn *> fd2conn;
// Set the listen fd to nonblocking mode
fd_set_nb(fd);
// Event loop
std::vector<struct pollfd> poll_args;
while(true) {
// Prepare the arguments of the poll()
poll_args.clear();
// The listening fd is put in the first position
struct pollfd pfd = {fd, POLLIN, 0};
poll_args.push_back(pfd);
// Connection fds
for (Conn *conn : fd2conn) {
if(!conn) {
continue;
}
struct pollfd pfd = {};
pfd.fd = conn->fd;
pfd.events = (conn->state == STATE_REQ) ? POLLIN : POLLOUT;
pfd.events = pfd.events | POLLERR;
poll_args.push_back(pfd);
}
// Poll for active fds
// Time arguments doesn't matter here
int rv = poll(poll_args.data(), static_cast<nfds_t>(poll_args.size()), 1000);
if (rv < 0){
die("poll");
}
// Process active connection
for (size_t i = 1; i < poll_args.size(); ++i) {
if (poll_args[i].revents) {
Conn *conn = fd2conn[poll_args[i].fd];
connection_io(conn);
if (conn->state == STATE_END) {
// Client closed normally or something bad happened.
fd2conn[conn->fd] = NULL;
static_cast<void>(close(conn->fd));
free(conn);
}
}
}
// Try to accept a new connectio if the listening fd is active
if(poll_args[0].revents) {
static_cast<void>(accept_new_conn(fd2conn, fd));
}
}
return 0;
}