From 205b01f6d1130431f8597205e394abe062c32092 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 30 Sep 2019 20:07:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhttp=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=8F=AF=E8=83=BD=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index bd8686db..b1c124d9 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -619,7 +619,7 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) { TimeTicker(); auto strongSelf = weakSelf.lock(); while(*piLeft && strongSelf){ - //更新超时定时器 + //更新超时计时器 strongSelf->_ticker.resetTime(); //从循环池获取一个内存片 auto sendBuf = strongSelf->obtainBuffer(); @@ -636,28 +636,41 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) { if (iRead < iReq || !*piLeft) { //文件读完 - if(iRead>0) { + if(iRead > 0) { sendBuf->setSize(iRead); strongSelf->send(sendBuf); } - if(bClose) { - strongSelf->shutdown(SockException(Err_shutdown,"read file eof")); + + if(strongSelf->isSocketBusy()){ + //套接字忙,我们等待触发下一次onFlush事件 + //待所有数据flush到socket fd再移除onFlush事件监听 + //标记文件读写完毕 + *piLeft = 0; + return true; } - return false; + + //文件全部flush到socket fd,可以直接关闭socket了 + break; } + //文件还未读完 sendBuf->setSize(iRead); - int iSent = strongSelf->send(sendBuf); - if(iSent == -1) { - //套机制销毁 + if(strongSelf->send(sendBuf) == -1) { + //socket已经销毁,不再监听onFlush事件 return false; } if(strongSelf->isSocketBusy()){ - //套接字忙,那么停止继续写 + //socket忙,那么停止继续写,等待下一次onFlush事件,然后再读文件写socket return true; } - //继续写套接字 + //socket还可写,继续写socket } + + if(bClose && strongSelf) { + //最后一次flush事件,文件也发送完毕了,可以关闭socket了 + strongSelf->shutdown(SockException(Err_shutdown,"read file eof")); + } + //不再监听onFlush事件 return false; };