mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +08:00
完善Http客户端复用机制
This commit is contained in:
parent
bdaeada3c9
commit
faab85e2ca
@ -60,7 +60,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
|||||||
if (_path.empty()) {
|
if (_path.empty()) {
|
||||||
_path = "/";
|
_path = "/";
|
||||||
}
|
}
|
||||||
auto port = atoi(FindField(host.data(), ":", NULL).data());
|
uint16_t port = atoi(FindField(host.data(), ":", NULL).data());
|
||||||
if (port <= 0) {
|
if (port <= 0) {
|
||||||
//默认端口
|
//默认端口
|
||||||
port = defaultPort;
|
port = defaultPort;
|
||||||
@ -99,6 +99,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
|||||||
|
|
||||||
if (!alive() || bChanged) {
|
if (!alive() || bChanged) {
|
||||||
//InfoL << "reconnet:" << _lastHost;
|
//InfoL << "reconnet:" << _lastHost;
|
||||||
|
onBeforeConnect(host, port , fTimeOutSec);
|
||||||
startConnect(host, port, fTimeOutSec);
|
startConnect(host, port, fTimeOutSec);
|
||||||
} else {
|
} else {
|
||||||
SockException ex;
|
SockException ex;
|
||||||
|
@ -207,13 +207,21 @@ public:
|
|||||||
HttpClient();
|
HttpClient();
|
||||||
virtual ~HttpClient();
|
virtual ~HttpClient();
|
||||||
virtual void sendRequest(const string &url,float fTimeOutSec);
|
virtual void sendRequest(const string &url,float fTimeOutSec);
|
||||||
void clear(){
|
|
||||||
|
virtual void clear(){
|
||||||
_header.clear();
|
_header.clear();
|
||||||
_body.reset();
|
_body.reset();
|
||||||
_method.clear();
|
_method.clear();
|
||||||
_path.clear();
|
_path.clear();
|
||||||
_parser.Clear();
|
_parser.Clear();
|
||||||
|
_recvedBodySize = 0;
|
||||||
|
_totalBodySize = 0;
|
||||||
|
_aliveTicker.resetTime();
|
||||||
|
_fTimeOutSec = 0;
|
||||||
|
_chunkedSplitter.reset();
|
||||||
|
HttpRequestSplitter::reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMethod(const string &method){
|
void setMethod(const string &method){
|
||||||
_method = method;
|
_method = method;
|
||||||
}
|
}
|
||||||
@ -291,6 +299,9 @@ protected:
|
|||||||
//HttpRequestSplitter override
|
//HttpRequestSplitter override
|
||||||
int64_t onRecvHeader(const char *data,uint64_t len) override ;
|
int64_t onRecvHeader(const char *data,uint64_t len) override ;
|
||||||
void onRecvContent(const char *data,uint64_t len) override;
|
void onRecvContent(const char *data,uint64_t len) override;
|
||||||
|
|
||||||
|
//在连接服务器前调用一次
|
||||||
|
virtual void onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) {};
|
||||||
protected:
|
protected:
|
||||||
virtual void onConnect(const SockException &ex) override;
|
virtual void onConnect(const SockException &ex) override;
|
||||||
virtual void onRecv(const Buffer::Ptr &pBuf) override;
|
virtual void onRecv(const Buffer::Ptr &pBuf) override;
|
||||||
@ -303,7 +314,6 @@ private:
|
|||||||
protected:
|
protected:
|
||||||
bool _isHttps;
|
bool _isHttps;
|
||||||
private:
|
private:
|
||||||
//send
|
|
||||||
HttpHeader _header;
|
HttpHeader _header;
|
||||||
HttpBody::Ptr _body;
|
HttpBody::Ptr _body;
|
||||||
string _method;
|
string _method;
|
||||||
|
@ -28,47 +28,21 @@
|
|||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
HttpClientImp::HttpClientImp() {
|
#if defined(ENABLE_OPENSSL)
|
||||||
// TODO Auto-generated constructor stub
|
void HttpClientImp::onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpClientImp::~HttpClientImp() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClientImp::sendRequest(const string& url,float fTimeOutSec) {
|
|
||||||
HttpClient::sendRequest(url,fTimeOutSec);
|
|
||||||
if(_isHttps){
|
if(_isHttps){
|
||||||
#ifndef ENABLE_OPENSSL
|
|
||||||
shutdown();
|
|
||||||
throw std::invalid_argument("不支持HTTPS协议");
|
|
||||||
#else
|
|
||||||
_sslBox.reset(new SSL_Box(false));
|
_sslBox.reset(new SSL_Box(false));
|
||||||
_sslBox->setOnDecData([this](const char *data, uint32_t len){
|
_sslBox->setOnDecData([this](const char *data, uint32_t len){
|
||||||
#if defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
public_onRecvBytes(data,len);
|
public_onRecvBytes(data,len);
|
||||||
#else//defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
HttpClient::onRecvBytes(data,len);
|
|
||||||
#endif//defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
});
|
});
|
||||||
_sslBox->setOnEncData([this](const char *data, uint32_t len){
|
_sslBox->setOnEncData([this](const char *data, uint32_t len){
|
||||||
|
|
||||||
#if defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
public_send(data,len);
|
public_send(data,len);
|
||||||
#else//defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
HttpClient::send(obtainBuffer(data,len));
|
|
||||||
#endif//defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
});
|
});
|
||||||
#endif //ENABLE_OPENSSL
|
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
#ifdef ENABLE_OPENSSL
|
|
||||||
_sslBox.reset();
|
_sslBox.reset();
|
||||||
#endif //ENABLE_OPENSSL
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_OPENSSL
|
|
||||||
void HttpClientImp::onRecvBytes(const char* data, int size) {
|
void HttpClientImp::onRecvBytes(const char* data, int size) {
|
||||||
if(_sslBox){
|
if(_sslBox){
|
||||||
_sslBox->onRecv(data,size);
|
_sslBox->onRecv(data,size);
|
||||||
@ -84,6 +58,7 @@ int HttpClientImp::send(const Buffer::Ptr &buf) {
|
|||||||
}
|
}
|
||||||
return HttpClient::send(buf);
|
return HttpClient::send(buf);
|
||||||
}
|
}
|
||||||
#endif //ENABLE_OPENSSL
|
|
||||||
|
#endif //defined(ENABLE_OPENSSL)
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
@ -36,29 +36,33 @@ using namespace toolkit;
|
|||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
|
#if defined(ENABLE_OPENSSL)
|
||||||
class HttpClientImp: public HttpClient {
|
class HttpClientImp: public HttpClient {
|
||||||
public:
|
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,float fTimeOutSec) override;
|
|
||||||
|
|
||||||
#if defined(__GNUC__) && (__GNUC__ < 5)
|
inline void public_onRecvBytes(const char *data,int len){
|
||||||
void public_onRecvBytes(const char *data,int len){
|
|
||||||
HttpClient::onRecvBytes(data,len);
|
HttpClient::onRecvBytes(data,len);
|
||||||
}
|
}
|
||||||
void public_send(const char *data, uint32_t len){
|
inline void public_send(const char *data, uint32_t len){
|
||||||
HttpClient::send(obtainBuffer(data,len));
|
HttpClient::send(obtainBuffer(data,len));
|
||||||
}
|
}
|
||||||
#endif //defined(__GNUC__) && (__GNUC__ < 5)
|
|
||||||
private:
|
private:
|
||||||
#ifdef ENABLE_OPENSSL
|
void onRecvBytes(const char *data,int size) override;
|
||||||
virtual void onRecvBytes(const char *data,int size) override;
|
int send(const Buffer::Ptr &buf) override;
|
||||||
virtual int send(const Buffer::Ptr &buf) override;
|
void onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) override;
|
||||||
|
private:
|
||||||
std::shared_ptr<SSL_Box> _sslBox;
|
std::shared_ptr<SSL_Box> _sslBox;
|
||||||
#endif //ENABLE_OPENSSL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
typedef HttpClient HttpClientImp;
|
||||||
|
|
||||||
|
#endif // defined(ENABLE_OPENSSL)
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
|
||||||
#endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */
|
#endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */
|
||||||
|
@ -126,6 +126,7 @@ void HttpRequestSplitter::setContentLen(int64_t content_len) {
|
|||||||
|
|
||||||
void HttpRequestSplitter::reset() {
|
void HttpRequestSplitter::reset() {
|
||||||
_content_len = 0;
|
_content_len = 0;
|
||||||
|
_remain_data_size = 0;
|
||||||
_remain_data.clear();
|
_remain_data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,5 +63,11 @@ void HttpRequester::startRequester(const string &url,const HttpRequesterResult &
|
|||||||
sendRequest(url,timeOutSecond);
|
sendRequest(url,timeOutSecond);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HttpRequester::clear() {
|
||||||
|
HttpClientImp::clear();
|
||||||
|
_strRecvBody.clear();
|
||||||
|
_onResult = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}//namespace mediakit
|
}//namespace mediakit
|
||||||
|
@ -39,6 +39,7 @@ public:
|
|||||||
HttpRequester();
|
HttpRequester();
|
||||||
virtual ~HttpRequester();
|
virtual ~HttpRequester();
|
||||||
void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10);
|
void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10);
|
||||||
|
void clear() override ;
|
||||||
private:
|
private:
|
||||||
int64_t onResponseHeader(const string &status,const HttpHeader &headers) override;
|
int64_t 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user