Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
36f688f1
Commit
36f688f1
authored
Mar 14, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化http文件服务器性能
parent
326d3a57
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
36 行增加
和
9 行删除
+36
-9
src/Http/HttpSession.cpp
+36
-9
没有找到文件。
src/Http/HttpSession.cpp
查看文件 @
36f688f1
...
...
@@ -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
<
FILE
>
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
(
pFile
Ptr
.
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
<
int64_t
>
piLeft
(
new
int64_t
(
iRangeEnd
-
iRangeStart
+
1
));
std
::
shared_ptr
<
FILE
>
pFilePtr
(
pFile
,
[](
FILE
*
pFp
)
{
fclose
(
pFp
);
});
GET_CONFIG_AND_REGISTER
(
uint32_t
,
sendBufSize
,
Config
::
Http
::
kSendBufSize
);
weak_ptr
<
HttpSession
>
weakSelf
=
dynamic_pointer_cast
<
HttpSession
>
(
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
(
onFlush
Wrapper
);
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
<
HttpSession
>
weakSelf
=
dynamic_pointer_cast
<
HttpSession
>
(
shared_from_this
());
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论