This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver2.c
93 lines (66 loc) · 1.83 KB
/
server2.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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ev.h>
#include "http_connection.h"
#define STRINGIZE_DETAIL(x) #x
#define STRINGIZE(x) STRINGIZE_DETAIL(x)
#define DIE_IF(x) ((x) ? (perror(__FILE__ ":" STRINGIZE(__LINE__)), exit(1)) : 0)
#define DIE_LZ(x) DIE_IF((x) < 0)
#define TRACE printf(__FILE__ ":" STRINGIZE(__LINE__) "\n");
#define UNUSED(x) (void)(x)
struct ev_loop *event_loop;
int bind_socket(int socket, uint16_t port)
{
struct sockaddr_in address = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {
.s_addr = 0
}
};
int reuse_addr = 1;
setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
return bind(socket, (struct sockaddr *) &address, sizeof(address));
}
int setup_listener(int port)
{
int listener = socket(AF_INET, SOCK_STREAM, 0);
DIE_LZ(listener);
set_nonblock(listener);
DIE_LZ(bind_socket(listener, port));
DIE_LZ(listen(listener, 100));
return listener;
}
void listener_callback(EV_P_ ev_io *w, int revents)
{
UNUSED(loop);
UNUSED(revents);
int connection = accept(w->fd, NULL, 0);
if (connection < 0 && errno == EWOULDBLOCK)
return;
DIE_LZ(connection);
new_http_connection(connection);
}
int main(void)
{
setup_http_parser_settings();
signal(SIGPIPE, SIG_IGN);
int listener = setup_listener(9000);
event_loop = ev_default_loop(EVBACKEND_EPOLL);
ev_io listener_watcher;
ev_io_init(&listener_watcher, listener_callback, listener, EV_READ);
ev_io_start(event_loop, &listener_watcher);
ev_run(event_loop, 0);
puts("finishing loop");
DIE_LZ(shutdown(listener, SHUT_RDWR));
DIE_LZ(close(listener));
return 0;
}