-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cpp
More file actions
154 lines (132 loc) · 4.79 KB
/
HttpServer.cpp
File metadata and controls
154 lines (132 loc) · 4.79 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
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
#include "HttpServer.h"
#include "SocketOps.h"
#include "EpollOps.h"
#include "Timer.h"
#include "Logger.h"
#include <iostream>
#include <sys/epoll.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
using namespace std;
HttpServer::HttpServer(int port,
int threadNum,
std::string userName, std::string passWd,
std::string databaseName, int sqlNum)
: port_(port), threadPool_(threadNum),
userName_(userName), passWd_(passWd),
databaseName_(databaseName), sqlNum_(sqlNum) {
listenFd_ = createNonblockingOrDie();
bindOrDie(listenFd_, port_);
listenOrDie(listenFd_);
setNonBlocking(listenFd_);
userConns_ = new HttpConn[kMaxFd];
activeFds_ = new epoll_event[kMaxEventNum];
char server_path[200];
char* ret = getcwd(server_path, 200); (void)ret;
char root[6] = "/root";
docRoot_ = (char *)malloc(strlen(server_path) + strlen(root) + 1);
strcpy(docRoot_, server_path);
strcat(docRoot_, root);
eventLoopInit_();
threadPoolInit_();
databaseInit_();
}
HttpServer::~HttpServer() {
close(epollFd_);
close(listenFd_);
delete[] userConns_;
delete[] activeFds_;
delete connPool_;
free(docRoot_);
}
void HttpServer::eventLoopInit_() {
epollFd_ = createEpollOrDie();
HttpConn::epollFd = epollFd_;
addFd(epollFd_, listenFd_);
}
void HttpServer::threadPoolInit_() {
threadPool_.start();
}
void HttpServer::databaseInit_() {
connPool_ = ConnectionPool::getInstance();
connPool_->init("127.0.0.1", userName_, passWd_, databaseName_, 3306, sqlNum_);
userConns_->initResponse(connPool_, docRoot_);
}
void HttpServer::loop() {
while (true) {
int timeOutMs = timerHeap_.getNextTick();
LOG_INFO("remaining %d(s) TIMEOUT!", timeOutMs == -1 ? -1 : timeOutMs/1000);
int numReadys = epoll_wait(epollFd_, activeFds_, kMaxEventNum, timeOutMs);
LOG_INFO("%d events arrived", numReadys);
if (numReadys == -1 && errno != EINTR) { // 不可恢复错误
LOG_ERROR("%s : %s", "epoll_wait error", strerror(errno));
}
for (int i = 0; i < numReadys; i++) {
int fd = activeFds_[i].data.fd;
if (fd == listenFd_) { // 监听描述符就绪
LOG_INFO("fd = %d readable", fd);
dealWithListen_();
} else if (activeFds_[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { // 本端读写均关闭,可以断开连接
dealWithError_(&userConns_[fd], activeFds_[i].events);
} else if (activeFds_[i].events & EPOLLIN) { // 可写
LOG_INFO("fd = %d readable", fd);
dealWithRead_(&userConns_[fd]);
} else if (activeFds_[i].events & EPOLLOUT) { // 可读
LOG_INFO("fd = %d writable", fd);
dealWithWrite_(&userConns_[fd]);
}
}
}
}
void HttpServer::dealWithListen_() {
struct sockaddr_in clientAddr;
bzero(&clientAddr, sizeof(clientAddr));
int connFd = acceptOrDie(listenFd_, &clientAddr);
if (connFd > kMaxFd || connFd < 0) { // 处理最大连接数
return;
}
userConns_[connFd].init(connFd, clientAddr);
addFd(epollFd_, connFd, true); // 加入到epoll中进行监听
setNonBlocking(connFd); // 非阻塞
timerHeap_.addTimer(connFd, kTimerSlot, std::bind(&HttpServer::closeConn_, this, &userConns_[connFd]));
LOG_INFO("connFd = %d connected!", connFd);
}
void HttpServer::dealWithWrite_(HttpConn* conn) {
// 写逻辑:
// 首先写数据,写完之后判断是否关闭连接,如果关闭则利用timer来关闭
// 如果不关闭,则调整时间
bool isClose = conn->sendMsg();
LOG_INFO("fd = %d dealWithWrite done! KeepAlive = %d", conn->getFd(), isClose ? 0 : 1);
if (isClose) {
timerHeap_.delTimer(conn->getFd());
return;
}
extentTime(conn);
}
void HttpServer::dealWithRead_(HttpConn* conn) {
// 读逻辑:
// 首先读数据,读完之后提交到线程池处理
ssize_t n = conn->recvMsg();
LOG_INFO("fd = %d dealWithRead done! received bytes = %d", conn->getFd(), (int)n);
if (n == 0) {
timerHeap_.delTimer(conn->getFd());
return;
}
extentTime(conn);
LOG_INFO("fd = %d submit to thread pool!", conn->getFd());
threadPool_.submit(conn);
}
void HttpServer::dealWithError_(HttpConn* conn, uint32_t events){
LOG_INFO("fd = %d dealWithError", conn->getFd());
timerHeap_.delTimer(conn->getFd());
}
void HttpServer::closeConn_(HttpConn* conn) {
LOG_INFO("fd = %d is being close!", conn->getFd());
conn->closeConn();
}
void HttpServer::extentTime(HttpConn* conn) {
timerHeap_.adjustTimer(conn->getFd());
}