Skip to content

Commit

Permalink
🐛 keep-alive
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Sep 23, 2020
1 parent dae8a91 commit b2ac521
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
11 changes: 8 additions & 3 deletions code/http/httpconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void HttpConn::reset() {
writeBuff_.RetrieveAll();
readBuff_.RetrieveAll();
isClose_ = false;
request_.Init();
}

void HttpConn::Close() {
Expand Down Expand Up @@ -99,13 +98,18 @@ ssize_t HttpConn::write(int* saveErrno) {
return len;
}

void HttpConn::process() {
if(request_.parse(readBuff_)) {
bool HttpConn::process() {
request_.Init();
if(readBuff_.ReadableBytes() <= 0) {
return false;
}
else if(request_.parse(readBuff_)) {
LOG_DEBUG("%s", request_.path().c_str());
response_.Init(srcDir, request_.path(), request_.IsKeepAlive(), 200);
} else {
response_.Init(srcDir, request_.path(), false, 400);
}

response_.MakeResponse(writeBuff_);
/* 响应头 */
iov_[0].iov_base = const_cast<char*>(writeBuff_.Peek());
Expand All @@ -119,4 +123,5 @@ void HttpConn::process() {
iovCnt_ = 2;
}
LOG_DEBUG("filesize:%d, %d to %d", response_.FileLen() , iovCnt_, ToWriteBytes());
return true;
}
2 changes: 1 addition & 1 deletion code/http/httpconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HttpConn {
const char* GetIP() const;
sockaddr_in GetAddr() const;

void process();
bool process();
int ToWriteBytes() {
return iov_[0].iov_len + iov_[1].iov_len;
}
Expand Down
7 changes: 3 additions & 4 deletions code/http/httprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void HttpRequest::Init() {
}

bool HttpRequest::IsKeepAlive() const {
if(post_.count("Connection") == 1) {
return post_.find("Connection")->second == "Keep-Alive" && version_ == "1.1";
if(header_.count("Connection") == 1) {
return header_.find("Connection")->second == "keep-alive" && version_ == "1.1";
}
return false;
}
Expand All @@ -46,7 +46,7 @@ bool HttpRequest::parse(Buffer& buff) {
case HEADERS:
ParseHeader_(line);
if(buff.ReadableBytes() <= 2) {
state_ = FINISH;
state_ = FINISH;
}
break;
case BODY:
Expand All @@ -57,7 +57,6 @@ bool HttpRequest::parse(Buffer& buff) {
}
if(lineEnd == buff.BeginWrite()) { break; }
buff.RetrieveUntil(lineEnd + 2);

}
LOG_DEBUG("[%s], [%s], [%s]", method_.c_str(), path_.c_str(), version_.c_str());
return true;
Expand Down
8 changes: 4 additions & 4 deletions code/http/httpresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ HttpResponse::~HttpResponse() {
}

void HttpResponse::Init(const string& srcDir, string& path, bool isKeepAlive, int code){
assert(srcDir != "" && path != "");
assert(srcDir != "");
if(mmFile_) { UnmapFile(); }
code_ = code;
isKeepAlive_ = isKeepAlive_;
isKeepAlive_ = isKeepAlive;
path_ = path;
srcDir_ = srcDir;
mmFile_ = nullptr;
Expand Down Expand Up @@ -112,8 +112,8 @@ void HttpResponse::AddStateLine_(Buffer& buff) {
void HttpResponse::AddHeader_(Buffer& buff) {
buff.Append("Connection: ");
if(isKeepAlive_) {
buff.Append("Keep-Alive\r\n");
buff.Append("Keep-Alive: timeout=10000\r\n");
buff.Append("keep-alive\r\n");
buff.Append("keep-alive: max=5, timeout=120\r\n");
} else{
buff.Append("close\r\n");
}
Expand Down
4 changes: 2 additions & 2 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ int main() {
/* 守护进程 后台运行 */
//daemon(1, 0);
WebServer server(
1316, 3, 5000, false, /* 端口 ET模式 timeoutMs 优雅退出 */
1316, 3, 60000, false, /* 端口 ET模式 timeoutMs 优雅退出 */
3306, "root", "root", "webserver", /* Mysql配置 */
12, 6, false, 0, 1024); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
12, 6, true, 1, 1024); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
server.Start();
}

17 changes: 11 additions & 6 deletions code/server/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ void WebServer::OnRead_(HttpConn* client) {
CloseConn_(client);
return;
}
client->process();
epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLOUT);
OnProcess(client);
}

void WebServer::OnProcess(HttpConn* client) {
if(client->process()) {
epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLOUT);
} else {
epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLIN);
}
}

void WebServer::OnWrite_(HttpConn* client) {
Expand All @@ -187,9 +194,7 @@ void WebServer::OnWrite_(HttpConn* client) {
if(client->ToWriteBytes() == 0) {
/* 传输完成 */
if(client->IsKeepAlive()) {
LOG_DEBUG("keepAlive!");
client->reset();
epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLIN);
OnProcess(client);
return;
}
}
Expand Down Expand Up @@ -236,7 +241,7 @@ bool WebServer::InitSocket_() {

int optval = 1;
/* 端口复用 */
/* 。但是,这些套接字并不是所有都能读取信息,只有最后一个套接字会正常接收数据。 */
/* 只有最后一个套接字会正常接收数据。 */
ret = setsockopt(listenFd_, SOL_SOCKET, SO_REUSEADDR, (const void*)&optval, sizeof(int));
if(ret == -1) {
LOG_ERROR("set socket setsockopt error !");
Expand Down
3 changes: 2 additions & 1 deletion code/server/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class WebServer {

void OnRead_(HttpConn* client);
void OnWrite_(HttpConn* client);

void OnProcess(HttpConn* client);

static const int MAX_FD = 65536;

static int SetFdNonblock(int fd);
Expand Down

0 comments on commit b2ac521

Please sign in to comment.