This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
272 lines (264 loc) · 8.14 KB
/
main.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
#define _GNU_SOURCE
#include "http.h"
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define MAX_EVENTS 10
#define WORKER_COUNT 8
#define MAX_CONNECTION 1024
struct connection_context
{
int bytes_sent;
struct http_response *response;
char ip_str[16];
int port;
};
// fd as index
struct connection_context connection_contexts[MAX_CONNECTION + 64];
struct thread_info
{
pthread_t thread_id; /* ID returned by pthread_create() */
int thread_num; /* Application-defined thread # */
};
int worker_epollfd[WORKER_COUNT];
char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch (sa->sa_family)
{
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), s, maxlen);
break;
default:
strncpy(s, "Unknown AF", maxlen);
return NULL;
}
return s;
}
void signal_handler()
{
}
void *worker(void *arg)
{
struct thread_info *tinfo = arg;
int worker_id = tinfo->thread_num;
int epollfd = worker_epollfd[worker_id];
struct epoll_event events[MAX_EVENTS];
int nfds;
char *recv_buffer = malloc(8192);
for (;;)
{
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds == -1)
{
if (errno == EINTR)
{
close(epollfd);
free(recv_buffer);
pthread_exit(NULL);
}
else
{
perror("epoll_pwait");
exit(EXIT_FAILURE);
}
}
for (int n = 0; n < nfds; ++n)
{
int fd = events[n].data.fd;
if (events[n].events & EPOLLIN)
{
// printf("EPOLLIN\n");
int recv_ret;
recv_ret = recv(fd, recv_buffer, 8192, MSG_DONTWAIT);
if (recv_ret == 0 || (recv_ret == -1 && errno != EAGAIN))
{
close(fd);
}
else
{
recv_buffer[recv_ret] = 0;
struct http_response *p =
handle_request(recv_buffer, connection_contexts[fd].ip_str, connection_contexts[fd].port);
int ret = send(fd, p->content, p->length, MSG_DONTWAIT);
if ((ret != p->length) && errno == EAGAIN)
{
connection_contexts[fd].response = p;
connection_contexts[fd].bytes_sent = ret;
}
else
{
destroy_http_response(p);
connection_contexts[fd].response = NULL;
connection_contexts[fd].bytes_sent = 0;
}
}
}
else if (events[n].events & EPOLLOUT)
{
// printf("EPOLLOUT\n");
struct http_response *p = connection_contexts[fd].response;
if (!p)
{
// printf("%d enter continue\n", fd);
continue;
}
int bytes_sent = connection_contexts[fd].bytes_sent;
int ret = send(fd, p->content + bytes_sent, p->length - bytes_sent, MSG_DONTWAIT);
if ((ret != p->length - bytes_sent) && errno == EAGAIN)
{
connection_contexts[fd].bytes_sent += ret;
}
else
{
destroy_http_response(p);
connection_contexts[fd].response = NULL;
connection_contexts[fd].bytes_sent = 0;
}
}
}
}
}
int main(int argc, char *argv[])
{
init_log();
printf("%d workers, use round robin\n", WORKER_COUNT);
struct epoll_event ev, events[MAX_EVENTS];
int listen_sock, conn_sock, nfds, epollfd;
struct sockaddr_in servaddr;
signal(SIGINT, signal_handler);
/* socket(), bind() and listen() */
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(8080);
int flag = 1;
if (-1 == setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
if (bind(listen_sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind");
exit(EXIT_FAILURE);
}
listen(listen_sock, MAX_CONNECTION);
/* create master epoll */
epollfd = epoll_create(32);
if (epollfd == -1)
{
perror("epoll_create");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN;
ev.data.fd = listen_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1)
{
perror("epoll_ctl: listen_sock");
exit(EXIT_FAILURE);
}
/* create worker epoll */
for (int i = 0; i < WORKER_COUNT; i++)
{
worker_epollfd[i] = epoll_create(32);
if (worker_epollfd[i] == -1)
{
perror("epoll_create");
exit(EXIT_FAILURE);
}
}
/* create worker threads */
int s, num_threads;
num_threads = WORKER_COUNT;
struct thread_info *tinfo = calloc(num_threads, sizeof(*tinfo));
if (tinfo == NULL)
{
perror("calloc");
exit(EXIT_FAILURE);
}
for (int tnum = 0; tnum < num_threads; tnum++)
{
tinfo[tnum].thread_num = tnum;
/* The pthread_create() call stores the thread ID into
corresponding element of tinfo[] */
s = pthread_create(&tinfo[tnum].thread_id, NULL, &worker, &tinfo[tnum]);
if (s != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
char ip_str[32];
unsigned int chosen_worker = 0;
for (;;)
{
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds == -1)
{
/* EINTR */
if (errno == EINTR)
{
printf("master received SIGINT, stopping workers\n");
for (int i = 0; i < WORKER_COUNT; ++i)
{
pthread_kill(tinfo[i].thread_id, SIGINT);
}
/* block until all threads complete */
for (int i = 0; i < WORKER_COUNT; ++i)
{
pthread_join(tinfo[i].thread_id, NULL);
}
close(listen_sock);
close(epollfd);
exit(EXIT_SUCCESS);
}
else
{
perror("epoll_pwait");
exit(EXIT_FAILURE);
}
}
for (int n = 0; n < nfds; ++n)
{
struct sockaddr local;
socklen_t addrlen = sizeof(local);
// use accept4 for SOCK_NONBLOCK
conn_sock = accept4(listen_sock, &local, &addrlen, SOCK_NONBLOCK);
// conn_sock = accept(listen_sock, &local, &addrlen);
if (conn_sock == -1)
{
perror("accept");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.fd = conn_sock;
// round robin scheduling
chosen_worker = (chosen_worker + 1) % WORKER_COUNT;
get_ip_str(&local, ip_str, sizeof(ip_str));
// printf("[%d] accept %s, count=%d\n", tid, ip_str, c++);
strcpy(connection_contexts[conn_sock].ip_str, ip_str);
connection_contexts[conn_sock].port = ((struct sockaddr_in *)&local)->sin_port;
if (epoll_ctl(worker_epollfd[chosen_worker], EPOLL_CTL_ADD, conn_sock, &ev) == -1)
{
perror("epoll_ctl: conn_sock");
exit(EXIT_FAILURE);
}
}
}
return 0;
}