Repository navigation
Expand file tree
/
Copy pathepoll_thread_pool.cpp
More file actions
92 lines (79 loc) · 3.07 KB
/
Copy pathepoll_thread_pool.cpp
File metadata and controls
92 lines (79 loc) · 3.07 KB
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
#include "socket_wrapper.h"
#include "common.h"
#include <signal.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <netinet/tcp.h>
#include "threadpool.h"
using namespace std;
/*
* fork() a new process to handle request
*/
#define MAXCONN 1000
#define BENCH_SNDBUF 5 * 1024 * 1024
#define BENCH_RCVBUF 5 * 1024 * 1024
typedef struct thread_data_tag {
char request_buffer[REQUEST_BUFSIZ];
int conn;
} thread_data_t;
void thread_routine(void *thread_data) {
thread_data_t *data = (thread_data_t *)thread_data;
//CALC_TIME_MS_P("COST",
w_read(data->conn, data->request_buffer, sizeof(data->request_buffer));
//handle_request();
server_response(data->conn);
w_close(data->conn);
//);
delete data;
}
int main(int argc, char **argv) {
int socket_server = w_socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
int on = 1;
//w_setsockopt(socket_server, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int));
int wait_seconds = 60; /* 60s */
/* good for http, should be tunned */
//w_setsockopt(socket_server, IPPROTO_TCP, TCP_DEFER_ACCEPT, &wait_seconds, sizeof(int));
/* cost my network mostly
* SNDBUF/RCVBUF = rtt * network-speed(Mpbs) / 8
*/
int sndbuf = BENCH_SNDBUF, rcvbuf = BENCH_RCVBUF;
w_setsockopt(socket_server, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(int));
w_setsockopt(socket_server, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int));
reuse_address(socket_server);
w_bind(socket_server, (struct sockaddr *)&server_addr, sizeof(server_addr));
w_listen(socket_server, BACKLOG);
int thread_num = atoi(argv[1]);
threadpool_t *thread_pool = threadpool_create(thread_num, MAX_QUEUE, 0);
int epollfd = w_epoll_create(0);
struct epoll_event listen_event = {0}, event;
listen_event.events = EPOLLIN/* | EPOLLET*/;
listen_event.data.fd = socket_server;
w_epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &listen_event);
struct epoll_event events[MAXCONN];
for (;;) {
int count = w_epoll_wait(epollfd, events, MAXCONN, -1);
for (int i = 0; i < count; ++i) {
if (events[i].events & EPOLLIN) {
if (events[i].data.fd == socket_server) {
//int conn = w_accept4(socket_server, 0, 0, SOCK_NONBLOCK);
int conn = w_accept(socket_server, 0, 0);
event.events = EPOLLIN/* | EPOLLET*/;
event.data.fd = conn;
w_epoll_ctl(epollfd, EPOLL_CTL_ADD, conn, &event);
} else {
thread_data_t *data = new thread_data_t;
data->conn = events[i].data.fd;
epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &events[i]);
threadpool_add(thread_pool, thread_routine, data, 0);
}
}
}
}
threadpool_destroy(thread_pool, threadpool_graceful);
return 0;
}