mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-13 11:41:44 +08:00
http服务器支持mmap,提示性能
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
#include "Util/base64.h"
|
||||
#include "Util/SHA1.h"
|
||||
#include "Rtmp/utils.h"
|
||||
#include "FileReader.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
@@ -601,7 +602,6 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
|
||||
} else {
|
||||
//分节下载
|
||||
pcHttpResult = "206 Partial Content";
|
||||
fseek(pFilePtr.get(), iRangeStart, SEEK_SET);
|
||||
//分节下载返回Content-Range头
|
||||
httpHeader.emplace("Content-Range",StrPrinter<<"bytes " << iRangeStart << "-" << iRangeEnd << "/" << tFileStat.st_size<< endl);
|
||||
}
|
||||
@@ -617,51 +617,34 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
|
||||
throw SockException(bClose ? Err_shutdown : Err_success,"close connection after access file");
|
||||
}
|
||||
//回复Content部分
|
||||
std::shared_ptr<int64_t> piLeft(new int64_t(iRangeEnd - iRangeStart + 1));
|
||||
|
||||
GET_CONFIG(uint32_t,sendBufSize,Http::kSendBufSize);
|
||||
|
||||
FileReader::Ptr fileReader = std::make_shared<FileReader>(pFilePtr,iRangeStart,iRangeEnd - iRangeStart + 1);
|
||||
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
|
||||
auto onFlush = [pFilePtr,bClose,weakSelf,piLeft]() {
|
||||
TimeTicker();
|
||||
|
||||
auto onFlush = [fileReader,bClose,weakSelf]() {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
while(*piLeft && strongSelf){
|
||||
if(!strongSelf){
|
||||
//本对象已经销毁
|
||||
return false;
|
||||
}
|
||||
while(true){
|
||||
//更新超时计时器
|
||||
strongSelf->_ticker.resetTime();
|
||||
//从循环池获取一个内存片
|
||||
auto sendBuf = strongSelf->obtainBuffer();
|
||||
sendBuf->setCapacity(sendBufSize);
|
||||
//本次需要读取文件字节数
|
||||
int64_t iReq = MIN(sendBufSize,*piLeft);
|
||||
//读文件
|
||||
int iRead;
|
||||
do{
|
||||
iRead = fread(sendBuf->data(), 1, iReq, pFilePtr.get());
|
||||
}while(-1 == iRead && UV_EINTR == get_uv_error(false));
|
||||
//文件剩余字节数
|
||||
*piLeft -= iRead;
|
||||
|
||||
if (iRead < iReq || !*piLeft) {
|
||||
//读取文件
|
||||
auto sendBuf = fileReader->read(sendBufSize);
|
||||
if (!sendBuf) {
|
||||
//文件读完
|
||||
if(iRead > 0) {
|
||||
sendBuf->setSize(iRead);
|
||||
strongSelf->send(sendBuf);
|
||||
}
|
||||
|
||||
if(strongSelf->isSocketBusy()){
|
||||
//套接字忙,我们等待触发下一次onFlush事件
|
||||
//待所有数据flush到socket fd再移除onFlush事件监听
|
||||
//标记文件读写完毕
|
||||
*piLeft = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
//文件全部flush到socket fd,可以直接关闭socket了
|
||||
break;
|
||||
}
|
||||
|
||||
//文件还未读完
|
||||
sendBuf->setSize(iRead);
|
||||
if(strongSelf->send(sendBuf) == -1) {
|
||||
//socket已经销毁,不再监听onFlush事件
|
||||
return false;
|
||||
@@ -673,7 +656,7 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
|
||||
//socket还可写,继续写socket
|
||||
}
|
||||
|
||||
if(bClose && strongSelf) {
|
||||
if(bClose) {
|
||||
//最后一次flush事件,文件也发送完毕了,可以关闭socket了
|
||||
strongSelf->shutdown(SockException(Err_shutdown,"read file eof"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user