ZLMediaKit/src/Http/WebSocketClient.h

327 lines
11 KiB
C
Raw Normal View History

2019-09-16 17:45:27 +08:00
/*
* MIT License
*
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ZLMEDIAKIT_WebSocketClient_H
#define ZLMEDIAKIT_WebSocketClient_H
2019-09-12 13:44:38 +08:00
#include "Util/util.h"
#include "Util/base64.h"
2019-09-16 17:45:27 +08:00
#include "Util/SHA1.h"
#include "Network/TcpClient.h"
#include "HttpClientImp.h"
#include "WebSocketSplitter.h"
2019-09-12 13:44:38 +08:00
using namespace toolkit;
2019-09-16 17:45:27 +08:00
namespace mediakit{
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
template <typename ClientType,WebSocketHeader::Type DataType>
class HttpWsClient;
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
template <typename ClientType,WebSocketHeader::Type DataType>
class ClientTypeImp : public ClientType {
2019-09-12 13:44:38 +08:00
public:
2019-09-16 17:45:27 +08:00
typedef function<int(const Buffer::Ptr &buf)> onBeforeSendCB;
friend class HttpWsClient<ClientType,DataType>;
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
template<typename ...ArgsType>
ClientTypeImp(ArgsType &&...args): ClientType(std::forward<ArgsType>(args)...){}
~ClientTypeImp() override {};
2019-09-12 13:44:38 +08:00
protected:
/**
2019-09-16 17:45:27 +08:00
* websocket协议
* @param buf
* @return
2019-09-12 13:44:38 +08:00
*/
2019-09-16 17:45:27 +08:00
int send(const Buffer::Ptr &buf) override{
if(_beforeSendCB){
return _beforeSendCB(buf);
}
return ClientType::send(buf);
}
2019-09-12 13:44:38 +08:00
/**
2019-09-16 17:45:27 +08:00
*
* @param cb
2019-09-12 13:44:38 +08:00
*/
2019-09-16 17:45:27 +08:00
void setOnBeforeSendCB(const onBeforeSendCB &cb){
_beforeSendCB = cb;
}
private:
onBeforeSendCB _beforeSendCB;
};
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
template <typename ClientType,WebSocketHeader::Type DataType = WebSocketHeader::TEXT>
class HttpWsClient : public HttpClientImp , public WebSocketSplitter{
public:
typedef shared_ptr<HttpWsClient> Ptr;
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
HttpWsClient(ClientTypeImp<ClientType,DataType> &delegate) : _delegate(delegate){
_Sec_WebSocket_Key = encodeBase64(SHA1::encode_bin(makeRandStr(16, false)));
}
~HttpWsClient(){}
void startWsClient(const string &ws_url,float fTimeOutSec){
string http_url = ws_url;
replace(http_url,"ws://","http://");
replace(http_url,"wss://","https://");
setMethod("GET");
addHeader("Upgrade","websocket");
addHeader("Connection","Upgrade");
addHeader("Sec-WebSocket-Version","13");
addHeader("Sec-WebSocket-Key",_Sec_WebSocket_Key);
_onRecv = nullptr;
sendRequest(http_url,fTimeOutSec);
}
2019-09-12 13:44:38 +08:00
protected:
2019-09-16 17:45:27 +08:00
//HttpClientImp override
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* http回复头
* @param status :200 OK
* @param headers http头
* @return content的长度-1:content>=0:content
* http头中带有Content-Length字段时
*/
int64_t onResponseHeader(const 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"));
if(Sec_WebSocket_Accept == const_cast<HttpHeader &>(headers)["Sec-WebSocket-Accept"]){
//success
onWebSocketException(SockException());
return 0;
}
shutdown(SockException(Err_shutdown,StrPrinter << "Sec-WebSocket-Accept mismatch"));
return 0;
}
shutdown(SockException(Err_shutdown,StrPrinter << "bad http status code:" << status));
return 0;
};
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* http回复完毕,
*/
void onResponseCompleted() override {}
//TcpClient override
void onManager() override {
if(_onRecv){
//websocket连接成功了
_delegate.onManager();
} else{
//websocket连接中...
HttpClientImp::onManager();
}
}
//数据全部发送完毕后回调
void onFlush() override{
if(_onRecv){
//websocket连接成功了
_delegate.onFlush();
} else{
//websocket连接中...
HttpClientImp::onFlush();
}
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* tcp连接结果
* @param ex
*/
void onConnect(const SockException &ex) override{
if(ex){
//tcp连接失败直接返回失败
onWebSocketException(ex);
return;
}
//开始websocket握手
HttpClientImp::onConnect(ex);
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* tcp收到数据
* @param pBuf
*/
void onRecv(const Buffer::Ptr &pBuf) override{
if(_onRecv){
//完成websocket握手后拦截websocket数据
_onRecv(pBuf);
}else{
//websocket握手数据
HttpClientImp::onRecv(pBuf);
}
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
//tcp连接断开
void onErr(const SockException &ex) override{
//tcp断开或者shutdown导致的断开
onWebSocketException(ex);
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
//WebSocketSplitter override
/**
* webSocket数据包包头onWebSocketDecodePlayload回调
* @param header
*/
void onWebSocketDecodeHeader(const WebSocketHeader &header) override{
_payload.clear();
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* webSocket数据包负载
* @param header
* @param ptr
* @param len
* @param recved ()header._playload_len时则接受完毕
*/
void onWebSocketDecodePlayload(const WebSocketHeader &header, const uint8_t *ptr, uint64_t len, uint64_t recved) override{
_payload.append((char *)ptr,len);
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* webSocket数据包后回调
* @param header
*/
void onWebSocketDecodeComplete(const WebSocketHeader &header_in) override{
WebSocketHeader& header = const_cast<WebSocketHeader&>(header_in);
auto flag = header._mask_flag;
//websocket客户端发送数据需要加密
header._mask_flag = true;
switch (header._opcode){
case WebSocketHeader::CLOSE:{
//服务器主动关闭
WebSocketSplitter::encode(header,nullptr);
shutdown(SockException(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)));
}
break;
case WebSocketHeader::CONTINUATION:{
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
}
break;
case WebSocketHeader::TEXT:
case WebSocketHeader::BINARY:{
//接收完毕websocket数据包触发onRecv事件
_delegate.onRecv(std::make_shared<BufferString>(std::move(_payload)));
}
break;
default:
break;
}
_payload.clear();
header._mask_flag = flag;
}
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
/**
* websocket数据编码回调
* @param ptr
* @param len
*/
void onWebSocketEncodeData(const Buffer::Ptr &buffer) override{
HttpClientImp::send(buffer);
}
2019-09-12 13:44:38 +08:00
private:
2019-09-16 17:45:27 +08:00
void onWebSocketException(const 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){
auto strongSelf = weakSelf.lock();
if(strongSelf){
WebSocketHeader header;
header._fin = true;
header._reserved = 0;
header._opcode = DataType;
//客户端需要加密
header._mask_flag = true;
strongSelf->WebSocketSplitter::encode(header,buf);
}
return buf->size();
});
//触发连接成功事件
_delegate._sock = HttpClientImp::_sock;
_delegate.onConnect(ex);
//拦截websocket数据接收
_onRecv = [this](const Buffer::Ptr &pBuf){
WebSocketSplitter::decode((uint8_t*)pBuf->data(),pBuf->size());
};
return;
}
//websocket握手失败或者tcp连接失败或者中途断开
if(_onRecv){
//握手成功之后的中途断开
_onRecv = nullptr;
_delegate.onErr(ex);
return;
}
//websocket握手失败或者tcp连接失败
_delegate.onConnect(ex);
}
2019-09-12 13:44:38 +08:00
private:
2019-09-16 17:45:27 +08:00
string _Sec_WebSocket_Key;
function<void(const Buffer::Ptr &pBuf)> _onRecv;
ClientTypeImp<ClientType,DataType> &_delegate;
string _payload;
2019-09-12 13:44:38 +08:00
};
2019-09-16 17:45:27 +08:00
template <typename ClientType,WebSocketHeader::Type DataType = WebSocketHeader::TEXT,bool userWSS = false >
class WebSocketClient : public ClientTypeImp<ClientType,DataType>{
public:
typedef std::shared_ptr<WebSocketClient> Ptr;
template<typename ...ArgsType>
WebSocketClient(ArgsType &&...args) : ClientTypeImp<ClientType,DataType>(std::forward<ArgsType>(args)...){
_wsClient.reset(new HttpWsClient<ClientType,DataType>(*this));
}
~WebSocketClient() override {}
void startConnect(const string &strUrl, uint16_t iPort, float fTimeOutSec = 3) override {
string ws_url;
if(userWSS){
ws_url = StrPrinter << "wss://" + strUrl << ":" << iPort << "/" ;
}else{
ws_url = StrPrinter << "ws://" + strUrl << ":" << iPort << "/" ;
}
_wsClient->startWsClient(ws_url,fTimeOutSec);
}
private:
typename HttpWsClient<ClientType,DataType>::Ptr _wsClient;
};
2019-09-12 13:44:38 +08:00
2019-09-16 17:45:27 +08:00
}//namespace mediakit
#endif //ZLMEDIAKIT_WebSocketClient_H