-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp-echo-server.c
257 lines (210 loc) · 5.85 KB
/
tcp-echo-server.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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <event.h>
#include <signal.h>
#include "workqueue.h"
#define SERVER_PORT 6666
#define CONNECTION_BACKLOG 8
#define NUM_THREADS 8
#define errorOut(...) {\
fprintf(stderr, "%s:%d: %s():\t", __FILE__, __LINE__, __FUNCTION__);\
fprintf(stderr, __VA_ARGS__);\
}
typedef struct client {
int fd;
struct event_base *evbase;
struct bufferevent *buf_ev;
struct evbuffer *output_buffer;
} client_t;
static struct event_base *evbase_accept;
static workqueue_t workqueue;
static void sighandler(int signal);
static int setnonblock(int fd) {
int flags;
flags = fcntl(fd, F_GETFL);
if (flags < 0) return flags;
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) return -1;
return 0;
}
static void closeClient(client_t *client) {
if (client != NULL) {
if (client->fd >= 0) {
close(client->fd);
client->fd = -1;
}
}
}
static void closeAndFreeClient(client_t *client) {
if (client != NULL) {
closeClient(client);
if (client->buf_ev != NULL) {
bufferevent_free(client->buf_ev);
client->buf_ev = NULL;
}
if (client->evbase != NULL) {
event_base_free(client->evbase);
client->evbase = NULL;
}
if (client->output_buffer != NULL) {
evbuffer_free(client->output_buffer);
client->output_buffer = NULL;
}
free(client);
}
}
void buffered_on_read(struct bufferevent *bev, void *arg) {
client_t *client = (client_t *)arg;
char data[4096];
int nbytes;
while (bev->input->off > 0) {
nbytes = (bev->input->off > 4096) ? 4096 : bev->input->off;
evbuffer_remove(bev->input, data, nbytes);
evbuffer_add(client->output_buffer, data, nbytes);
}
if (bufferevent_write_buffer(bev, client->output_buffer)) {
errorOut("Error sending data to client on fd %d\n", client->fd);
closeClient(client);
}
}
void buffered_on_write(struct bufferevent *bev, void *arg) {
}
void buffered_on_error(struct bufferevent *bev, short what, void *arg) {
closeClient((client_t *)arg);
}
static void server_job_function(struct job *job) {
client_t *client = (client_t *)job->user_data;
event_base_dispatch(client->evbase);
closeAndFreeClient(client);
free(job);
}
void on_accept(int fd, short ev, void *arg) {
int client_fd;
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
workqueue_t *workqueue = (workqueue_t *)arg;
client_t *client;
job_t *job;
client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd < 0) {
warn("accept failed");
return;
}
if (setnonblock(client_fd) < 0) {
warn("failed to set client socket to non-blocking");
close(client_fd);
return;
}
if ((client = malloc(sizeof(*client))) == NULL) {
warn("failed to allocate memory for client state");
close(client_fd);
return;
}
memset(client, 0, sizeof(*client));
client->fd = client_fd;
if ((client->output_buffer = evbuffer_new()) == NULL) {
warn("client output buffer allocation failed");
closeAndFreeClient(client);
return;
}
if ((client->evbase = event_base_new()) == NULL) {
warn("client event_base creation failed");
closeAndFreeClient(client);
return;
}
if ((client->buf_ev = bufferevent_new(client_fd, buffered_on_read, buffered_on_write, buffered_on_error, client)) == NULL) {
warn("client bufferevent creation failed");
closeAndFreeClient(client);
return;
}
bufferevent_base_set(client->evbase, client->buf_ev);
bufferevent_enable(client->buf_ev, EV_READ);
if ((job = malloc(sizeof(*job))) == NULL) {
warn("failed to allocate memory for job state");
closeAndFreeClient(client);
return;
}
job->job_function = server_job_function;
job->user_data = client;
workqueue_add_job(workqueue, job);
}
int runServer(void) {
int listenfd;
struct sockaddr_in listen_addr;
struct event ev_accept;
int reuseaddr_on;
event_init();
sigset_t sigset;
sigemptyset(&sigset);
struct sigaction siginfo = {
.sa_handler = sighandler,
.sa_mask = sigset,
.sa_flags = SA_RESTART,
};
sigaction(SIGINT, &siginfo, NULL);
sigaction(SIGTERM, &siginfo, NULL);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
err(1, "listen failed");
}
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(SERVER_PORT);
if (bind(listenfd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
err(1, "bind failed");
}
if (listen(listenfd, CONNECTION_BACKLOG) < 0) {
err(1, "listen failed");
}
reuseaddr_on = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on));
if (setnonblock(listenfd) < 0) {
err(1, "failed to set server socket to non-blocking");
}
if ((evbase_accept = event_base_new()) == NULL) {
perror("Unable to create socket accept event base");
close(listenfd);
return 1;
}
if (workqueue_init(&workqueue, NUM_THREADS)) {
perror("Failed to create work queue");
close(listenfd);
workqueue_shutdown(&workqueue);
return 1;
}
event_set(&ev_accept, listenfd, EV_READ|EV_PERSIST, on_accept, (void *)&workqueue);
event_base_set(evbase_accept, &ev_accept);
event_add(&ev_accept, NULL);
printf("Server running.\n");
event_base_dispatch(evbase_accept);
event_base_free(evbase_accept);
evbase_accept = NULL;
close(listenfd);
printf("Server shutdown.\n");
return 0;
}
void killServer(void) {
fprintf(stdout, "Stopping socket listener event loop.\n");
if (event_base_loopexit(evbase_accept, NULL)) {
perror("Error shutting down server");
}
fprintf(stdout, "Stopping workers.\n");
workqueue_shutdown(&workqueue);
}
static void sighandler(int signal) {
fprintf(stdout, "Received signal %d: %s. Shutting down.\n", signal, strsignal(signal));
killServer();
}
int main(int argc, char *argv[]) {
return runServer();
}