完善http超时管理

This commit is contained in:
xiongziliang 2018-02-28 17:30:12 +08:00
parent d9dea060d3
commit c2b11e3868
8 changed files with 15 additions and 16 deletions

View File

@ -35,7 +35,7 @@ HttpClient::HttpClient(){
} }
HttpClient::~HttpClient(){ HttpClient::~HttpClient(){
} }
void HttpClient::sendRequest(const string &strUrl){ void HttpClient::sendRequest(const string &strUrl,float fTimeOutSec){
auto protocol = FindField(strUrl.data(), NULL , "://"); auto protocol = FindField(strUrl.data(), NULL , "://");
uint16_t defaultPort; uint16_t defaultPort;
bool isHttps; bool isHttps;
@ -84,7 +84,7 @@ void HttpClient::sendRequest(const string &strUrl){
if(!alive() || bChanged){ if(!alive() || bChanged){
//InfoL << "reconnet:" << _lastHost; //InfoL << "reconnet:" << _lastHost;
startConnect(host, port); startConnect(host, port,fTimeOutSec);
}else{ }else{
SockException ex; SockException ex;
onConnect(ex); onConnect(ex);

View File

@ -68,7 +68,7 @@ public:
typedef std::shared_ptr<HttpClient> Ptr; typedef std::shared_ptr<HttpClient> Ptr;
HttpClient(); HttpClient();
virtual ~HttpClient(); virtual ~HttpClient();
virtual void sendRequest(const string &url); virtual void sendRequest(const string &url,float fTimeOutSec);
void clear(){ void clear(){
_header.clear(); _header.clear();
_body.clear(); _body.clear();

View File

@ -37,8 +37,8 @@ HttpClientImp::HttpClientImp() {
HttpClientImp::~HttpClientImp() { HttpClientImp::~HttpClientImp() {
} }
void HttpClientImp::sendRequest(const string& url) { void HttpClientImp::sendRequest(const string& url,float fTimeOutSec) {
HttpClient::sendRequest(url); HttpClient::sendRequest(url,fTimeOutSec);
if(_isHttps){ if(_isHttps){
#ifndef ENABLE_OPENSSL #ifndef ENABLE_OPENSSL
shutdown(); shutdown();

View File

@ -41,7 +41,7 @@ public:
typedef std::shared_ptr<HttpClientImp> Ptr; typedef std::shared_ptr<HttpClientImp> Ptr;
HttpClientImp(); HttpClientImp();
virtual ~HttpClientImp(); virtual ~HttpClientImp();
virtual void sendRequest(const string &url) override; virtual void sendRequest(const string &url,float fTimeOutSec) override;
#if defined(__GNUC__) && (__GNUC__ < 5) #if defined(__GNUC__) && (__GNUC__ < 5)
void public_onRecvBytes(const char *data,int len){ void public_onRecvBytes(const char *data,int len){

View File

@ -41,7 +41,7 @@ HttpDownloader::~HttpDownloader() {
closeFile(); closeFile();
} }
void HttpDownloader::startDownload(const string& url, const string& filePath,bool bAppend,uint32_t timeOutSecond) { void HttpDownloader::startDownload(const string& url, const string& filePath,bool bAppend,float timeOutSecond) {
_filePath = filePath; _filePath = filePath;
_timeOutSecond = timeOutSecond; _timeOutSecond = timeOutSecond;
_downloadTicker.resetTime(); _downloadTicker.resetTime();
@ -64,7 +64,7 @@ void HttpDownloader::startDownload(const string& url, const string& filePath,boo
addHeader("Range", StrPrinter << "bytes=" << currentLen << "-" << endl); addHeader("Range", StrPrinter << "bytes=" << currentLen << "-" << endl);
} }
setMethod("GET"); setMethod("GET");
sendRequest(url); sendRequest(url,timeOutSecond);
} }
void HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) { void HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {

View File

@ -39,11 +39,10 @@ public:
HttpDownloader(); HttpDownloader();
virtual ~HttpDownloader(); virtual ~HttpDownloader();
//开始下载文件,默认断点续传方式下载 //开始下载文件,默认断点续传方式下载
void startDownload(const string &url,const string &filePath = "",bool bAppend = false,uint32_t timeOutSecond = 10 ); void startDownload(const string &url,const string &filePath = "",bool bAppend = false, float timeOutSecond = 10 );
void startDownload(const string &url,const onDownloadResult &cb,uint32_t timeOutSecond = 10){ void startDownload(const string &url,const onDownloadResult &cb,float timeOutSecond = 10){
_timeOutSecond = timeOutSecond;
setOnResult(cb); setOnResult(cb);
startDownload(url); startDownload(url,"",false,timeOutSecond);
} }
void setOnResult(const onDownloadResult &cb){ void setOnResult(const onDownloadResult &cb){
_onResult = cb; _onResult = cb;

View File

@ -57,11 +57,11 @@ void HttpRequester::onDisconnect(const SockException &ex){
} }
} }
void HttpRequester::startRequester(const string &url,const HttpRequesterResult &onResult , uint32_t timeOutSecond){ void HttpRequester::startRequester(const string &url,const HttpRequesterResult &onResult , float timeOutSecond){
_onResult = onResult; _onResult = onResult;
_resTicker.resetTime(); _resTicker.resetTime();
_timeOutSecond = timeOutSecond; _timeOutSecond = timeOutSecond;
sendRequest(url); sendRequest(url,timeOutSecond);
} }
void HttpRequester::onManager(){ void HttpRequester::onManager(){

View File

@ -40,7 +40,7 @@ public:
HttpRequester(); HttpRequester();
virtual ~HttpRequester(); virtual ~HttpRequester();
void startRequester(const string &url,const HttpRequesterResult &onResult,uint32_t timeOutSecond = 10); void startRequester(const string &url,const HttpRequesterResult &onResult,float 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;
@ -50,7 +50,7 @@ private:
string _strRecvBody; string _strRecvBody;
HttpRequesterResult _onResult; HttpRequesterResult _onResult;
Ticker _resTicker; Ticker _resTicker;
uint32_t _timeOutSecond; float _timeOutSecond;
}; };
}//namespace Http }//namespace Http