-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathserver_mulport_mulepoll.c
597 lines (444 loc) · 12.6 KB
/
server_mulport_mulepoll.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* author : wangbojing
* email : [email protected]
* 测试操作系统的并发量
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/poll.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <pthread.h>
#define SERVER_PORT 8080
#define SERVER_IP "127.0.0.1"
#define MAX_BUFFER 128
#define MAX_EPOLLSIZE 100000
#define MAX_THREAD 80
#define MAX_PORT 100
#define CPU_CORES_SIZE 8
#define TIME_SUB_MS(tv1, tv2) ((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000)
static int ntySetNonblock(int fd) {
int flags;
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) return flags;
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) return -1;
return 0;
}
static int ntySetReUseAddr(int fd) {
int reuse = 1;
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
}
static int ntySetAlive(int fd) {
int alive = 1;
int idle = 60;
int interval = 5;
int count = 2;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&alive, sizeof(alive));
setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void*)&idle, sizeof(idle));
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void*)&interval, sizeof(interval));
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void*)&count, sizeof(count));
}
/** **** ******** **************** thread pool **************** ******** **** **/
#define LL_ADD(item, list) { \
item->prev = NULL; \
item->next = list; \
list = item; \
}
#define LL_REMOVE(item, list) { \
if (item->prev != NULL) item->prev->next = item->next; \
if (item->next != NULL) item->next->prev = item->prev; \
if (list == item) list = item->next; \
item->prev = item->next = NULL; \
}
typedef struct worker {
pthread_t thread;
int terminate;
struct workqueue *workqueue;
struct worker *prev;
struct worker *next;
} worker_t;
typedef struct job {
void (*job_function)(struct job *job);
void *user_data;
struct job *prev;
struct job *next;
} job_t;
typedef struct workqueue {
struct worker *workers;
struct job *waiting_jobs;
pthread_mutex_t jobs_mutex;
pthread_cond_t jobs_cond;
} workqueue_t;
static void *worker_function(void *ptr) {
worker_t *worker = (worker_t *)ptr;
job_t *job;
while (1) {
pthread_mutex_lock(&worker->workqueue->jobs_mutex);
while (worker->workqueue->waiting_jobs == NULL) {
if (worker->terminate) break;
pthread_cond_wait(&worker->workqueue->jobs_cond, &worker->workqueue->jobs_mutex);
}
if (worker->terminate) break;
job = worker->workqueue->waiting_jobs;
if (job != NULL) {
LL_REMOVE(job, worker->workqueue->waiting_jobs);
}
pthread_mutex_unlock(&worker->workqueue->jobs_mutex);
if (job == NULL) continue;
/* Execute the job. */
job->job_function(job);
}
free(worker);
pthread_exit(NULL);
}
int workqueue_init(workqueue_t *workqueue, int numWorkers) {
int i;
worker_t *worker;
pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
if (numWorkers < 1) numWorkers = 1;
memset(workqueue, 0, sizeof(*workqueue));
memcpy(&workqueue->jobs_mutex, &blank_mutex, sizeof(workqueue->jobs_mutex));
memcpy(&workqueue->jobs_cond, &blank_cond, sizeof(workqueue->jobs_cond));
for (i = 0; i < numWorkers; i++) {
if ((worker = malloc(sizeof(worker_t))) == NULL) {
perror("Failed to allocate all workers");
return 1;
}
memset(worker, 0, sizeof(*worker));
worker->workqueue = workqueue;
if (pthread_create(&worker->thread, NULL, worker_function, (void *)worker)) {
perror("Failed to start all worker threads");
free(worker);
return 1;
}
LL_ADD(worker, worker->workqueue->workers);
}
return 0;
}
void workqueue_shutdown(workqueue_t *workqueue) {
worker_t *worker = NULL;
for (worker = workqueue->workers; worker != NULL; worker = worker->next) {
worker->terminate = 1;
}
pthread_mutex_lock(&workqueue->jobs_mutex);
workqueue->workers = NULL;
workqueue->waiting_jobs = NULL;
pthread_cond_broadcast(&workqueue->jobs_cond);
pthread_mutex_unlock(&workqueue->jobs_mutex);
}
void workqueue_add_job(workqueue_t *workqueue, job_t *job) {
pthread_mutex_lock(&workqueue->jobs_mutex);
LL_ADD(job, workqueue->waiting_jobs);
pthread_cond_signal(&workqueue->jobs_cond);
pthread_mutex_unlock(&workqueue->jobs_mutex);
}
static workqueue_t workqueue;
void threadpool_init(void) {
workqueue_init(&workqueue, MAX_THREAD);
}
/** **** ******** **************** thread pool **************** ******** **** **/
typedef struct client {
int fd;
char rBuffer[MAX_BUFFER];
int length;
} client_t;
void *client_cb(void *arg) {
int clientfd = *(int *)arg;
char buffer[MAX_BUFFER] = {0};
int childpid = getpid();
while (1) {
bzero(buffer, MAX_BUFFER);
ssize_t length = recv(clientfd, buffer, MAX_BUFFER, 0); //bio
if (length > 0) {
//printf(" PID:%d --> buffer: %s\n", childpid, buffer);
int sLen = send(clientfd, buffer, length, 0);
//printf(" PID:%d --> sLen: %d\n", childpid, sLen);
} else if (length == 0) {
printf(" PID:%d client disconnect\n", childpid);
break;
} else {
printf(" PID:%d errno:%d\n", childpid, errno);
break;
}
}
}
static int nRecv(int sockfd, void *data, size_t length, int *count) {
int left_bytes;
int read_bytes;
int res;
int ret_code;
unsigned char *p;
struct pollfd pollfds;
pollfds.fd = sockfd;
pollfds.events = ( POLLIN | POLLERR | POLLHUP );
read_bytes = 0;
ret_code = 0;
p = (unsigned char *)data;
left_bytes = length;
while (left_bytes > 0) {
read_bytes = recv(sockfd, p, left_bytes, 0);
if (read_bytes > 0) {
left_bytes -= read_bytes;
p += read_bytes;
continue;
} else if (read_bytes < 0) {
if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
ret_code = (errno != 0 ? errno : EINTR);
}
} else {
ret_code = ENOTCONN;
break;
}
res = poll(&pollfds, 1, 5);
if (pollfds.revents & POLLHUP) {
ret_code = ENOTCONN;
break;
}
if (res < 0) {
if (errno == EINTR) {
continue;
}
ret_code = (errno != 0 ? errno : EINTR);
} else if (res == 0) {
ret_code = ETIMEDOUT;
break;
}
}
if (count != NULL) {
*count = length - left_bytes;
}
//printf("nRecv:%s, ret_code:%d, count:%d\n", (char*)data, ret_code, *count);
return ret_code;
}
void epoll_et_loop(int sockfd)
{
char buffer[MAX_BUFFER];
int ret;
while (1)
{
memset(buffer, 0, MAX_BUFFER);
ret = recv(sockfd, buffer, MAX_BUFFER, 0);
if (ret == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
printf(" read all data\n");
break;
}
close(sockfd);
break;
}
else if (ret == 0)
{
printf(" disconnect\n");
close(sockfd);
break;
}
else
printf("Recv:%s, %d Bytes\n", buffer, ret);
}
}
static int nSend(int sockfd, const void *buffer, int length, int flags) {
int wrotelen = 0;
int writeret = 0;
unsigned char *p = (unsigned char *)buffer;
struct pollfd pollfds = {0};
pollfds.fd = sockfd;
pollfds.events = ( POLLOUT | POLLERR | POLLHUP );
do {
int result = poll( &pollfds, 1, 5);
if (pollfds.revents & POLLHUP) {
printf(" ntySend errno:%d, revent:%x\n", errno, pollfds.revents);
return -1;
}
if (result < 0) {
if (errno == EINTR) continue;
printf(" ntySend errno:%d, result:%d\n", errno, result);
return -1;
} else if (result == 0) {
printf(" ntySend errno:%d, socket timeout \n", errno);
return -1;
}
writeret = send( sockfd, p + wrotelen, length - wrotelen, flags );
if( writeret <= 0 )
{
break;
}
wrotelen += writeret ;
} while (wrotelen < length);
return wrotelen;
}
static int curfds = 1;
static int nRun = 0;
int sockfds[MAX_PORT] = {0};
void client_job(job_t *job) {
client_t *rClient = (client_t*)job->user_data;
int clientfd = rClient->fd;
char buffer[MAX_BUFFER];
bzero(buffer, MAX_BUFFER);
int length = 0;
int ret = nRecv(clientfd, buffer, MAX_BUFFER, &length);
if (length > 0) {
if (nRun || buffer[0] == 'a') {
printf(" TcpRecv --> curfds : %d, buffer: %s\n", curfds, buffer);
nSend(clientfd, buffer, strlen(buffer), 0);
}
} else if (ret == ENOTCONN) {
curfds --;
close(clientfd);
} else {
}
free(rClient);
free(job);
}
void client_data_process(int clientfd) {
char buffer[MAX_BUFFER];
bzero(buffer, MAX_BUFFER);
int length = 0;
//int ret = nRecv(clientfd, buffer, MAX_BUFFER, &length);
int ret = recv(clientfd, buffer, MAX_BUFFER, 0);
if (length > 0) {
if (nRun || buffer[0] == 'a') {
printf(" TcpRecv --> curfds : %d, buffer: %s\n", curfds, buffer);
//nSend(clientfd, buffer, strlen(buffer), 0);
send(clientfd, buffer, strlen(buffer), 0);
}
} else if (ret == ENOTCONN) {
curfds --;
close(clientfd);
} else {
}
}
int listenfd(int fd, int *fds) {
int i = 0;
for (i = 0;i < MAX_PORT;i ++) {
if (fd == *(fds+i)) return *(fds+i);
}
return 0;
}
struct timeval tv_begin;
struct timeval tv_cur;
void *listen_thread(void *arg) {
int i = 0;
int epoll_fd = *(int *)arg;
struct epoll_event events[MAX_EPOLLSIZE];
gettimeofday(&tv_begin, NULL);
while (1) {
int nfds = epoll_wait(epoll_fd, events, curfds, 5);
if (nfds == -1) {
perror("epoll_wait");
break;
}
for (i = 0;i < nfds;i ++) {
int sockfd = listenfd(events[i].data.fd, sockfds);
if (sockfd) {
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr_in));
socklen_t client_len = sizeof(client_addr);
int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);
if (clientfd < 0) {
perror("accept");
return NULL;
}
if (curfds ++ > 1000 * 1000) {
nRun = 1;
}
#if 0
printf(" Client %d: %d.%d.%d.%d:%d \n", curfds, *(unsigned char*)(&client_addr.sin_addr.s_addr), *((unsigned char*)(&client_addr.sin_addr.s_addr)+1),
*((unsigned char*)(&client_addr.sin_addr.s_addr)+2), *((unsigned char*)(&client_addr.sin_addr.s_addr)+3),
client_addr.sin_port);
#elif 0
if(curfds % 1000 == 999) {
printf("connections: %d, fd: %d\n", curfds, clientfd);
}
#else
if (curfds % 1000 == 999) {
memcpy(&tv_cur, &tv_begin, sizeof(struct timeval));
gettimeofday(&tv_begin, NULL);
int time_used = TIME_SUB_MS(tv_begin, tv_cur);
printf("connections: %d, sockfd:%d, time_used:%d\n", curfds, clientfd, time_used);
}
#endif
ntySetNonblock(clientfd);
ntySetReUseAddr(clientfd);
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET | EPOLLOUT;
ev.data.fd = clientfd;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, clientfd, &ev);
} else {
int clientfd = events[i].data.fd;
#if 0
if (nRun) {
printf(" New Data is Comming\n");
client_data_process(clientfd);
} else {
client_t *rClient = (client_t*)malloc(sizeof(client_t));
memset(rClient, 0, sizeof(client_t));
rClient->fd = clientfd;
job_t *job = malloc(sizeof(job_t));
job->job_function = client_job;
job->user_data = rClient;
workqueue_add_job(&workqueue, job);
}
#else
client_data_process(clientfd);
#endif
}
}
}
}
int main(void) {
int i = 0;
printf("C1000K Server Start\n");
threadpool_init(); //
#if 0
int epoll_fd = epoll_create(MAX_EPOLLSIZE);
#else
int epoll_fds[CPU_CORES_SIZE] = {0};
pthread_t thread_id[CPU_CORES_SIZE] = {0};
for (i = 0;i < CPU_CORES_SIZE;i ++) {
epoll_fds[i] = epoll_create(MAX_EPOLLSIZE);
pthread_create(&thread_id[i], NULL, listen_thread, &epoll_fds[i]);
}
#endif
for (i = 0;i < MAX_PORT;i ++) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_PORT+i);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0) {
perror("bind");
return 2;
}
if (listen(sockfd, 5) < 0) {
perror("listen");
return 3;
}
sockfds[i] = sockfd;
printf("C1000K Server Listen on Port:%d\n", SERVER_PORT+i);
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET; //EPOLLLT
ev.data.fd = sockfd;
epoll_ctl(epoll_fds[i%CPU_CORES_SIZE], EPOLL_CTL_ADD, sockfd, &ev);
}
for (i = 0;i < CPU_CORES_SIZE;i ++) {
pthread_join(thread_id[i], NULL);
}
getchar();
printf("end\n");
}