Repository navigation
Expand file tree
/
Copy pathweb_final.cpp
More file actions
96 lines (87 loc) · 2.41 KB
/
Copy pathweb_final.cpp
File metadata and controls
96 lines (87 loc) · 2.41 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
93
94
95
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include "csapp.h"
#include "epoll_ulti.h"
#include "http_handle.h"
#include "thread_pool.h"
#include "timer.h"
using namespace utility;
/*-
* 非阻塞版本的web server,主要利用epoll机制来实现多路IO复用.加上了线程池,这样以来可以实现更高
* 的性能.
*/
#define MAXEVENTNUM 100
void blockSigpipe()
{
sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE);
int rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
if (rc != 0) {
printf("block sigpipe error\n");
}
}
int http_close_conn(HttpHandle* request)
{
printf("closing\n");
close(request->getFd());
return 0;
}
int main(int argc, char *argv[])
{
int listenfd = Open_listenfd(8080); /* 8080号端口监听 */
epoll_event events[MAXEVENTNUM];
sockaddr clnaddr;
socklen_t clnlen = sizeof(clnaddr);
blockSigpipe(); /* 首先要将SIGPIPE消息阻塞掉 */
int epollfd = Epoll_create(1024); /* 10基本上没有什么用处 */
addfd(epollfd, listenfd, false); /* epollfd要监听listenfd上的可读事件 */
ThreadPool pools(3, 30000);
HttpHandle::setEpollfd(epollfd);
HttpHandle handle[20000];
tk_timer_init();
while(true)
{
int time = tk_find_timer();
int eventnum = Epoll_wait(epollfd, events, MAXEVENTNUM, -1);
tk_handle_expire_timers();
for (int i = 0; i < eventnum; ++i)
{
int sockfd = events[i].data.fd;
if (sockfd == listenfd)
{ /* 有连接到来 */
while(true)
{
int connfd = accept(listenfd, &clnaddr, &clnlen);
if (connfd == -1)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
{ /* 将连接已经建立完了 */
break;
}
unix_error("accept error");
}
handle[connfd].init(connfd); /* 初始化 */
addfd(epollfd, connfd, true); /* 加入监听 */
tk_add_timer(&handle[connfd], TIMEOUT_DEFAULT, http_close_conn);
}
}
else
{ /* 有数据可读或者可写 */
pools.append(boost::bind(&HttpHandle::process, &handle[sockfd])); /* 多线程版本 */
//handle[sockfd].process(); // 单进程版本
}
}
}
return 0;
}