简化ssl相关代码

This commit is contained in:
xiongziliang 2019-03-27 10:01:18 +08:00
parent 6d7f987783
commit e3ab51b337
5 changed files with 10 additions and 69 deletions

@ -1 +1 @@
Subproject commit 8e90a8dbefe9060fdb86b9dc8036345aa69faf41 Subproject commit ea623e89153b7f5e4f693e3f2d4c5e60b79540ed

View File

@ -100,7 +100,6 @@ 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;
@ -132,12 +131,8 @@ void HttpClient::onConnect(const SockException &ex) {
} }
void HttpClient::onRecv(const Buffer::Ptr &pBuf) { void HttpClient::onRecv(const Buffer::Ptr &pBuf) {
onRecvBytes(pBuf->data(), pBuf->size());
}
void HttpClient::onRecvBytes(const char *data, int size) {
_aliveTicker.resetTime(); _aliveTicker.resetTime();
HttpRequestSplitter::input(data, size); HttpRequestSplitter::input(pBuf->data(), pBuf->size());
} }
void HttpClient::onErr(const SockException &ex) { void HttpClient::onErr(const SockException &ex) {

View File

@ -283,13 +283,6 @@ protected:
DebugL; DebugL;
} }
/**
* http回复数据回调
* @param data
* @param size
*/
virtual void onRecvBytes(const char *data,int size);
/** /**
* http链接断开回调 * http链接断开回调
* @param ex * @param ex
@ -299,9 +292,6 @@ 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;

View File

@ -28,37 +28,13 @@
namespace mediakit { namespace mediakit {
#if defined(ENABLE_OPENSSL) void HttpClientImp::onConnect(const SockException &ex) {
void HttpClientImp::onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) { if(!_isHttps){
if(_isHttps){ HttpClient::onConnect(ex);
_sslBox.reset(new SSL_Box(false)); } else {
_sslBox->setOnDecData([this](const char *data, uint32_t len){ TcpClientWithSSL<HttpClient>::onConnect(ex);
public_onRecvBytes(data,len);
});
_sslBox->setOnEncData([this](const char *data, uint32_t len){
public_send(data,len);
});
}else{
_sslBox.reset();
} }
}
void HttpClientImp::onRecvBytes(const char* data, int size) {
if(_sslBox){
_sslBox->onRecv(data,size);
}else{
HttpClient::onRecvBytes(data,size);
}
} }
int HttpClientImp::send(const Buffer::Ptr &buf) {
if(_sslBox){
_sslBox->onSend(buf->data(),buf->size());
return buf->size();
}
return HttpClient::send(buf);
}
#endif //defined(ENABLE_OPENSSL)
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -28,41 +28,21 @@
#define SRC_HTTP_HTTPCLIENTIMP_H_ #define SRC_HTTP_HTTPCLIENTIMP_H_
#include "HttpClient.h" #include "HttpClient.h"
#ifdef ENABLE_OPENSSL
#include "Util/SSLBox.h" #include "Util/SSLBox.h"
#endif //ENABLE_OPENSSL
using namespace toolkit; using namespace toolkit;
namespace mediakit { namespace mediakit {
#if defined(ENABLE_OPENSSL) class HttpClientImp: public TcpClientWithSSL<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() {}
protected:
inline void public_onRecvBytes(const char *data,int len){ void onConnect(const SockException &ex) override ;
HttpClient::onRecvBytes(data,len);
}
inline void public_send(const char *data, uint32_t len){
HttpClient::send(obtainBuffer(data,len));
}
private:
void onRecvBytes(const char *data,int size) override;
int send(const Buffer::Ptr &buf) override;
void onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) override;
private:
std::shared_ptr<SSL_Box> _sslBox;
}; };
#else
typedef HttpClient HttpClientImp;
#endif // defined(ENABLE_OPENSSL)
} /* namespace mediakit */ } /* namespace mediakit */
#endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */ #endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */