修复http文件服务器可能丢失数据的问题

This commit is contained in:
xiongziliang 2019-09-30 20:07:09 +08:00
parent b573b5bbac
commit 205b01f6d1

View File

@ -619,7 +619,7 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
TimeTicker(); TimeTicker();
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
while(*piLeft && strongSelf){ while(*piLeft && strongSelf){
//更新超时时器 //更新超时时器
strongSelf->_ticker.resetTime(); strongSelf->_ticker.resetTime();
//从循环池获取一个内存片 //从循环池获取一个内存片
auto sendBuf = strongSelf->obtainBuffer(); auto sendBuf = strongSelf->obtainBuffer();
@ -640,24 +640,37 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
sendBuf->setSize(iRead); sendBuf->setSize(iRead);
strongSelf->send(sendBuf); 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); sendBuf->setSize(iRead);
int iSent = strongSelf->send(sendBuf); if(strongSelf->send(sendBuf) == -1) {
if(iSent == -1) { //socket已经销毁不再监听onFlush事件
//套机制销毁
return false; return false;
} }
if(strongSelf->isSocketBusy()){ if(strongSelf->isSocketBusy()){
//套接字忙,那么停止继续写 //socket忙那么停止继续写,等待下一次onFlush事件然后再读文件写socket
return true; return true;
} }
//继续写套接字 //socket还可写继续写socket
} }
if(bClose && strongSelf) {
//最后一次flush事件文件也发送完毕了可以关闭socket了
strongSelf->shutdown(SockException(Err_shutdown,"read file eof"));
}
//不再监听onFlush事件
return false; return false;
}; };