diff --git a/src/Http/HttpDownloader.cpp b/src/Http/HttpDownloader.cpp index 6b7e8b0d..295d57ee 100644 --- a/src/Http/HttpDownloader.cpp +++ b/src/Http/HttpDownloader.cpp @@ -41,8 +41,9 @@ HttpDownloader::~HttpDownloader() { 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; + _timeOutSecond = timeOutSecond; if(_filePath.empty()){ _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 ZL */ diff --git a/src/Http/HttpDownloader.h b/src/Http/HttpDownloader.h index 3be00e93..78c471ff 100644 --- a/src/Http/HttpDownloader.h +++ b/src/Http/HttpDownloader.h @@ -39,8 +39,9 @@ public: HttpDownloader(); virtual ~HttpDownloader(); //开始下载文件,默认断点续传方式下载 - void startDownload(const string &url,const string &filePath = "",bool bAppend = false); - void startDownload(const string &url,const onDownloadResult &cb){ + void startDownload(const string &url,const string &filePath = "",bool bAppend = false,uint32_t timeOutSecond = 10 ); + void startDownload(const string &url,const onDownloadResult &cb,uint32_t timeOutSecond = 10){ + _timeOutSecond = timeOutSecond; setOnResult(cb); startDownload(url); } @@ -52,11 +53,14 @@ private: void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize) override; void onResponseCompleted() override; void onDisconnect(const SockException &ex) override; - void closeFile(); + void onManager() override; + + void closeFile(); FILE *_saveFile = nullptr; string _filePath; onDownloadResult _onResult; + uint32_t _timeOutSecond; bool _bDownloadSuccess = false; }; diff --git a/src/Http/HttpRequester.cpp b/src/Http/HttpRequester.cpp index b518523f..b628f4f5 100644 --- a/src/Http/HttpRequester.cpp +++ b/src/Http/HttpRequester.cpp @@ -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; + _resTicker.resetTime(); + _timeOutSecond = timeOutSecond; sendRequest(url); - +} + +void HttpRequester::onManager(){ + if(_onResult && _resTicker.elapsedTime() > _timeOutSecond * 1000){ + //超时 + onDisconnect(SockException(Err_timeout,"wait http response timeout")); + shutdown(); + } } diff --git a/src/Http/HttpRequester.h b/src/Http/HttpRequester.h index 42dff595..2dc287e3 100644 --- a/src/Http/HttpRequester.h +++ b/src/Http/HttpRequester.h @@ -40,17 +40,17 @@ public: HttpRequester(); virtual ~HttpRequester(); - void startRequester(const string &url,const HttpRequesterResult &onResult); + void startRequester(const string &url,const HttpRequesterResult &onResult,uint32_t timeOutSecond = 10); private: 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 onResponseCompleted() override; void onDisconnect(const SockException &ex) override; - + void onManager() override; string _strRecvBody; HttpRequesterResult _onResult; - - + Ticker _resTicker; + uint32_t _timeOutSecond; }; }//namespace Http