From ae1b62c78f64f941fbdd1ed7a9fefa4671fb145c Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 6 Feb 2018 17:35:32 +0800 Subject: [PATCH] =?UTF-8?q?Http=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=B6=85=E6=97=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpDownloader.cpp | 12 +++++++++++- src/Http/HttpDownloader.h | 10 +++++++--- src/Http/HttpRequester.cpp | 13 +++++++++++-- src/Http/HttpRequester.h | 8 ++++---- 4 files changed, 33 insertions(+), 10 deletions(-) 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