-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
277 lines (244 loc) · 7.41 KB
/
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "server.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#include "addr.h"
#include "argument.h"
#include "buffer.h"
#include "error.h"
#include "evctx.h"
#include "hashmap.h"
#include "request.h"
#include "threadpool.h"
#define DEFAULT_LISTEN_CNT 128
#define DEFAULT_THREAD_CNT 5
#define MAX_EVENTS 1024
#define MAGIC 0x00686A6C
#define MAGIC_SIZE 4
typedef void (*handler_t)(request_t*, error_t*, struct argument*);
static int init_listenfd(struct server* svr) {
int lfd, ret, opt;
struct sockaddr_in svr_addr;
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd < 0) return lfd;
opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
svr->lfd = lfd;
svr_addr.sin_family = AF_INET;
svr_addr.sin_port = htons(svr->port);
svr_addr.sin_addr.s_addr = svr->ip;
ret = bind(lfd, (struct sockaddr*)&svr_addr, sizeof(struct sockaddr_in));
if (ret < 0) return ret;
ret = listen(lfd, DEFAULT_LISTEN_CNT);
if (ret < 0) return ret;
return 0;
}
static struct evctx* build_evctx(int fd, struct server* svr, buffer_t* buff,
void (*cb)(void*)) {
struct evctx* ctx;
ctx = malloc(sizeof(struct evctx));
ctx->buff = buff;
ctx->callback = cb;
ctx->events = EPOLLIN;
ctx->fd = fd;
ctx->self = ctx;
ctx->svr = svr;
ctx->req = NULL;
ctx->err = error_new();
ctx->magic_read = 0;
return ctx;
}
static void reset_oneshot(struct evctx* ctx) {
struct epoll_event ev;
ev.data.ptr = (void*)ctx;
ev.events = EPOLLIN | EPOLLONESHOT;
epoll_ctl(ctx->svr->epoll_fd, EPOLL_CTL_MOD, ctx->fd, &ev);
}
static void send_response(uint64_t seq, struct argument* arg,
struct evctx* ctx) {
buffer_t* buff = ctx->buff;
error_t* err = &ctx->err;
//用户在Handler中设置错误
if (!err->null) {
arg->type_kind = TYPE_KIND_ERROR;
arg->type_name_len = 0;
arg->data_len = strlen(ctx->err.msg);
arg->data = err->msg;
arg->no_free = 1;
err->null = true;
}
buffer_write(buff, (char*)&seq, 8, err);
buffer_write(buff, (char*)&arg->type_kind, 2, err);
buffer_write(buff, (char*)&arg->type_name_len, 2, err);
buffer_write(buff, (char*)&arg->data_len, 4, err);
buffer_write(buff, arg->type_name, arg->type_name_len, err);
buffer_write(buff, arg->data, arg->data_len, err);
if (!err->null) return;
buffer_flush(buff, err);
return;
}
static void cfd_callback(void* arg) {
int ret;
uint32_t magic;
struct evctx* ctx;
struct request* req;
handler_t handler;
struct argument resp;
// read data into buffer
ctx = (struct evctx*)arg;
buffer_fill(ctx->buff, &ctx->err);
if (!ctx->err.null) goto bad;
// read magic number
if (!ctx->magic_read) {
if (buffer_buffered(ctx->buff) < 4) goto not_ready;
magic = *(uint32_t*)(ctx->buff->buf + ctx->buff->front);
if (magic != MAGIC) {
error_put(&ctx->err, ERR_INVALID_MAGIC);
goto bad;
}
buffer_drop(ctx->buff, 4);
ctx->magic_read = 1;
}
// read request
while (1) {
ret = read_request(ctx->req, ctx->buff, &ctx->err);
if (!ctx->err.null) goto bad;
if (ret == 0) goto not_ready;
req = ctx->req;
handler =
(handler_t)hashmap_get(ctx->svr->services, req->service_method);
if (handler == NULL) {
error_put(&ctx->err, ERR_BAD_REQUEST_SERVICE);
goto bad;
}
argument_init(&resp);
handler(req, &ctx->err, &resp);
send_response(req->seq, &resp, ctx);
argument_destroy(&resp);
if (!ctx->err.null) goto bad;
request_set_init_state(ctx->req);
}
not_ready:
reset_oneshot(ctx);
return;
bad:
// TODO error handling
// printf("err :%s\n", ctx->err.msg);
epoll_ctl(ctx->svr->epoll_fd, EPOLL_CTL_DEL, ctx->fd, NULL);
evctx_destroy(ctx);
}
static void lfd_callback(void* arg) {
int cfd;
struct sockaddr_in cli_addr;
struct epoll_event ev;
struct buffer* buff;
struct evctx *lfd_ctx, *ctx;
lfd_ctx = (struct evctx*)arg;
socklen_t cli_addr_len = sizeof(struct sockaddr_in);
cfd = accept(lfd_ctx->svr->lfd, (struct sockaddr*)&cli_addr, &cli_addr_len);
if (cfd < 0) perror("accept failed");
reset_oneshot(lfd_ctx);
conn_t* conn = conn_new(cfd, &cli_addr);
buff = buffer_new(conn, 4096, 4096);
ctx = build_evctx(cfd, lfd_ctx->svr, buff, cfd_callback);
ctx->req = request_new();
ev.data.ptr = ctx;
ev.events = EPOLLIN | EPOLLONESHOT;
epoll_ctl(lfd_ctx->svr->epoll_fd, EPOLL_CTL_ADD, cfd, &ev);
}
static int init_epollfd(struct server* svr) {
int ret;
int epoll_fd;
struct epoll_event ev;
epoll_fd = epoll_create(1);
if (epoll_fd < 0) return epoll_fd;
svr->epoll_fd = epoll_fd;
ev.data.ptr = build_evctx(svr->lfd, svr, NULL, lfd_callback);
ev.events = EPOLLIN | EPOLLONESHOT;
ret = epoll_ctl(svr->epoll_fd, EPOLL_CTL_ADD, svr->lfd, &ev);
return ret;
}
static void init(struct server* svr, struct server_attr* attr, error_t* err) {
int ret;
svr->pool = pool_init(attr->thread_cnt);
ret = init_listenfd(svr);
if (ret != 0) goto bad;
ret = init_epollfd(svr);
if (ret != 0) goto bad;
return;
bad:
error_put(err, strerror(errno));
}
struct server* server_create() {
struct server* svr;
svr = malloc(sizeof(struct server));
svr->lfd = -1;
svr->epoll_fd = -1;
svr->port = 0;
svr->services = hashmap_init(NULL, Type_String);
return svr;
}
void server_destroy(struct server* svr) {
if (svr->lfd != -1) {
close(svr->lfd);
}
if (svr->epoll_fd != -1) {
close(svr->epoll_fd);
}
pool_destory(svr->pool);
hashmap_destroy(svr->services);
free(svr);
}
static inline void init_default_attr(struct server_attr* attr) {
attr->thread_cnt = DEFAULT_THREAD_CNT;
}
void server_listen(struct server* svr, const char* addr,
struct server_attr* attr, error_t* err) {
int ready, i;
struct epoll_event evs[MAX_EVENTS];
struct evctx* ctx;
struct addr_v4 add;
struct server_attr _attr;
if (!err) {
error_t e = error_new();
err = &e;
}
parse_addr(&add, addr, err);
if (!err->null) return;
svr->ip = add.ip;
svr->port = add.port;
if (attr == NULL) {
init_default_attr(&_attr);
attr = &_attr;
}
init(svr, attr, err);
if (!err->null) return;
while (1) {
ready = epoll_wait(svr->epoll_fd, evs, MAX_EVENTS, -1);
if (ready < 0) {
errorf(err, "%s: epoll_wait failed: %s", __func__, strerror(errno));
return;
}
for (i = 0; i < ready; i++) {
if (!(evs[i].events & EPOLLIN)) continue;
ctx = (struct evctx*)(evs[i].data.ptr);
pool_put(svr->pool, ctx->callback, ctx);
}
}
}
void build_resp(struct argument* resp, uint16_t type_kind,
const char* type_name, uint32_t data_len, char* data) {
resp->type_kind = type_kind;
resp->type_name_len = strlen(type_name);
resp->data_len = data_len;
resp->type_name = strdup(type_name);
resp->data = malloc(data_len);
memcpy(resp->data, data, data_len);
}