mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-13 11:41:44 +08:00
整理命名空间 (#1409)
* feat: remove using namespace mediakit in header files. (cherry picked from commit d44aeb339a8a0e1f0455be82b21fe4b1b536299f) * feat: remove using namespace mediakit in FFmpegSource.h * feat: remove using namespace mediakit in RtpExt.h * feat: remove using namespace mediakit in header files. * feat: remove using namespace std in header files. * feat: remove using namespace std in header files when zltoolkit remove std in header * 补充命名空间 * 整理命名空间 * 整理命名空间2 * 修复macos ci * 修复编译问题 * 修复编译问题2 * 修复编译问题3 Co-authored-by: Johnny <hellojinqiang@gmail.com> Co-authored-by: Xiaofeng Wang <wasphin@gmail.com>
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
#include "Network/TcpClient.h"
|
||||
#include "HttpClientImp.h"
|
||||
#include "WebSocketSplitter.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
@@ -32,8 +31,9 @@ class HttpWsClient;
|
||||
template <typename ClientType,WebSocketHeader::Type DataType>
|
||||
class ClientTypeImp : public ClientType {
|
||||
public:
|
||||
typedef function<ssize_t (const Buffer::Ptr &buf)> onBeforeSendCB;
|
||||
friend class HttpWsClient<ClientType,DataType>;
|
||||
friend class HttpWsClient<ClientType, DataType>;
|
||||
|
||||
using onBeforeSendCB = std::function<ssize_t(const toolkit::Buffer::Ptr &buf)>;
|
||||
|
||||
template<typename ...ArgsType>
|
||||
ClientTypeImp(ArgsType &&...args): ClientType(std::forward<ArgsType>(args)...){}
|
||||
@@ -43,7 +43,7 @@ protected:
|
||||
/**
|
||||
* 发送前拦截并打包为websocket协议
|
||||
*/
|
||||
ssize_t send(Buffer::Ptr buf) override{
|
||||
ssize_t send(toolkit::Buffer::Ptr buf) override{
|
||||
if(_beforeSendCB){
|
||||
return _beforeSendCB(buf);
|
||||
}
|
||||
@@ -70,11 +70,11 @@ private:
|
||||
template <typename ClientType,WebSocketHeader::Type DataType = WebSocketHeader::TEXT>
|
||||
class HttpWsClient : public HttpClientImp , public WebSocketSplitter{
|
||||
public:
|
||||
typedef shared_ptr<HttpWsClient> Ptr;
|
||||
typedef std::shared_ptr<HttpWsClient> Ptr;
|
||||
|
||||
HttpWsClient(const std::shared_ptr<ClientTypeImp<ClientType, DataType> > &delegate) : _weak_delegate(delegate),
|
||||
_delegate(*delegate) {
|
||||
_Sec_WebSocket_Key = encodeBase64(makeRandStr(16, false));
|
||||
_Sec_WebSocket_Key = encodeBase64(toolkit::makeRandStr(16, false));
|
||||
setPoller(_delegate.getPoller());
|
||||
}
|
||||
~HttpWsClient(){}
|
||||
@@ -84,10 +84,10 @@ public:
|
||||
* @param ws_url ws连接url
|
||||
* @param fTimeOutSec 超时时间
|
||||
*/
|
||||
void startWsClient(const string &ws_url, float fTimeOutSec) {
|
||||
string http_url = ws_url;
|
||||
replace(http_url, "ws://", "http://");
|
||||
replace(http_url, "wss://", "https://");
|
||||
void startWsClient(const std::string &ws_url, float fTimeOutSec) {
|
||||
std::string http_url = ws_url;
|
||||
toolkit::replace(http_url, "ws://", "http://");
|
||||
toolkit::replace(http_url, "wss://", "https://");
|
||||
setMethod("GET");
|
||||
addHeader("Upgrade", "websocket");
|
||||
addHeader("Connection", "Upgrade");
|
||||
@@ -120,27 +120,27 @@ protected:
|
||||
* @param status 状态码,譬如:200 OK
|
||||
* @param headers http头
|
||||
*/
|
||||
void onResponseHeader(const string &status,const HttpHeader &headers) override {
|
||||
void onResponseHeader(const std::string &status, const HttpHeader &headers) override {
|
||||
if(status == "101"){
|
||||
auto Sec_WebSocket_Accept = encodeBase64(SHA1::encode_bin(_Sec_WebSocket_Key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
|
||||
auto Sec_WebSocket_Accept = encodeBase64(toolkit::SHA1::encode_bin(_Sec_WebSocket_Key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
|
||||
if(Sec_WebSocket_Accept == const_cast<HttpHeader &>(headers)["Sec-WebSocket-Accept"]){
|
||||
//success
|
||||
onWebSocketException(SockException());
|
||||
onWebSocketException(toolkit::SockException());
|
||||
//防止ws服务器返回Content-Length
|
||||
const_cast<HttpHeader &>(headers).erase("Content-Length");
|
||||
return;
|
||||
}
|
||||
shutdown(SockException(Err_shutdown, StrPrinter << "Sec-WebSocket-Accept mismatch"));
|
||||
shutdown(toolkit::SockException(toolkit::Err_shutdown, StrPrinter << "Sec-WebSocket-Accept mismatch"));
|
||||
return;
|
||||
}
|
||||
|
||||
shutdown(SockException(Err_shutdown,StrPrinter << "bad http status code:" << status));
|
||||
shutdown(toolkit::SockException(toolkit::Err_shutdown,StrPrinter << "bad http status code:" << status));
|
||||
};
|
||||
|
||||
/**
|
||||
* 接收http回复完毕,
|
||||
*/
|
||||
void onResponseCompleted(const SockException &ex) override {}
|
||||
void onResponseCompleted(const toolkit::SockException &ex) override {}
|
||||
|
||||
/**
|
||||
* 接收websocket负载数据
|
||||
@@ -154,7 +154,7 @@ protected:
|
||||
|
||||
//TcpClient override
|
||||
|
||||
void onRecv(const Buffer::Ptr &buf) override {
|
||||
void onRecv(const toolkit::Buffer::Ptr &buf) override {
|
||||
auto strong_ref = _weak_delegate.lock();;
|
||||
HttpClientImp::onRecv(buf);
|
||||
}
|
||||
@@ -190,7 +190,7 @@ protected:
|
||||
/**
|
||||
* tcp连接结果
|
||||
*/
|
||||
void onConnect(const SockException &ex) override {
|
||||
void onConnect(const toolkit::SockException &ex) override {
|
||||
auto strong_ref = _weak_delegate.lock();;
|
||||
if (ex) {
|
||||
//tcp连接失败,直接返回失败
|
||||
@@ -204,7 +204,7 @@ protected:
|
||||
/**
|
||||
* tcp连接断开
|
||||
*/
|
||||
void onErr(const SockException &ex) override {
|
||||
void onErr(const toolkit::SockException &ex) override {
|
||||
auto strong_ref = _weak_delegate.lock();;
|
||||
//tcp断开或者shutdown导致的断开
|
||||
onWebSocketException(ex);
|
||||
@@ -245,14 +245,14 @@ protected:
|
||||
case WebSocketHeader::CLOSE:{
|
||||
//服务器主动关闭
|
||||
WebSocketSplitter::encode(header,nullptr);
|
||||
shutdown(SockException(Err_eof,"websocket server close the connection"));
|
||||
shutdown(toolkit::SockException(toolkit::Err_eof,"websocket server close the connection"));
|
||||
break;
|
||||
}
|
||||
|
||||
case WebSocketHeader::PING:{
|
||||
//心跳包
|
||||
header._opcode = WebSocketHeader::PONG;
|
||||
WebSocketSplitter::encode(header,std::make_shared<BufferString>(std::move(_payload_section)));
|
||||
WebSocketSplitter::encode(header,std::make_shared<toolkit::BufferString>(std::move(_payload_section)));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -294,17 +294,17 @@ protected:
|
||||
* @param ptr 数据指针
|
||||
* @param len 数据指针长度
|
||||
*/
|
||||
void onWebSocketEncodeData(Buffer::Ptr buffer) override{
|
||||
void onWebSocketEncodeData(toolkit::Buffer::Ptr buffer) override{
|
||||
HttpClientImp::send(std::move(buffer));
|
||||
}
|
||||
|
||||
private:
|
||||
void onWebSocketException(const SockException &ex){
|
||||
void onWebSocketException(const toolkit::SockException &ex){
|
||||
if(!ex){
|
||||
//websocket握手成功
|
||||
//此处截取TcpClient派生类发送的数据并进行websocket协议打包
|
||||
weak_ptr<HttpWsClient> weakSelf = dynamic_pointer_cast<HttpWsClient>(shared_from_this());
|
||||
_delegate.setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){
|
||||
std::weak_ptr<HttpWsClient> weakSelf = std::dynamic_pointer_cast<HttpWsClient>(shared_from_this());
|
||||
_delegate.setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(strongSelf){
|
||||
WebSocketHeader header;
|
||||
@@ -343,12 +343,12 @@ private:
|
||||
}
|
||||
|
||||
private:
|
||||
string _Sec_WebSocket_Key;
|
||||
function<void(const char *data, size_t len)> _onRecv;
|
||||
weak_ptr<ClientTypeImp<ClientType,DataType> > _weak_delegate;
|
||||
ClientTypeImp<ClientType,DataType> &_delegate;
|
||||
string _payload_section;
|
||||
string _payload_cache;
|
||||
std::string _Sec_WebSocket_Key;
|
||||
std::function<void(const char *data, size_t len)> _onRecv;
|
||||
std::weak_ptr<ClientTypeImp<ClientType, DataType>> _weak_delegate;
|
||||
ClientTypeImp<ClientType, DataType> &_delegate;
|
||||
std::string _payload_section;
|
||||
std::string _payload_cache;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -378,8 +378,8 @@ public:
|
||||
* @param timeout_sec 超时时间
|
||||
* @param local_port 本地监听端口,此处不起作用
|
||||
*/
|
||||
void startConnect(const string &host, uint16_t port, float timeout_sec = 3, uint16_t local_port = 0) override {
|
||||
string ws_url;
|
||||
void startConnect(const std::string &host, uint16_t port, float timeout_sec = 3, uint16_t local_port = 0) override {
|
||||
std::string ws_url;
|
||||
if (useWSS) {
|
||||
//加密的ws
|
||||
ws_url = StrPrinter << "wss://" + host << ":" << port << "/";
|
||||
@@ -390,9 +390,9 @@ public:
|
||||
startWebSocket(ws_url, timeout_sec);
|
||||
}
|
||||
|
||||
void startWebSocket(const string &ws_url,float fTimeOutSec = 3){
|
||||
_wsClient = std::make_shared<HttpWsClient<ClientType,DataType> >(static_pointer_cast<WebSocketClient>(this->shared_from_this()));
|
||||
_wsClient->setOnCreateSocket([this](const EventPoller::Ptr &){
|
||||
void startWebSocket(const std::string &ws_url, float fTimeOutSec = 3) {
|
||||
_wsClient = std::make_shared<HttpWsClient<ClientType, DataType> >(std::static_pointer_cast<WebSocketClient>(this->shared_from_this()));
|
||||
_wsClient->setOnCreateSocket([this](const toolkit::EventPoller::Ptr &){
|
||||
return this->createSocket();
|
||||
});
|
||||
_wsClient->startWsClient(ws_url,fTimeOutSec);
|
||||
|
||||
Reference in New Issue
Block a user