-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
1208 lines (1073 loc) · 29.7 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <vector>
#include <iostream>
#include <map>
#include <sys/epoll.h>
#include <math.h>
#include <string>
#include "hashtable.h"
#include "zset.h"
#include "common.h"
#include "list.h"
#include "heap.h"
#include "thread_pool.h"
using namespace std;
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();
}
static uint64_t get_monotonic_usec()
{
timespec tv = {0, 0}; // Describes times in seconds and nanoseconds.
clock_gettime(CLOCK_MONOTONIC, &tv); // CLOCK_REALTIME is affected by NTP, and can move forwards and backwards. CLOCK_MONOTONIC is not, and advances at one tick per tick.
return uint64_t(tv.tv_sec) * 1000000 + tv.tv_nsec / 1000;
}
static void fd_set_nb(int fd)
{
errno = 0;
int flags = fcntl(fd, F_GETFL, 0); // Value of file status flags.
/*
above third argument is ignored as it is not required for F_GETFL
*/
if (errno)
{
die("fcntl error");
return;
}
// printf("flags %d ",flags);
flags |= O_NONBLOCK;
/*
two flags passed open and nonBlocking
flag=2 is 'O_RDWR' which means open file
we want to set non block and open file to out connection
*/
// printf("flags %d %d\n",O_NONBLOCK,flags);
/*
#define F_GETFL 3 Get file status flags.
#define F_SETFL 4 Set file status flags.
*/
errno = 0;
(void)fcntl(fd, F_SETFL, flags);
if (errno)
{
die("fcntl error");
}
}
const size_t k_max_msg = 4096;
enum
{
STATE_REQ = 0,
STATE_RES = 1,
STATE_END = 2, // mark the connection for deletion
};
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];
uint64_t idle_start = 0;
// timer
DList idle_list;
};
// the structure for the key
struct Entry
{
struct HNode node;
std::string key;
std::string val;
uint32_t type = 0;
ZSet *zset = NULL;
// for TTLs
size_t heap_idx = -1;
};
static struct
{
HMap db;
// a map of all client connections, keyed by fd
std::vector<Conn *> fd2conn;
// timers for idle connections
DList idle_list;
// timers for TTLs
std::vector<HeapItem> heap;
// the thread pool
TheadPool tp;
} g_data;
static void conn_put(std::vector<Conn *> &fd2conn, struct Conn *conn)
{
if (fd2conn.size() <= (size_t)conn->fd)
{
fd2conn.resize(conn->fd + 1);
}
fd2conn[conn->fd] = conn;
}
static int32_t accept_new_conn(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->idle_start = get_monotonic_usec();
dlist_insert_before(&g_data.idle_list, &conn->idle_list);
conn_put(g_data.fd2conn, conn);
return 0;
}
static void state_req(Conn *conn);
static void state_res(Conn *conn);
static void entry_set_ttl(Entry *ent, int64_t ttl_ms);
const size_t k_max_args = 1024;
static int32_t parse_req(
const uint8_t *data, size_t len, std::vector<std::string> &out)
{
if (len < 4)
{
return -1;
}
uint32_t n = 0;
memcpy(&n, &data[0], 4);
if (n > k_max_args)
{
return -1;
}
size_t pos = 4;
while (n--)
{
if (pos + 4 > len)
{
return -1;
}
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;
}
cout << "command read in server: ";
for (auto it : out)
cout << it << " ";
cout << endl;
if (pos != len)
{
return -1; // trailing garbage
}
return 0;
}
enum
{
RES_OK = 0,
RES_ERR = 1,
RES_NX = 2,
};
enum
{
T_STR = 0,
T_ZSET = 1,
};
// the structure for the key
// struct Entry {
// struct HNode node;
// std::string key;
// std::string val;
// uint32_t type = 0;
// ZSet *zset = NULL;
// // for TTLs
// size_t heap_idx = -1;
// };
static bool entry_eq(HNode *lhs, HNode *rhs)
{
struct Entry *le = container_of(lhs, struct Entry, node);
struct Entry *re = container_of(rhs, struct Entry, node);
return le->key == re->key;
}
// static uint64_t str_hash(const uint8_t *data, size_t len) {
// uint32_t h = 0x811C9DC5;
// for (size_t i = 0; i < len; i++) {
// h = (h + data[i]) * 0x01000193;
// }
// return h;
// }
enum
{
ERR_UNKNOWN = 1,
ERR_2BIG = 2,
ERR_TYPE = 3,
ERR_ARG = 4,
};
// enum {
// SER_NIL = 0,
// SER_ERR = 1,
// SER_STR = 2,
// SER_INT = 3,
// SER_ARR = 4,
// };
static void out_nil(std::string &out)
{
out.push_back(SER_NIL);
}
static void out_str(std::string &out, const char *s, size_t size)
{
out.push_back(SER_STR);
uint32_t len = (uint32_t)size;
out.append((char *)&len, 4);
out.append(s, len);
}
static void out_str(std::string &out, const std::string &val)
{
return out_str(out, val.data(), val.size());
}
static void out_int(std::string &out, int64_t val)
{
out.push_back(SER_INT);
out.append((char *)&val, 8);
}
static void out_dbl(std::string &out, double val)
{
out.push_back(SER_DBL);
out.append((char *)&val, 8);
}
static void out_err(std::string &out, int32_t code, const std::string &msg)
{
out.push_back(SER_ERR);
out.append((char *)&code, 4);
uint32_t len = (uint32_t)msg.size();
out.append((char *)&len, 4);
out.append(msg);
}
static void out_arr(std::string &out, uint32_t n)
{
out.push_back(SER_ARR);
out.append((char *)&n, 4);
}
static void *begin_arr(std::string &out)
{
out.push_back(SER_ARR);
out.append("\0\0\0\0", 4); // filled in end_arr()
return (void *)(out.size() - 4); // the `ctx` arg
}
static void end_arr(std::string &out, void *ctx, uint32_t n)
{
size_t pos = (size_t)ctx;
assert(out[pos - 1] == SER_ARR);
memcpy(&out[pos], &n, 4);
}
static void do_get(std::vector<std::string> &cmd, std::string &out)
{
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *node = hm_lookup(&g_data.db, &key.node, &entry_eq);
if (!node)
{
return out_nil(out);
}
Entry *ent = container_of(node, Entry, node);
if (ent->type != T_STR)
{
return out_err(out, ERR_TYPE, "expect string type");
}
return out_str(out, ent->val);
}
static void do_set(std::vector<std::string> &cmd, std::string &out)
{
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *node = hm_lookup(&g_data.db, &key.node, &entry_eq);
if (node)
{
Entry *ent = container_of(node, Entry, node);
if (ent->type != T_STR)
{
return out_err(out, ERR_TYPE, "expect string type");
}
ent->val.swap(cmd[2]);
}
else
{
Entry *ent = new Entry();
ent->key.swap(key.key);
ent->node.hcode = key.node.hcode;
ent->val.swap(cmd[2]);
hm_insert(&g_data.db, &ent->node);
}
return out_nil(out);
}
// deallocate the key immediately
static void entry_destroy(Entry *ent)
{
switch (ent->type)
{
case T_ZSET:
zset_dispose(ent->zset);
delete ent->zset;
break;
}
delete ent;
}
static void entry_del_async(void *arg)
{
entry_destroy((Entry *)arg);
}
static void entry_del(Entry *ent)
{
entry_set_ttl(ent, -1);
const size_t k_large_container_size = 10000;
bool too_big = false;
switch (ent->type)
{
case T_ZSET:
too_big = hm_size(&ent->zset->hmap) > k_large_container_size;
break;
}
if (too_big)
{
thread_pool_queue(&g_data.tp, &entry_del_async, ent);
}
else
{
entry_destroy(ent);
}
}
static void do_del(std::vector<std::string> &cmd, std::string &out)
{
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *node = hm_pop(&g_data.db, &key.node, &entry_eq);
if (node)
{
// delete container_of(node, Entry, node);
entry_del(container_of(node, Entry, node));
}
return out_int(out, node ? 1 : 0);
}
static void h_scan(HTab *tab, void (*f)(HNode *, void *), void *arg)
{
if (tab->size == 0)
{
return;
}
for (size_t i = 0; i < tab->mask + 1; ++i)
{
HNode *node = tab->tab[i];
while (node)
{
f(node, arg);
node = node->next;
}
}
}
static void cb_scan(HNode *node, void *arg)
{
std::string &out = *(std::string *)arg;
out_str(out, container_of(node, Entry, node)->key);
}
static void do_keys(std::vector<std::string> &cmd, std::string &out)
{
(void)cmd;
out_arr(out, (uint32_t)hm_size(&g_data.db));
std::cout << out << std::endl;
h_scan(&g_data.db.ht1, &cb_scan, &out);
std::cout << out << std::endl;
h_scan(&g_data.db.ht2, &cb_scan, &out);
std::cout << out << std::endl;
}
static bool str2dbl(const std::string &s, double &out)
{
char *endp = NULL;
out = strtod(s.c_str(), &endp);
return endp == s.c_str() + s.size() && !isnan(out);
}
// set or remove the TTL
static void entry_set_ttl(Entry *ent, int64_t ttl_ms)
{
if (ttl_ms < 0 && ent->heap_idx != (size_t)-1)
{
// erase an item from the heap
// by replacing it with the last item in the array.
size_t pos = ent->heap_idx;
g_data.heap[pos] = g_data.heap.back();
g_data.heap.pop_back();
if (pos < g_data.heap.size())
{
heap_update(g_data.heap.data(), pos, g_data.heap.size());
}
ent->heap_idx = -1;
}
else if (ttl_ms >= 0)
{
size_t pos = ent->heap_idx;
if (pos == (size_t)-1)
{
// add an new item to the heap
HeapItem item;
item.ref = &ent->heap_idx;
g_data.heap.push_back(item);
pos = g_data.heap.size() - 1;
}
g_data.heap[pos].val = get_monotonic_usec() + (uint64_t)ttl_ms * 1000;
heap_update(g_data.heap.data(), pos, g_data.heap.size());
}
}
static bool str2int(const std::string &s, int64_t &out)
{
char *endp = NULL;
out = strtoll(s.c_str(), &endp, 10);
return endp == s.c_str() + s.size();
}
static void do_expire(std::vector<std::string> &cmd, std::string &out)
{
int64_t ttl_ms = 0;
if (!str2int(cmd[2], ttl_ms))
{
return out_err(out, ERR_ARG, "expect int64");
}
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *node = hm_lookup(&g_data.db, &key.node, &entry_eq);
if (node)
{
Entry *ent = container_of(node, Entry, node);
entry_set_ttl(ent, ttl_ms);
}
return out_int(out, node ? 1 : 0);
}
static void do_ttl(std::vector<std::string> &cmd, std::string &out)
{
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *node = hm_lookup(&g_data.db, &key.node, &entry_eq);
if (!node)
{
return out_int(out, -2);
}
Entry *ent = container_of(node, Entry, node);
if (ent->heap_idx == (size_t)-1)
{
return out_int(out, -1);
}
uint64_t expire_at = g_data.heap[ent->heap_idx].val;
uint64_t now_us = get_monotonic_usec();
return out_int(out, expire_at > now_us ? (expire_at - now_us) / 1000 : 0);
}
// zadd zset score name
static void do_zadd(std::vector<std::string> &cmd, std::string &out)
{
double score = 0;
if (!str2dbl(cmd[2], score))
{
return out_err(out, ERR_ARG, "expect fp number");
}
// look up or create the zset
Entry key;
key.key.swap(cmd[1]);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *hnode = hm_lookup(&g_data.db, &key.node, &entry_eq);
Entry *ent = NULL;
if (!hnode)
{
ent = new Entry();
ent->key.swap(key.key);
ent->node.hcode = key.node.hcode;
ent->type = T_ZSET;
ent->zset = new ZSet();
hm_insert(&g_data.db, &ent->node);
}
else
{
ent = container_of(hnode, Entry, node);
if (ent->type != T_ZSET)
{
return out_err(out, ERR_TYPE, "expect zset");
}
}
// add or update the tuple
const std::string &name = cmd[3];
bool added = zset_add(ent->zset, name.data(), name.size(), score);
return out_int(out, (int64_t)added);
}
static bool expect_zset(std::string &out, std::string &s, Entry **ent)
{
Entry key;
key.key.swap(s);
key.node.hcode = str_hash((uint8_t *)key.key.data(), key.key.size());
HNode *hnode = hm_lookup(&g_data.db, &key.node, &entry_eq);
if (!hnode)
{
out_nil(out);
return false;
}
*ent = container_of(hnode, Entry, node);
if ((*ent)->type != T_ZSET)
{
out_err(out, ERR_TYPE, "expect zset");
return false;
}
return true;
}
// zrem zset name
static void do_zrem(std::vector<std::string> &cmd, std::string &out)
{
Entry *ent = NULL;
if (!expect_zset(out, cmd[1], &ent))
{
return;
}
const std::string &name = cmd[2];
ZNode *znode = zset_pop(ent->zset, name.data(), name.size());
if (znode)
{
znode_del(znode);
}
return out_int(out, znode ? 1 : 0);
}
// zscore zset name
static void do_zscore(std::vector<std::string> &cmd, std::string &out)
{
Entry *ent = NULL;
if (!expect_zset(out, cmd[1], &ent))
{
return;
}
const std::string &name = cmd[2];
ZNode *znode = zset_lookup(ent->zset, name.data(), name.size());
return znode ? out_dbl(out, znode->score) : out_nil(out);
}
// zquery zset score name offset limit
static void do_zquery(std::vector<std::string> &cmd, std::string &out)
{
// parse args
double score = 0;
if (!str2dbl(cmd[2], score))
{
return out_err(out, ERR_ARG, "expect fp number");
}
const std::string &name = cmd[3];
int64_t offset = 0;
int64_t limit = 0;
if (!str2int(cmd[4], offset))
{
return out_err(out, ERR_ARG, "expect int");
}
if (!str2int(cmd[5], limit))
{
return out_err(out, ERR_ARG, "expect int");
}
// get the zset
Entry *ent = NULL;
if (!expect_zset(out, cmd[1], &ent))
{
if (out[0] == SER_NIL)
{
out.clear();
out_arr(out, 0);
}
return;
}
// look up the tuple
if (limit <= 0)
{
return out_arr(out, 0);
}
ZNode *znode = zset_query(ent->zset, score, name.data(), name.size());
znode = znode_offset(znode, offset);
// output
void *arr = begin_arr(out);
uint32_t n = 0;
while (znode && (int64_t)n < limit)
{
out_str(out, znode->name, znode->len);
out_dbl(out, znode->score);
znode = znode_offset(znode, +1);
n += 2;
}
end_arr(out, arr, n);
}
static bool cmd_is(const std::string &word, const char *cmd)
{
return 0 == strcasecmp(word.c_str(), cmd);
}
static void do_request(std::vector<std::string> &cmd, std::string &out)
{
if (cmd.size() == 1 && cmd_is(cmd[0], "keys"))
{
do_keys(cmd, out);
}
else if (cmd.size() == 2 && cmd_is(cmd[0], "get"))
{
do_get(cmd, out);
}
else if (cmd.size() == 3 && cmd_is(cmd[0], "set"))
{
do_set(cmd, out);
}
else if (cmd.size() == 2 && cmd_is(cmd[0], "del"))
{
do_del(cmd, out);
}
else if (cmd.size() == 3 && cmd_is(cmd[0], "pexpire"))
{
do_expire(cmd, out);
}
else if (cmd.size() == 2 && cmd_is(cmd[0], "pttl"))
{
do_ttl(cmd, out);
}
else if (cmd.size() == 4 && cmd_is(cmd[0], "zadd"))
{
do_zadd(cmd, out);
}
else if (cmd.size() == 3 && cmd_is(cmd[0], "zrem"))
{
do_zrem(cmd, out);
}
else if (cmd.size() == 3 && cmd_is(cmd[0], "zscore"))
{
do_zscore(cmd, out);
}
else if (cmd.size() == 6 && cmd_is(cmd[0], "zquery"))
{
do_zquery(cmd, out);
}
else
{
// cmd is not recognized
out_err(out, ERR_UNKNOWN, "Unknown cmd");
}
}
static bool try_one_request(Conn *conn)
{
// try to parse a request from the buffer
if (conn->rbuf_size < 4)
{
// not enough 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)
{
// not enough data in the buffer. Will retry in the next iteration
return false;
}
// cout<<"JUST BEFORE calling parse request"<<endl;
// parse the request
std::vector<std::string> cmd;
if (0 != parse_req(&conn->rbuf[4], len, cmd))
{
msg("bad req");
conn->state = STATE_END;
return false;
}
// got one request, generate the response.
std::string out;
// cout<<"JUST BEFORE calling do request"<<endl;
do_request(cmd, out);
// pack the response into the buffer
if (4 + out.size() > k_max_msg)
{
out.clear();
out_err(out, ERR_2BIG, "response is too big");
}
uint32_t wlen = (uint32_t)out.size();
memcpy(&conn->wbuf[0], &wlen, 4);
memcpy(&conn->wbuf[4], out.data(), out.size());
conn->wbuf_size = 4 + wlen;
// remove the request from the buffer.
// note: frequent memmove is inefficient.
// note: need better handling for production code.
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;
// cout<<"JUST BEFORE calling flush"<<endl;
state_res(conn);
// continue the outer loop if the request was fully processed
return (conn->state == STATE_REQ);
}
static bool try_fill_buffer(Conn *conn)
{
// try to fill the buffer
// cout<<"JUST BEFORE calling try fill buffer"<<endl;
assert(conn->rbuf_size < sizeof(conn->rbuf));
ssize_t rv = 0;
// std::cout<<"new try read "<<std::endl;
do
{
size_t cap = sizeof(conn->rbuf) - conn->rbuf_size;
rv = read(conn->fd, &conn->rbuf[conn->rbuf_size], cap);
// rv -> 11 is EAGAIN which means It means "there is no data available right now, try again later".
// std::cout<<cap<<" "<<rv<<" "<<errno<<std::endl;
} while (rv < 0 && errno == EINTR); // #define EINTR -> 4 /* Interrupted system call */
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 += (size_t)rv;
assert(conn->rbuf_size <= sizeof(conn->rbuf));
// cout<<"JUST BEFORE calling tryone request"<<endl;
// Try to process requests one by one.
// Why is there a loop? Please read the explanation of "pipelining".
while (try_one_request(conn))
{
}
return (conn->state == STATE_REQ);
}
static void state_req(Conn *conn)
{
while (try_fill_buffer(conn))
{
}
}
static bool try_flush_buffer(Conn *conn)
{
ssize_t rv = 0;
// cout<<"output is "<<conn->wbuf<<endl;
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 += (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;
}
static void state_res(Conn *conn)
{
while (try_flush_buffer(conn))
{
}
}
static void connection_io(Conn *conn)
{
// waked up by poll, update the idle timer
// by moving conn to the end of the list.
// cout<<"JUST BEFORE calling connection_io"<<endl;
conn->idle_start = get_monotonic_usec();
dlist_detach(&conn->idle_list);
dlist_insert_before(&g_data.idle_list, &conn->idle_list);
if (conn->state == STATE_REQ)
{
state_req(conn);
}
else if (conn->state == STATE_RES)
{
// cout<<"resend req "<<endl;
state_res(conn);
}
else
{
assert(0); // not expected
}
}
const uint64_t k_idle_timeout_ms = 5 * 1000;
static uint32_t next_timer_ms()
{
uint64_t now_us = get_monotonic_usec();
uint64_t next_us = (uint64_t)-1;
// idle timers
if (!dlist_empty(&g_data.idle_list))
{
Conn *next = container_of(g_data.idle_list.next, Conn, idle_list);
next_us = next->idle_start + k_idle_timeout_ms * 1000;
}
// ttl timers
if (!g_data.heap.empty() && g_data.heap[0].val < next_us)
{
next_us = g_data.heap[0].val;
}
if (next_us == (uint64_t)-1)
{
return 10000; // no timer, the value doesn't matter
}
if (next_us <= now_us)
{
// missed?
return 0;
}
return (uint32_t)((next_us - now_us) / 1000);
}
static void conn_done(Conn *conn)
{
g_data.fd2conn[conn->fd] = NULL;
(void)close(conn->fd);
dlist_detach(&conn->idle_list);
free(conn);
}