forked from embedded2016/server-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.c
54 lines (45 loc) · 1.24 KB
/
httpd.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
#include <stdio.h>
#include <stdlib.h>
#define THREAD_COUNT 1
#include "protocol-server.h"
static void on_data(server_pt srv, int fd)
{
static char reply[] =
"HTTP/1.1 200 OK\r\n"
"Content-Length: 12\r\n"
"Connection: keep-alive\r\n"
"Keep-Alive: timeout=2\r\n"
"\r\n"
"Hello World!";
char buff[1024];
if (Server.read(srv, fd, buff, 1024))
Server.write(srv, fd, reply, sizeof(reply));
}
void print_conn(server_pt srv, int fd, void *arg)
{
printf("- Connection to FD: %d\n", fd);
}
void done_printing(server_pt srv, int fd, void *arg)
{
fprintf(stderr, "# Total Clients: %lu\n", Server.count(srv, NULL));
}
void timer_task(server_pt srv)
{
size_t count = Server.each(srv, 0,
NULL, print_conn,
NULL, done_printing);
fprintf(stderr, "Clients: %lu\n", count);
}
void on_init(server_pt srv)
{
Server.run_every(srv, 1000, -1, (void *) timer_task, srv);
}
int main(int argc, char *argv[])
{
struct Protocol protocol = { .on_data = on_data };
start_server(.protocol = &protocol,
.timeout = 2,
.on_init = on_init,
.threads = THREAD_COUNT);
return 0;
}