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
ae1b62c7
Commit
ae1b62c7
authored
Feb 06, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Http客户端添加超时功能
parent
c801de99
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
33 行增加
和
10 行删除
+33
-10
src/Http/HttpDownloader.cpp
+11
-1
src/Http/HttpDownloader.h
+7
-3
src/Http/HttpRequester.cpp
+11
-2
src/Http/HttpRequester.h
+4
-4
没有找到文件。
src/Http/HttpDownloader.cpp
查看文件 @
ae1b62c7
...
@@ -41,8 +41,9 @@ HttpDownloader::~HttpDownloader() {
...
@@ -41,8 +41,9 @@ HttpDownloader::~HttpDownloader() {
closeFile
();
closeFile
();
}
}
void
HttpDownloader
::
startDownload
(
const
string
&
url
,
const
string
&
filePath
,
bool
bAppend
)
{
void
HttpDownloader
::
startDownload
(
const
string
&
url
,
const
string
&
filePath
,
bool
bAppend
,
uint32_t
timeOutSecond
)
{
_filePath
=
filePath
;
_filePath
=
filePath
;
_timeOutSecond
=
timeOutSecond
;
if
(
_filePath
.
empty
()){
if
(
_filePath
.
empty
()){
_filePath
=
exeDir
()
+
"HttpDownloader/"
+
MD5
(
url
).
hexdigest
();
_filePath
=
exeDir
()
+
"HttpDownloader/"
+
MD5
(
url
).
hexdigest
();
}
}
...
@@ -124,5 +125,14 @@ void HttpDownloader::closeFile() {
...
@@ -124,5 +125,14 @@ void HttpDownloader::closeFile() {
}
}
}
}
void
HttpDownloader
::
onManager
(){
if
(
elapsedTime
()
>
_timeOutSecond
*
1000
){
//超时
onDisconnect
(
SockException
(
Err_timeout
,
"download timeout"
));
shutdown
();
}
}
}
/* namespace Http */
}
/* namespace Http */
}
/* namespace ZL */
}
/* namespace ZL */
src/Http/HttpDownloader.h
查看文件 @
ae1b62c7
...
@@ -39,8 +39,9 @@ public:
...
@@ -39,8 +39,9 @@ public:
HttpDownloader
();
HttpDownloader
();
virtual
~
HttpDownloader
();
virtual
~
HttpDownloader
();
//开始下载文件,默认断点续传方式下载
//开始下载文件,默认断点续传方式下载
void
startDownload
(
const
string
&
url
,
const
string
&
filePath
=
""
,
bool
bAppend
=
false
);
void
startDownload
(
const
string
&
url
,
const
string
&
filePath
=
""
,
bool
bAppend
=
false
,
uint32_t
timeOutSecond
=
10
);
void
startDownload
(
const
string
&
url
,
const
onDownloadResult
&
cb
){
void
startDownload
(
const
string
&
url
,
const
onDownloadResult
&
cb
,
uint32_t
timeOutSecond
=
10
){
_timeOutSecond
=
timeOutSecond
;
setOnResult
(
cb
);
setOnResult
(
cb
);
startDownload
(
url
);
startDownload
(
url
);
}
}
...
@@ -52,11 +53,14 @@ private:
...
@@ -52,11 +53,14 @@ private:
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recvedSize
,
size_t
totalSize
)
override
;
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recvedSize
,
size_t
totalSize
)
override
;
void
onResponseCompleted
()
override
;
void
onResponseCompleted
()
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
void
closeFile
();
void
onManager
()
override
;
void
closeFile
();
FILE
*
_saveFile
=
nullptr
;
FILE
*
_saveFile
=
nullptr
;
string
_filePath
;
string
_filePath
;
onDownloadResult
_onResult
;
onDownloadResult
_onResult
;
uint32_t
_timeOutSecond
;
bool
_bDownloadSuccess
=
false
;
bool
_bDownloadSuccess
=
false
;
};
};
...
...
src/Http/HttpRequester.cpp
查看文件 @
ae1b62c7
...
@@ -57,10 +57,19 @@ void HttpRequester::onDisconnect(const SockException &ex){
...
@@ -57,10 +57,19 @@ void HttpRequester::onDisconnect(const SockException &ex){
}
}
}
}
void
HttpRequester
::
startRequester
(
const
string
&
url
,
const
HttpRequesterResult
&
onResult
){
void
HttpRequester
::
startRequester
(
const
string
&
url
,
const
HttpRequesterResult
&
onResult
,
uint32_t
timeOutSecond
){
_onResult
=
onResult
;
_onResult
=
onResult
;
_resTicker
.
resetTime
();
_timeOutSecond
=
timeOutSecond
;
sendRequest
(
url
);
sendRequest
(
url
);
}
void
HttpRequester
::
onManager
(){
if
(
_onResult
&&
_resTicker
.
elapsedTime
()
>
_timeOutSecond
*
1000
){
//超时
onDisconnect
(
SockException
(
Err_timeout
,
"wait http response timeout"
));
shutdown
();
}
}
}
...
...
src/Http/HttpRequester.h
查看文件 @
ae1b62c7
...
@@ -40,17 +40,17 @@ public:
...
@@ -40,17 +40,17 @@ public:
HttpRequester
();
HttpRequester
();
virtual
~
HttpRequester
();
virtual
~
HttpRequester
();
void
startRequester
(
const
string
&
url
,
const
HttpRequesterResult
&
onResult
);
void
startRequester
(
const
string
&
url
,
const
HttpRequesterResult
&
onResult
,
uint32_t
timeOutSecond
=
10
);
private
:
private
:
void
onResponseHeader
(
const
string
&
status
,
const
HttpHeader
&
headers
)
override
;
void
onResponseHeader
(
const
string
&
status
,
const
HttpHeader
&
headers
)
override
;
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recvedSize
,
size_t
totalSize
)
override
;
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recvedSize
,
size_t
totalSize
)
override
;
void
onResponseCompleted
()
override
;
void
onResponseCompleted
()
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
void
onManager
()
override
;
string
_strRecvBody
;
string
_strRecvBody
;
HttpRequesterResult
_onResult
;
HttpRequesterResult
_onResult
;
Ticker
_resTicker
;
uint32_t
_timeOutSecond
;
};
};
}
//namespace Http
}
//namespace Http
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论