forked from xiaoliang314/libatask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.c
1131 lines (982 loc) · 34 KB
/
httpserver.c
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 <winsock2.h>
#include <ws2tcpip.h>
#include <winerror.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
/* #define TASK_INFO(task, size, used) printf("sizeof(void*):%d task:%p stack size:%d stack used:%d\n", (int)sizeof(void *), (task), (int)(size), (int)(used)) */
/* #define TASK_ASSERT(expr) do { if (!(expr)) {printf("ERROR: stack overflow!!\n"); abort();} } while(0) */
#include "../lib/atask.h"
struct iocp_evt_s
{
event_t event;
OVERLAPPED overlapped;
ULONG_PTR key;
DWORD numberOfBytes;
DWORD dwError;
};
static HANDLE s_iocp = NULL;
static SOCKET s_sock = -1;
#ifndef WSAID_ACCEPTEX
#define WSAID_ACCEPTEX {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
#endif
#ifndef WSAID_GETACCEPTEXSOCKADDRS
#define WSAID_GETACCEPTEXSOCKADDRS { 0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92} }
#endif
typedef BOOL (WINAPI *MY_LPFN_ACCEPTEX)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
typedef VOID (WINAPI *MY_LPFN_GETACCEPTEXSOCKADDRS) (PVOID, DWORD, DWORD, DWORD, struct sockaddr **, LPINT, struct sockaddr **, LPINT);
MY_LPFN_ACCEPTEX lpfnAcceptEx = NULL;
MY_LPFN_GETACCEPTEXSOCKADDRS lpfnGetAcceptExSockAddrs = NULL;
/* IOCP event loop */
/* IOCP事件循环 */
void iocp_atask_run(HANDLE iocp)
{
DWORD dwError;
DWORD numberOfBytes;
ULONG_PTR key;
OVERLAPPED *pOverlapped;
time_nclk_t due, now;
time_ms_t timeout;
struct iocp_evt_s *iocp_evt;
while (1)
{
due = el_schedule();
/* calculate the timeout */
/* 计算超时 */
now = time_nclk_get();
timeout = due < now ? 0 : time_nclk_to_us(due - now) / 1000;
timeout = timeout > UINT32_MAX ? INFINITE : timeout;
/* If IOCP exists, use IOCP timeout,
otherwise use sleep instead */
/* 若IOCP存在,则使用IOCP超时,否则使用Sleep替代 */
if (iocp != NULL)
{
/* Get completed events in the IOCP queue until fully get. */
/* 获取IOCP队列中已完成的事件,直到完全获取 */
while (1)
{
dwError = GetQueuedCompletionStatus(iocp, &numberOfBytes, &key, &pOverlapped, (DWORD)timeout) ? 0 : GetLastError();
/* Post to the event loop if an IOCP event is get. */
/* 若获取到IOCP事件,则提交至事件循环 */
if (pOverlapped != NULL)
{
iocp_evt = container_of(pOverlapped, struct iocp_evt_s, overlapped);
iocp_evt->key = key;
iocp_evt->numberOfBytes = numberOfBytes;
iocp_evt->dwError = dwError;
el_event_post(&iocp_evt->event);
}
else
{
break;
}
timeout = 0;
};
}
else
{
Sleep((DWORD)timeout);
}
}
}
/* Http client data receive timeout */
/* Http客户端数据接收超时 */
#define HTTP_CLIENT_RECV_TIMEOUT_MS 5000
/* Http client request task stack size */
/* Http客户端请求任务的栈大小 */
#define HTTP_CLIENT_REQUST_TASK_STACK_SIZE (256 + 3072)
/* Maximum number of Http clients */
/* Http客户端最大数量 */
#define HTTP_CLIENT_MAX_NUMS 30000
#define SERVER_STRING "Server: libatask httpd 1.0\r\n"
struct http_task_with_stack_s
{
task_t task;
event_t free_ev;
uint8_t stack[HTTP_CLIENT_REQUST_TASK_STACK_SIZE];
};
static uint8_t http_client_tasks_slab_buff[HTTP_CLIENT_REQUST_TASK_STACK_SIZE * HTTP_CLIENT_MAX_NUMS + sizeof(slab_t)];
static slab_t *http_client_tasks_slab = NULL;
/* Get data from the client */
/* 从客户端获取数据 */
void http_client_data_get(task_t *task,
event_t *ev,
SOCKET _cli_sock,
void *_buf,
uint32_t _buf_size,
uint32_t *_recv_size,
time_ms_t _recv_timeout)
{
uint8_t *bpd = TASK_BPD(task);
struct vars
{
SOCKET cli_sock;
timer_event_t timer;
struct iocp_evt_s iocp_ev;
WSABUF wsa_buf;
uint32_t *recv_size;
} *vars = (struct vars *)task_asyn_vars_get(task, sizeof(*vars));
DWORD flags;
DWORD dwError;
/* coroutine begin */
/* 协程开始 */
bpd_begin(2);
/* Save parameters */
/* 保存参数 */
vars->cli_sock = _cli_sock;
vars->recv_size = _recv_size;
/* Initialize the timer */
/* 初始化定时器 */
timer_init_inherit(&vars->timer, &task->event);
/* Initialize IOCP event */
/* 初始化IOCP事件 */
memset(&vars->iocp_ev, 0, sizeof(vars->iocp_ev));
event_init_inherit(&vars->iocp_ev.event, &task->event);
vars->wsa_buf.buf = _buf;
vars->wsa_buf.len = _buf_size;
flags = 0;
/* Start WSARecv opreate */
/* 启动WSARecv操作 */
if (SOCKET_ERROR == WSARecv(vars->cli_sock,
&vars->wsa_buf,
1,
NULL,
&flags,
&vars->iocp_ev.overlapped, NULL))
{
dwError = WSAGetLastError();
if (dwError != ERROR_IO_PENDING)
{
task->ret_val.u32 = dwError;
/* The coroutine end */
/* 协程结束 */
bpd_break;
}
}
/* Enable receive timeout */
/* 开启接收超时 */
el_timer_start_ms(&vars->timer, _recv_timeout);
/* Suspend this coroutine and wait an event. */
/* 挂起协程,等待事件 */
bpd_yield(1);
/* If it is a timeout event, end the request. */
/* 若为超时事件,则结束请求 */
if (ev == &vars->timer.event)
{
/* 若IOCP事件已就绪,但未到达 */
if (el_event_is_ready(&vars->iocp_ev.event))
{
/* Cancel the event from the event loop */
/* 从事件循环中取消该事件 */
el_event_cancel(&vars->iocp_ev.event);
}
else
{
/* Cancel IOCP request */
/* 取消IOCP请求 */
if (FALSE == CancelIo((HANDLE)vars->cli_sock))
{
printf("Warnnig: CancelIo Failed, Error:%u\n", (uint32_t)GetLastError());
}
/* Wait IOCP event return an error */
/* 等待IOCP事件返回错误 */
bpd_yield(2);
*vars->recv_size = 0;
task->ret_val.u32 = WAIT_TIMEOUT;
/* The coroutine end */
/* 协程结束 */
bpd_break;
}
}
/* If it is IOCP event, stop the timer */
/* 若是IOCP事件,停止定时器 */
else
{
el_timer_stop(&vars->timer);
}
task->ret_val.u32 = vars->iocp_ev.dwError;
*vars->recv_size = vars->iocp_ev.numberOfBytes;
/* coroutine end */
/* 协程结束 */
bpd_end();
/* asynchronous return */
/* 异步返回 */
task_asyn_return(task);
}
int mystrcasecmp(const char *s1, const char *s2)
{
while (*s1 && *s2)
{
if (*s1 != *s2
&& tolower(*s1) != tolower(*s2))
{
break;
}
s1++;
s2++;
}
return (signed char)*s1 - (signed char)*s2;
}
static const char http_501_rsp_text[] =
"HTTP/1.0 501 Method Not Implemented\r\n"
SERVER_STRING
"Content-Type: text/html\r\n"
"\r\n"
"<html><head><title>Method Not Implemented\r\n"
"</title></head>\r\n"
"<body><p>HTTP request method not supported.\r\n"
"</p></body></html>\r\n";
static const char http_404_rsp_text[] =
"HTTP/1.0 404 NOT FOUND\r\n"
SERVER_STRING
"Content-Type: text/html\r\n"
"\r\n"
"<html><head><title>Not Found\r\n"
"</title></head>\r\n"
"<body><p>The service could not respond to the path you requested.\r\n"
"</p></body></html>\r\n";
static const char http_ok_rsp_html[] =
"HTTP/1.0 200 OK\r\n"
SERVER_STRING
"Content-Type: text/html\r\n"
"\r\n";
static const char http_ok_rsp_js[] =
"HTTP/1.0 200 OK\r\n"
SERVER_STRING
"Content-Type: application/x-javascript\r\n"
"\r\n";
static const char http_ok_rsp_css[] =
"HTTP/1.0 200 OK\r\n"
SERVER_STRING
"Content-Type: text/css\r\n"
"\r\n";
static const char http_ok_rsp_other[] =
"HTTP/1.0 200 OK\r\n"
SERVER_STRING
"Content-Type: application/octet-stream\r\n"
"\r\n";
/* Http client send data */
/* http客户端发送数据 */
void http_client_send(task_t *task, event_t *ev, SOCKET _cli_sock, const void *_buf, uint32_t _buf_size)
{
uint8_t *bpd = TASK_BPD(task);
struct vars
{
SOCKET cli_sock;
DWORD send_bytes;
WSABUF wsa_buf;
struct iocp_evt_s iocp_ev;
char *buf;
uint32_t buf_size;
} *vars = (struct vars *)task_asyn_vars_get(task, sizeof(*vars));
DWORD dwError;
/* coroutine begin */
/* 协程开始 */
bpd_begin(1);
/* Save parameters */
/* 保存参数 */
vars->buf = (char *)_buf;
vars->buf_size = _buf_size;
vars->cli_sock = _cli_sock;
vars->send_bytes = 0;
/* Initialize IOCP event */
/* 初始化IOCP事件 */
memset(&vars->iocp_ev, 0, sizeof(vars->iocp_ev));
event_init_inherit(&vars->iocp_ev.event, &task->event);
while (vars->send_bytes < vars->buf_size)
{
vars->wsa_buf.buf = vars->buf + vars->send_bytes;
vars->wsa_buf.len = vars->buf_size - vars->send_bytes;
/* Start WSASend opreate */
/* 启动WSASend操作 */
if (SOCKET_ERROR == WSASend(vars->cli_sock,
&vars->wsa_buf,
1,
NULL,
0,
&vars->iocp_ev.overlapped, NULL))
{
dwError = WSAGetLastError();
if (dwError != ERROR_IO_PENDING)
{
task->ret_val.u32 = vars->send_bytes;
/* The coroutine end */
/* 协程结束 */
bpd_break;
}
}
/* Suspend this coroutine and wait an event. */
/* 挂起协程,等待事件 */
bpd_yield(1);
/* send failed */
/* 发送失败 */
if (vars->iocp_ev.dwError != 0 || vars->iocp_ev.numberOfBytes == 0)
{
task->ret_val.u32 = vars->send_bytes;
/* The coroutine end */
/* 协程结束 */
bpd_break;
}
vars->send_bytes += vars->iocp_ev.numberOfBytes;
}
task->ret_val.u32 = vars->send_bytes;
/* coroutine end */
/* 协程结束 */
bpd_end();
/* asynchronous return */
/* 异步返回 */
task_asyn_return(task);
}
/* client requst processing task handler */
/* 客户端请求处理任务函数 */
void http_client_requst_task_handler(task_t *task, event_t *ev, SOCKET _cli_sock, const struct sockaddr_in *_client_addr)
{
uint8_t *bpd = TASK_BPD(task);
struct vars
{
SOCKET cli_sock;
struct sockaddr_in client_addr;
uint8_t buf[2048];
char method[6];
char url[400];
char path[512];
char *query_string;
uint32_t data_size;
uint8_t cgi;
uint8_t bps;
uint8_t header_end;
uint32_t i;
HANDLE hFile;
struct iocp_evt_s read_iocp_evt;
uint64_t offset;
} *vars = (struct vars *)task_asyn_vars_get(task, sizeof(*vars));
uint8_t *buf;
uint8_t *buf_end;
WIN32_FIND_DATAA findInfo;
HANDLE hFind;
const char *str;
/* coroutine begin */
/* 协程开始 */
bpd_begin(14);
/* Save incoming parameters to asynchronous variables */
/* 保存传入的参数至异步变量 */
vars->cli_sock = _cli_sock;
vars->client_addr = *_client_addr;
vars->header_end = 0;
while (1)
{
/* Asynchronously call the http_client_data_get method to get client data with timeout */
/* 异步调用http_client_data_get方法,带超时地获取客户端数据 */
task_bpd_asyn_call(1, task, http_client_data_get,
vars->cli_sock,
vars->buf,
sizeof(vars->buf),
&vars->data_size,
HTTP_CLIENT_RECV_TIMEOUT_MS);
if (task->ret_val.u32 != 0 || vars->data_size == 0)
{
closesocket(vars->cli_sock);
bpd_break;
}
/* Set buf and buf_end for parsing of the bpd block.
* There is a line break in buf,
* then set the buf_end to line break postion.
*/
/* 设置buf, buf_end以供bpd语句块解析。
* buf中存在换行,则设置buf_end换行处
*/
buf = vars->buf;
for (buf_end = buf; buf_end < buf + vars->data_size; buf_end++)
{
if (*buf_end == '\r' || *buf_end == '\n')
{
break;
}
}
/* bpd statement block begin */
/* bpd语句块开始 */
#define BP_STMT_DEFAULT vars->bps
#define BP_STMT_DEFAULT_PREFIX parse_data_label
bpd_stmt_begin(4)
{
/* Filter out the previous white space */
/* 过滤掉前面的空白符 */
do
{
/* Wait the buf is not empty. */
/* 等待buf非空 */
bpd_stmt_yield_until(1, buf < buf_end);
} while (isspace(*buf) && buf++);
/* get method field */
/* 获取method字段 */
vars->i = 0;
while (1)
{
/* Wait the buf is not empty. */
/* 等待buf非空 */
bpd_stmt_yield_until(2, buf < buf_end);
if (!isspace(*buf))
{
if (vars->i < sizeof(vars->method) - 1)
{
vars->method[vars->i++] = *buf;
}
buf++;
}
else
{
break;
}
}
vars->method[vars->i] = 0;
/* Filter out the previous white space */
/* 过滤掉前面的空白符 */
do
{
/* Wait the buf is not empty. */
/* 等待buf非空 */
bpd_stmt_yield_until(3, buf < buf_end);
} while (isspace(*buf) && buf++);
/* get url field */
/* 获取url字段 */
vars->i = 0;
while (1)
{
/* Wait the buf is not empty. */
/* 等待buf非空 */
bpd_stmt_yield_until(4, buf < buf_end);
if (!isspace(*buf))
{
if (vars->i < sizeof(vars->url) - 1)
{
vars->url[vars->i++] = *buf;
}
buf++;
}
else
{
break;
}
}
vars->url[vars->i] = 0;
}
/* bpd statement block end */
/* bpd语句块结束 */
bpd_stmt_end();
#undef BP_STMT_DEFAULT
#undef BP_STMT_DEFAULT_PREFIX
/* In one line, the method and url are not parsed,
http request error */
/* 在一行中,未解析出method和url,http请求错误 */
if (buf_end != vars->buf + vars->data_size
&& vars->bps != 0)
{
closesocket(vars->cli_sock);
bpd_break;
}
/* parse successfully */
/* 解析成功 */
if (vars->bps == BP_STMT_INIT_VAL)
{
break;
}
}
/* Read the full header */
/* 读取整个header */
while (1)
{
/* Check if there is a header end identifier in the previous buf */
/* 检查上一个buf是否存在header结尾标识 */
buf = buf_end;
buf_end = vars->buf + vars->data_size;
/* bpd statement block begin */
/* bpd语句块开始 */
#define BP_STMT_DEFAULT vars->bps
#define BP_STMT_DEFAULT_PREFIX find_header_end
bpd_stmt_begin(3)
{
while (1)
{
bpd_stmt_yield_until(1, buf < buf_end);
if (*buf == '\n')
{
buf++;
bpd_stmt_yield_until(2, buf < buf_end);
if (*buf == '\r')
{
buf++;
bpd_stmt_yield_until(3, buf < buf_end);
}
/* successfully */
/* 成功 */
if (*buf == '\n')
{
bpd_stmt_break;
}
}
else
{
buf++;
}
}
}
/* bpd statement block end */
/* bpd语句块结束 */
bpd_stmt_end();
#undef BP_STMT_DEFAULT
#undef BP_STMT_DEFAULT_PREFIX
/* Read full header finished */
/* 读取整个头部结束 */
if (vars->bps == BP_STMT_INIT_VAL)
{
break;
}
/* Asynchronously call the http_client_data_get method to get client data with timeout */
/* 异步调用http_client_data_get方法,带超时地获取客户端数据 */
task_bpd_asyn_call(2, task, http_client_data_get,
vars->cli_sock,
vars->buf,
sizeof(vars->buf),
&vars->data_size,
HTTP_CLIENT_RECV_TIMEOUT_MS);
if (task->ret_val.u32 != 0 || vars->data_size == 0)
{
closesocket(vars->cli_sock);
bpd_break;
}
buf_end = vars->buf;
}
/* Does not support non-GET and non-POST methods */
/* 不支持非GET和非POST方法 */
if (mystrcasecmp(vars->method, "GET") != 0
&& mystrcasecmp(vars->method, "POST") != 0)
{
task_bpd_asyn_call(3, task, http_client_send,
vars->cli_sock,
http_501_rsp_text,
sizeof(http_501_rsp_text) - 1);
goto client_close;
}
/* Processing URL and parameter information */
/* 处理URL及参数信息 */
if (mystrcasecmp(vars->method, "GET") == 0)
{
vars->query_string = vars->url;
while ((*vars->query_string != '?') && (*vars->query_string != '\0'))
{
vars->query_string++;
}
if (*vars->query_string == '?')
{
vars->cgi = 1;
*vars->query_string = '\0';
vars->query_string++;
}
}
/* Generate Path */
/* 生成Path */
sprintf(vars->path, "wwwroot%s", vars->url);
if (vars->path[strlen(vars->path) - 1] == '/')
{
strcat(vars->path, "index.html");
}
/* Determine if the file exists */
/* 判断文件是否存在 */
hFind = FindFirstFileA(vars->path, &findInfo);
if (hFind == INVALID_HANDLE_VALUE)
{
task_bpd_asyn_call(4, task, http_client_send,
vars->cli_sock,
http_404_rsp_text,
sizeof(http_404_rsp_text) - 1);
goto client_close;
}
FindClose(hFind);
/* Is a directory */
/* 是一个目录 */
if (findInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
strcat(vars->path, "/index.html");
hFind = FindFirstFileA(vars->path, &findInfo);
if (hFind == INVALID_HANDLE_VALUE)
{
task_bpd_asyn_call(5, task, http_client_send,
vars->cli_sock,
http_404_rsp_text,
sizeof(http_404_rsp_text) - 1);
goto client_close;
}
FindClose(hFind);
}
/* Open File */
/* 打开文件 */
vars->hFile = CreateFileA(vars->path,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (vars->hFile == INVALID_HANDLE_VALUE)
{
task_bpd_asyn_call(6, task, http_client_send,
vars->cli_sock,
http_404_rsp_text,
sizeof(http_404_rsp_text) - 1);
goto client_close;
}
/* Add HANDLE to IOCP */
/* 添加HANDLE到IOCP */
if (NULL == CreateIoCompletionPort(vars->hFile, s_iocp, 0, 0))
{
task_bpd_asyn_call(7, task, http_client_send,
vars->cli_sock,
http_404_rsp_text,
sizeof(http_404_rsp_text) - 1);
CloseHandle(vars->hFile);
goto client_close;
}
/* 发送HTTP头 */
/* Send Http header */
str = strrchr(vars->path, '.');
if (str == NULL)
{
task_bpd_asyn_call(8, task, http_client_send,
vars->cli_sock,
http_ok_rsp_other,
sizeof(http_ok_rsp_other) - 1);
}
else if (mystrcasecmp(str, ".htm") == 0 || mystrcasecmp(str, ".html") == 0)
{
task_bpd_asyn_call(9, task, http_client_send,
vars->cli_sock,
http_ok_rsp_html,
sizeof(http_ok_rsp_html) - 1);
}
else if (mystrcasecmp(str, ".js") == 0)
{
task_bpd_asyn_call(10, task, http_client_send,
vars->cli_sock,
http_ok_rsp_js,
sizeof(http_ok_rsp_js) - 1);
}
else if (mystrcasecmp(str, ".css") == 0)
{
task_bpd_asyn_call(11, task, http_client_send,
vars->cli_sock,
http_ok_rsp_css,
sizeof(http_ok_rsp_css) - 1);
}
else
{
task_bpd_asyn_call(12, task, http_client_send,
vars->cli_sock,
http_ok_rsp_other,
sizeof(http_ok_rsp_other) - 1);
}
/***Send file to client***/
/***发送文件到客户端***/
memset(&vars->read_iocp_evt, 0, sizeof(vars->read_iocp_evt));
event_init_inherit(&vars->read_iocp_evt.event, &task->event);
vars->offset = 0;
while (1)
{
vars->read_iocp_evt.overlapped.Offset = vars->offset & 0xFFFFFFFF;
vars->read_iocp_evt.overlapped.OffsetHigh = vars->offset >> 32;
/* Read File */
/* 读文件 */
if (FALSE == ReadFile(vars->hFile,
vars->buf,
sizeof(vars->buf),
NULL,
&vars->read_iocp_evt.overlapped))
{
if (GetLastError() != ERROR_IO_PENDING)
{
CloseHandle(vars->hFile);
goto client_close;
}
}
bpd_yield(13);
/* send to client*/
/* 发送到客户端 */
if (vars->read_iocp_evt.numberOfBytes != 0
&& (vars->read_iocp_evt.dwError == 0 || vars->read_iocp_evt.dwError == ERROR_HANDLE_EOF))
{
task_bpd_asyn_call(14, task, http_client_send,
vars->cli_sock,
vars->buf,
vars->read_iocp_evt.numberOfBytes);
if (task->ret_val.u32 != vars->read_iocp_evt.numberOfBytes)
{
CloseHandle(vars->hFile);
goto client_close;
}
}
if (vars->read_iocp_evt.dwError == ERROR_HANDLE_EOF)
{
CloseHandle(vars->hFile);
goto client_close;
}
vars->offset += vars->read_iocp_evt.numberOfBytes;
}
client_close:
closesocket(vars->cli_sock);
/* coroutine end */
/* 协程结束 */
bpd_end();
/* asynchronous return */
/* 异步返回 */
task_asyn_return(task);
}
static void on_task_end(void *task, event_t *ev)
{
/* free the task */
/* 释放task */
slab_free(http_client_tasks_slab, task);
}
/* Accept task handler */
/* Accept任务函数 */
void http_accept_task_handler(task_t *task, event_t *ev)
{
uint8_t *bpd = TASK_BPD(task);
struct http_task_with_stack_s *task_with_stack;
DWORD dwError;
/* Get or assign asynchronous variables from the task stack.
When the coroutine is yield, the local variables of the function will be destroyed.
While the asynchronous variables will still exist. */
/* 从任务的栈中获取或分配异步变量。
协程在挂起时,函数的局部变量将被销毁,而异步变量则依旧存在 */
struct vars
{
struct iocp_evt_s accept_evt;
char acceptex_out_buffer[(sizeof(struct sockaddr_in) + 16) * 2 + 16];
SOCKET cli_sock;
DWORD dwBytes;
struct sockaddr_in *clientAddr, *localAddr;
int clientAddrLen, localAddrLen;
timer_event_t timer;
slab_alloc_event_t alloc_ev;
} *vars = (struct vars *)task_asyn_vars_get(task, sizeof(*vars));
/* coroutine begin */
/* 协程开始 */
bpd_begin(3);
/* empty accept_evt structure */
/* 清零accept_evt结构体 */
memset(&vars->accept_evt, 0, sizeof(vars->accept_evt));
/* The accept_evt event is inherited from the task,
This accpet_evt event will be process by this coroutine. */
/* accept_evt的事件从task中继承,
这样accpet_evt事件将由本协程处理 */
event_init_inherit(&vars->accept_evt.event, &task->event);
/* Initialize timer */
/* 初始化timer */
timer_init_inherit(&vars->timer, &task->event);
/* Initialize slab alloc event */
/* 初始化slab分配器事件 */
slab_alloc_event_init_inherit(&vars->alloc_ev, &task->event);
/* Initialize http client slab */
/* 初始化http客户端slab */
http_client_tasks_slab = slab_create(http_client_tasks_slab_buff,
sizeof(http_client_tasks_slab_buff),
sizeof(struct http_task_with_stack_s));
while (1)
{
/* Create a socket for the new client */
/* 为新客户端创建一个套接字 */
vars->cli_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (vars->cli_sock == INVALID_SOCKET) {
printf("Create accept socket failed with error: %u\n", WSAGetLastError());
/* Retry after 500 milliseconds */
/* 500毫秒后重试 */
el_timer_start_ms(&vars->timer, 500);
/* Suspend the coroutine and wait for the timer */
/* 挂起协程,并等待定时器 */
bpd_yield(1);
continue;
}
/* Start AcceptEx, When AcceptEx overlapped I/O is completed,
The accept_evt event is triggered and re-enters the coroutine. */
/* 开始AcceptEx,当AcceptEx重叠I/O完成时,
将触发accept_evt事件,重新进入协程 */
if (FALSE == lpfnAcceptEx(s_sock,
vars->cli_sock,
vars->acceptex_out_buffer,
0,
sizeof(struct sockaddr_in) + 16,
sizeof(struct sockaddr_in) + 16,
&vars->dwBytes,
&vars->accept_evt.overlapped))
{
dwError = WSAGetLastError();
if (dwError != ERROR_IO_PENDING)
{
printf("AcceptEx failed! error:%u\n", (uint32_t)dwError);
closesocket(vars->cli_sock);
continue;
}
}
/* Suspend this coroutine and wait an event. */
/* 挂起协程,并等待事件 */
bpd_yield(2);
/* Determine whether the operation is successful */
/* 判断操作是否成功 */
if (vars->accept_evt.dwError != 0)
{
printf("AcceptEx overlapped I/O failed! error:%u\n", (uint32_t)vars->accept_evt.dwError);
closesocket(vars->cli_sock);
continue;
}
/* Getting Client Information */
/* 获取客户端信息 */
lpfnGetAcceptExSockAddrs(vars->acceptex_out_buffer,
0,
sizeof(struct sockaddr_in),
sizeof(struct sockaddr_in),
(LPSOCKADDR *)&vars->localAddr,
&vars->localAddrLen,
(LPSOCKADDR *)&vars->clientAddr,
&vars->clientAddrLen);
/* add socket handle to iocp */
/* 添加Socket句柄到IOCP */
if (NULL == CreateIoCompletionPort((HANDLE)vars->cli_sock, s_iocp, 0, 0))
{
printf("Add socket handle failed! error:%d\n", (uint32_t)GetLastError());
closesocket(vars->cli_sock);
continue;
}
/* Alloc a task from the slab*/
/* 从slab分配一个task */
task_with_stack = (struct http_task_with_stack_s *)slab_alloc(http_client_tasks_slab);
while (task_with_stack == NULL)
{
/* Waiting for slab to be available */
/* 等待slab可用 */
slab_wait(http_client_tasks_slab, &vars->alloc_ev);
bpd_yield(3);
task_with_stack = (struct http_task_with_stack_s *)vars->alloc_ev.mem_blk;
}
/* Initialize the http client request task */
/* 初始化http客户端请求task */
task_init(&task_with_stack->task,
task_with_stack->stack,
sizeof(task_with_stack->stack),
EVENT_PRIORITY(&task->event));
/* The listening task ends and the used memory is released in on_task_end */
/* 监听任务结束,并在on_task_end释放使用的内存 */
event_init(&task_with_stack->free_ev, on_task_end, task_with_stack, MIDDLE_GROUP_PRIORITY);
task_end_wait(&task_with_stack->task, &task_with_stack->free_ev);
/* Process the client Http request */
/* 处理该客户端Http请求 */