From 36f688f1185280c1de77d54bbdb62c908dccd545 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 14 Mar 2018 22:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 45 ++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 88aef575..2e4a9600 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -317,6 +317,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { if(emitHttpEvent(false)){ return Http_success; } + //再看看是否为http-flv直播请求 if(checkLiveFlvStream()){ return Http_success; } @@ -329,7 +330,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { /////////////HTTP连接是否需要被关闭//////////////// GET_CONFIG_AND_REGISTER(uint32_t,reqCnt,Config::Http::kMaxReqCount); - bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) && ( ++m_iReqCnt < reqCnt); + bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) || ( ++m_iReqCnt > reqCnt); HttpCode eHttpCode = bClose ? Http_failed : Http_success; //访问的是文件夹 if (strFile.back() == '/') { @@ -350,12 +351,19 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { sendNotFound(bClose); return eHttpCode; } - FILE *pFile = fopen(strFile.data(), "rb"); - if (pFile == NULL) { + //文件智能指针,防止退出时未关闭 + std::shared_ptr pFilePtr(fopen(strFile.data(), "rb"), [](FILE *pFile) { + if(pFile){ + fclose(pFile); + } + }); + + if (!pFilePtr) { //打开文件失败 sendNotFound(bClose); return eHttpCode; } + //判断是不是分节下载 auto &strRange = m_parser["Range"]; int64_t iRangeStart = 0, iRangeEnd = 0; @@ -371,7 +379,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { } else { //分节下载 pcHttpResult = "206 Partial Content"; - fseek(pFile, iRangeStart, SEEK_SET); + fseek(pFilePtr.get(), iRangeStart, SEEK_SET); } auto httpHeader=makeHttpHeader(bClose, iRangeEnd - iRangeStart + 1, get_mime_type(strFile.data())); if (strRange.size() != 0) { @@ -391,9 +399,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { } //回复Content部分 std::shared_ptr piLeft(new int64_t(iRangeEnd - iRangeStart + 1)); - std::shared_ptr pFilePtr(pFile, [](FILE *pFp) { - fclose(pFp); - }); + GET_CONFIG_AND_REGISTER(uint32_t,sendBufSize,Config::Http::kSendBufSize); weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); @@ -452,9 +458,30 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { }; //关闭tcp_nodelay ,优化性能 SockUtil::setNoDelay(_sock->rawFD(),false); + //设置MSG_MORE,优化性能 (*this) << SocketFlags(sock_flags); + + //后台线程执行onFlush + auto onFlushWrapper = [onFlush,weakSelf](){ + auto strongSelf = weakSelf.lock(); + if(!strongSelf){ + return false; + } + strongSelf->async([onFlush,weakSelf](){ + //在后台线程完成文件读取,释放主线程性能 + if(!onFlush()){ + //如果onFlush返回false,则说明不再监听flush事件 + auto strongSelf = weakSelf.lock(); + if(strongSelf){ + strongSelf->_sock->setOnFlush(nullptr); + } + } + }); + return true; + }; + onFlush(); - _sock->setOnFlush(onFlush); + _sock->setOnFlush(onFlushWrapper); return Http_success; } @@ -595,7 +622,7 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){ ///////////////////是否断开本链接/////////////////////// GET_CONFIG_AND_REGISTER(uint32_t,reqCnt,Config::Http::kMaxReqCount); - bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) && ( ++m_iReqCnt < reqCnt); + bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) || ( ++m_iReqCnt > reqCnt); auto Origin = m_parser["Origin"]; /////////////////////异步回复Invoker/////////////////////////////// weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this());