-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
277 lines (228 loc) · 6.65 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 <stdint.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#define PORT 7000
#define MAX_EVENTS 256
#define BUFLEN 1024
#define WNUM 4
struct Packet {
struct sockaddr_in peer_addr;
char msg[BUFLEN];
};
struct Worker {
pthread_t t_id;
int id;
int cfd; //control fd
int epollfd;
int processed_msg_num;
uint16_t port;
};
struct Server {
struct Worker* w[WNUM];
int cfd[WNUM][2];
int listenfd;
int epollfd;
int processed_msg_num;
uint16_t port;
};
typedef void (*handle_event)(void* arg);
struct EventFunc {
handle_event handle;
void* arg;
};
void handle_msg_from_client(void* data)
{
struct sockaddr_in remote_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);
int fd = (unsigned long)data;
int ret;
char buf[BUFLEN];
//print
memset(buf, 0, BUFLEN);
ret = recvfrom(fd, &buf, sizeof(buf), 0, (struct sockaddr*)&remote_addr, &addr_len);
if(ret <= 0) {
perror("handle fd");
dlog("read from remote peer: %d\n", ret);
return;
}
dlog("msg from remote %s:%d, fd: %d, msg: %s", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port), fd, buf);
}
void handle_msg_from_server(void* data)
{
struct Worker* w = (struct Worker*)data;
struct Packet pkt;
int ret, listenfd;
struct epoll_event ev;
ret = read(w->cfd, &pkt, sizeof(pkt));
if(ret != sizeof(pkt)) {
dlog("read from cfd: %d, len %d was not pkt size\n", w->cfd, ret);
return;
}
listenfd = create_udp_fd(w->port);
if(listenfd < 0) {
dlog("create udp fd failed\n");
return;
}
//connect remote peer
if(connect(listenfd, (struct sockaddr*)&pkt.peer_addr, sizeof(pkt.peer_addr)) == -1) {
perror("connect");
dlog("connect remote addr failed\n");
return;
}
//get msg and remote addr
dlog("thread id: %d, connect with remote addr, (fd: %d <---> remote_port: %d), msg from main thread, msg: %s", w->id, listenfd, ntohs(pkt.peer_addr.sin_port), pkt.msg);
//add event
struct EventFunc* event_func = (struct EventFunc*)malloc(sizeof(struct EventFunc));
event_func->handle = handle_msg_from_client;
event_func->arg = (void*)(unsigned long)listenfd;
//add
ev.data.ptr = event_func;
ev.events = EPOLLIN;
if(epoll_ctl(w->epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1){
perror("epoll_ctl");
dlog("epoll_ctl failed\n");
return;
}
}
void* thread_func(void* arg)
{
int nfds, n;
struct epoll_event ev, events[MAX_EVENTS];
struct Worker* w = (struct Worker*)arg;
dlog("create worker\n");
w->epollfd = epoll_create1(0);
if(w->epollfd == -1) {
perror("epoll_create1");
dlog("epoll create failed\n");
return NULL;
}
//
struct EventFunc* event_func = (struct EventFunc*)malloc(sizeof(struct EventFunc));
event_func->handle = handle_msg_from_server;
event_func->arg = w;
//add
ev.data.ptr = event_func;
ev.events = EPOLLIN;
if(epoll_ctl(w->epollfd, EPOLL_CTL_ADD, w->cfd, &ev) == -1){
perror("epoll_ctl");
dlog("epoll_ctl failed\n");
return NULL;
}
for(;;) {
nfds = epoll_wait(w->epollfd, events, MAX_EVENTS, -1);
for(n = 0; n < nfds; ++n) {
struct EventFunc* func = (struct EventFunc*)events[n].data.ptr;
func->handle(func->arg);
}
}
}
struct Worker* create_worker(int cfd, uint16_t port, int id)
{
struct Worker *w = (struct Worker*)malloc(sizeof(struct Worker));
int ret;
memset(w, 0, sizeof(struct Worker));
w->cfd = cfd;
w->port = port;
w->id = id;
ret = pthread_create(&w->t_id, NULL, thread_func, w);
if(ret != 0) {
dlog("pthread create error, ret: %d\n", ret);
return NULL;
}
return w;
}
struct Server* server_init(uint16_t port)
{
struct Server* server = NULL;
int listenfd;
int i;
//
server = (struct Server*)malloc(sizeof(struct Server));
memset(server, 0, sizeof(struct Server));
server->port = port;
listenfd = create_udp_fd(server->port);
if(listenfd < 0) {
dlog("create udp fd failed\n");
return NULL;
}
server->listenfd = listenfd;
//create socketpair, create worker
for(i = 0; i < WNUM; ++i) {
if(socketpair(AF_UNIX, SOCK_DGRAM, 0, server->cfd[i]) < 0){
perror("socketpair");
dlog("create socketpair failed, i: %d\n", i);
return NULL;
}
server->w[i] = create_worker(server->cfd[i][1], server->port, i);
if(server->w[i] == NULL) {
dlog("create worker failed, i: %d\n", i);
return NULL;
}
}
return server;
}
void server_handle_msg(void* data) {
char buf[BUFLEN];
int ret, index;
struct Server* server = (struct Server*)data;
struct Packet pkt;
struct sockaddr_in remote_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);
memset(buf, 0, BUFLEN);
ret = recvfrom(server->listenfd, &buf, sizeof(buf), 0, (struct sockaddr*)&remote_addr, &addr_len);
if(ret <= 0) {
perror("handle fd");
dlog("read from remote peer: %d\n", ret);
return;
}
memcpy(pkt.msg, buf, BUFLEN);
pkt.peer_addr = remote_addr;
//hash
index = ntohs(remote_addr.sin_port) % WNUM;
//post to client
dlog("main thread, msg from remote port: %d, msg: %s, hash to thread: %d", ntohs(remote_addr.sin_port), buf, server->w[index]->id);
//post
write(server->cfd[index][0], &pkt, sizeof(pkt));
}
void server_run(struct Server* server)
{
int nfds, n;
struct epoll_event ev, events[MAX_EVENTS];
server->epollfd = epoll_create1(0);
if(server->epollfd == -1) {
perror("epoll_create1");
dlog("epoll create failed\n");
return;
}
//
struct EventFunc* event_func = (struct EventFunc*)malloc(sizeof(struct EventFunc));
event_func->handle = server_handle_msg;
event_func->arg = server;
//add
ev.data.ptr = event_func;
ev.events = EPOLLIN;
if(epoll_ctl(server->epollfd, EPOLL_CTL_ADD, server->listenfd, &ev) == -1){
perror("epoll_ctl");
return;
}
//wait
for(;;) {
nfds = epoll_wait(server->epollfd, events, MAX_EVENTS, -1);
for(n = 0; n < nfds; ++n) {
struct EventFunc* func = (struct EventFunc*)events[n].data.ptr;
func->handle(func->arg);
}
}
}
void run(short port) {
struct Server* server = server_init(port);
server_run(server);
}