Http客户端添加超时功能

This commit is contained in:
xiongziliang 2018-02-06 17:35:32 +08:00
parent c801de9965
commit ae1b62c78f
4 changed files with 33 additions and 10 deletions

View File

@ -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() {
} }
} }
void HttpDownloader::onManager(){
if(elapsedTime() > _timeOutSecond * 1000){
//超时
onDisconnect(SockException(Err_timeout,"download timeout"));
shutdown();
}
}
} /* namespace Http */ } /* namespace Http */
} /* namespace ZL */ } /* namespace ZL */

View File

@ -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:
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;
}; };

View File

@ -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();
}
} }

View File

@ -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