Commit aa0ae0c8 by xiongziliang

优化http服务器性能

parent 49375fd9
...@@ -343,40 +343,44 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { ...@@ -343,40 +343,44 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() {
fclose(pFp); fclose(pFp);
}); });
static uint32_t sendBufSize = mINI::Instance()[Config::Http::kSendBufSize].as<uint32_t>(); static uint32_t sendBufSize = mINI::Instance()[Config::Http::kSendBufSize].as<uint32_t>();
std::shared_ptr<char> pacSendBuf(new char[sendBufSize],[](char *ptr){
delete [] ptr;
});
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this()); weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
auto onFlush = [pFilePtr,bClose,weakSelf,piLeft,pacSendBuf]() { auto onFlush = [pFilePtr,bClose,weakSelf,piLeft]() {
TimeTicker(); TimeTicker();
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
while(*piLeft && strongSelf){ while(*piLeft && strongSelf){
//更新超时定时器
strongSelf->m_ticker.resetTime(); strongSelf->m_ticker.resetTime();
//从循环池获取一个内存片
auto sendBuf = strongSelf->sock->obtainBuffer();
sendBuf->setCapacity(sendBufSize);
//本次需要读取文件字节数
int64_t iReq = MIN(sendBufSize,*piLeft); int64_t iReq = MIN(sendBufSize,*piLeft);
int64_t iRead = fread(pacSendBuf.get(), 1, iReq, pFilePtr.get()); //读文件
int64_t iRead = fread(sendBuf->data(), 1, iReq, pFilePtr.get());
//文件剩余字节数
*piLeft -= iRead; *piLeft -= iRead;
//InfoL << "Send file :" << iReq << " " << *piLeft;
if (iRead < iReq || !*piLeft) { if (iRead < iReq || !*piLeft) {
//send completed! //文件读完
//FatalL << "send completed!";
if(iRead>0) { if(iRead>0) {
strongSelf->sock->send(pacSendBuf.get(), iRead,SOCKET_DEFAULE_FLAGS | FLAG_MORE); sendBuf->setSize(iRead);
strongSelf->sock->send(sendBuf,SOCKET_DEFAULE_FLAGS | FLAG_MORE);
} }
if(bClose) { if(bClose) {
strongSelf->shutdown(); strongSelf->shutdown();
} }
return false; return false;
} }
//文件还未读完
int iSent = strongSelf->sock->send(pacSendBuf.get(), iRead,SOCKET_DEFAULE_FLAGS | FLAG_MORE); sendBuf->setSize(iRead);
int iSent = strongSelf->sock->send(sendBuf,SOCKET_DEFAULE_FLAGS | FLAG_MORE);
if(iSent == -1) { if(iSent == -1) {
//send error //send error
//FatalL << "send error";
return false; return false;
} }
if(iSent < iRead) { if(iSent < iRead) {
//send wait //send wait
//FatalL << "send wait";
return true; return true;
} }
//send success //send success
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论