Commit ae1b62c7 by xiongziliang

Http客户端添加超时功能

parent c801de99
...@@ -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 */
...@@ -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;
}; };
......
...@@ -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();
}
} }
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论