2018-12-20 10:31:31 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-12-20 10:31:31 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-12-20 10:31:31 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-12-20 10:31:31 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_WEBSOCKETSESSION_H
|
|
|
|
|
#define ZLMEDIAKIT_WEBSOCKETSESSION_H
|
|
|
|
|
|
|
|
|
|
#include "HttpSession.h"
|
2019-11-30 11:44:05 +08:00
|
|
|
|
#include "Network/TcpServer.h"
|
2018-12-20 10:31:31 +08:00
|
|
|
|
|
2020-01-15 15:11:42 +08:00
|
|
|
|
/**
|
|
|
|
|
* 数据发送拦截器
|
|
|
|
|
*/
|
|
|
|
|
class SendInterceptor{
|
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using onBeforeSendCB =std::function<ssize_t (const toolkit::Buffer::Ptr &buf)>;
|
|
|
|
|
|
2020-01-15 15:11:42 +08:00
|
|
|
|
SendInterceptor() = default;
|
|
|
|
|
virtual ~SendInterceptor() = default;
|
|
|
|
|
virtual void setOnBeforeSendCB(const onBeforeSendCB &cb) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-19 09:33:10 +08:00
|
|
|
|
* 该类实现了Session派生类发送数据的截取
|
2020-01-15 15:11:42 +08:00
|
|
|
|
* 目的是发送业务数据前进行websocket协议的打包
|
|
|
|
|
*/
|
2022-11-19 09:33:10 +08:00
|
|
|
|
template <typename SessionType>
|
|
|
|
|
class SessionTypeImp : public SessionType, public SendInterceptor{
|
2020-01-15 15:11:42 +08:00
|
|
|
|
public:
|
2022-11-19 09:33:10 +08:00
|
|
|
|
using Ptr = std::shared_ptr<SessionTypeImp>;
|
2020-01-15 15:11:42 +08:00
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
SessionTypeImp(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock) :
|
2023-03-11 11:08:14 +08:00
|
|
|
|
SessionType(pSock) {}
|
2020-01-15 15:11:42 +08:00
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
~SessionTypeImp() = default;
|
2020-01-15 15:11:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置发送数据截取回调函数
|
|
|
|
|
* @param cb 截取回调函数
|
|
|
|
|
*/
|
|
|
|
|
void setOnBeforeSendCB(const onBeforeSendCB &cb) override {
|
|
|
|
|
_beforeSendCB = cb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* 重载send函数截取数据
|
|
|
|
|
* @param buf 需要截取的数据
|
|
|
|
|
* @return 数据字节数
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
ssize_t send(toolkit::Buffer::Ptr buf) override {
|
2020-01-15 15:11:42 +08:00
|
|
|
|
if (_beforeSendCB) {
|
|
|
|
|
return _beforeSendCB(buf);
|
|
|
|
|
}
|
2022-11-19 09:33:10 +08:00
|
|
|
|
return SessionType::send(std::move(buf));
|
2020-01-15 15:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
onBeforeSendCB _beforeSendCB;
|
2020-01-15 15:11:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
template <typename SessionType>
|
|
|
|
|
class SessionCreator {
|
2020-01-15 15:11:42 +08:00
|
|
|
|
public:
|
2022-11-19 09:33:10 +08:00
|
|
|
|
//返回的Session必须派生于SendInterceptor,可以返回null
|
|
|
|
|
toolkit::Session::Ptr operator()(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock){
|
|
|
|
|
return std::make_shared<SessionTypeImp<SessionType> >(header,parent,pSock);
|
2020-01-15 15:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通过该模板类可以透明化WebSocket协议,
|
|
|
|
|
* 用户只要实现WebSock协议下的具体业务协议,譬如基于WebSocket协议的Rtmp协议等
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
template<typename Creator, typename HttpSessionType = mediakit::HttpSession, mediakit::WebSocketHeader::Type DataType = mediakit::WebSocketHeader::TEXT>
|
2020-01-15 15:11:42 +08:00
|
|
|
|
class WebSocketSessionBase : public HttpSessionType {
|
2018-12-20 10:31:31 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
WebSocketSessionBase(const toolkit::Socket::Ptr &pSock) : HttpSessionType(pSock){}
|
2023-04-22 21:59:26 +08:00
|
|
|
|
virtual ~WebSocketSessionBase() = default;
|
2018-12-20 10:31:31 +08:00
|
|
|
|
|
|
|
|
|
//收到eof或其他导致脱离TcpServer事件的回调
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onError(const toolkit::SockException &err) override{
|
2018-12-20 10:31:31 +08:00
|
|
|
|
HttpSessionType::onError(err);
|
|
|
|
|
if(_session){
|
|
|
|
|
_session->onError(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//每隔一段时间触发,用来做超时管理
|
|
|
|
|
void onManager() override{
|
2023-03-11 11:08:14 +08:00
|
|
|
|
if (_session) {
|
2018-12-20 10:31:31 +08:00
|
|
|
|
_session->onManager();
|
2023-03-11 11:08:14 +08:00
|
|
|
|
} else {
|
2018-12-20 10:31:31 +08:00
|
|
|
|
HttpSessionType::onManager();
|
|
|
|
|
}
|
2023-03-11 11:08:14 +08:00
|
|
|
|
if (!_session) {
|
|
|
|
|
// websocket尚未链接
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_recv_ticker.elapsedTime() > 30 * 1000) {
|
|
|
|
|
HttpSessionType::shutdown(toolkit::SockException(toolkit::Err_timeout, "websocket timeout"));
|
|
|
|
|
} else if (_recv_ticker.elapsedTime() > 10 * 1000) {
|
|
|
|
|
// 没收到回复,每10秒发送次ping 包
|
|
|
|
|
mediakit::WebSocketHeader header;
|
|
|
|
|
header._fin = true;
|
|
|
|
|
header._reserved = 0;
|
|
|
|
|
header._opcode = mediakit::WebSocketHeader::PING;
|
|
|
|
|
header._mask_flag = false;
|
|
|
|
|
HttpSessionType::encode(header, nullptr);
|
|
|
|
|
}
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void attachServer(const toolkit::Server &server) override{
|
2018-12-20 10:31:31 +08:00
|
|
|
|
HttpSessionType::attachServer(server);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
_weak_server = const_cast<toolkit::Server &>(server).shared_from_this();
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
protected:
|
2019-09-16 17:42:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* websocket客户端连接上事件
|
|
|
|
|
* @param header http头
|
|
|
|
|
* @return true代表允许websocket连接,否则拒绝
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool onWebSocketConnect(const mediakit::Parser &header) override{
|
2019-09-16 17:42:52 +08:00
|
|
|
|
//创建websocket session类
|
2023-03-11 11:08:14 +08:00
|
|
|
|
_session = _creator(header, *this, HttpSessionType::getSock());
|
|
|
|
|
if (!_session) {
|
|
|
|
|
// 此url不允许创建websocket连接
|
2020-01-15 15:11:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
auto strongServer = _weak_server.lock();
|
2023-03-11 11:08:14 +08:00
|
|
|
|
if (strongServer) {
|
2019-09-16 17:42:52 +08:00
|
|
|
|
_session->attachServer(*strongServer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//此处截取数据并进行websocket协议打包
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::weak_ptr<WebSocketSessionBase> weakSelf = std::dynamic_pointer_cast<WebSocketSessionBase>(HttpSessionType::shared_from_this());
|
|
|
|
|
std::dynamic_pointer_cast<SendInterceptor>(_session)->setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf) {
|
2019-09-16 17:42:52 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
2020-01-15 15:11:42 +08:00
|
|
|
|
if (strongSelf) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
mediakit::WebSocketHeader header;
|
2019-09-16 17:42:52 +08:00
|
|
|
|
header._fin = true;
|
|
|
|
|
header._reserved = 0;
|
|
|
|
|
header._opcode = DataType;
|
|
|
|
|
header._mask_flag = false;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
strongSelf->HttpSessionType::encode(header, buf);
|
2019-09-16 17:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
return buf->size();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//允许websocket客户端
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 开始收到一个webSocket数据包
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWebSocketDecodeHeader(const mediakit::WebSocketHeader &packet) override{
|
2018-12-20 10:31:31 +08:00
|
|
|
|
//新包,原来的包残余数据清空掉
|
2020-08-08 12:17:06 +08:00
|
|
|
|
_payload_section.clear();
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到websocket数据包负载
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWebSocketDecodePayload(const mediakit::WebSocketHeader &packet,const uint8_t *ptr,size_t len,size_t recved) override {
|
2020-08-08 12:17:06 +08:00
|
|
|
|
_payload_section.append((char *)ptr,len);
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接收到完整的一个webSocket数据包后回调
|
|
|
|
|
* @param header 数据包包头
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWebSocketDecodeComplete(const mediakit::WebSocketHeader &header_in) override {
|
|
|
|
|
auto header = const_cast<mediakit::WebSocketHeader&>(header_in);
|
2018-12-20 10:31:31 +08:00
|
|
|
|
auto flag = header._mask_flag;
|
|
|
|
|
header._mask_flag = false;
|
2023-03-11 11:08:14 +08:00
|
|
|
|
_recv_ticker.resetTime();
|
2018-12-20 10:31:31 +08:00
|
|
|
|
switch (header._opcode){
|
2022-02-02 20:34:50 +08:00
|
|
|
|
case mediakit::WebSocketHeader::CLOSE:{
|
2019-08-13 12:00:21 +08:00
|
|
|
|
HttpSessionType::encode(header,nullptr);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HttpSessionType::shutdown(toolkit::SockException(toolkit::Err_shutdown, "recv close request from client"));
|
2018-12-20 10:31:31 +08:00
|
|
|
|
break;
|
2020-08-08 12:17:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
case mediakit::WebSocketHeader::PING:{
|
|
|
|
|
header._opcode = mediakit::WebSocketHeader::PONG;
|
|
|
|
|
HttpSessionType::encode(header,std::make_shared<toolkit::BufferString>(_payload_section));
|
2018-12-20 10:31:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
case mediakit::WebSocketHeader::CONTINUATION:
|
|
|
|
|
case mediakit::WebSocketHeader::TEXT:
|
|
|
|
|
case mediakit::WebSocketHeader::BINARY:{
|
2020-08-08 12:17:06 +08:00
|
|
|
|
if (!header._fin) {
|
|
|
|
|
//还有后续分片数据, 我们先缓存数据,所有分片收集完成才一次性输出
|
|
|
|
|
_payload_cache.append(std::move(_payload_section));
|
|
|
|
|
if (_payload_cache.size() < MAX_WS_PACKET) {
|
|
|
|
|
//还有内存容量缓存分片数据
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//分片缓存太大,需要清空
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//最后一个包
|
|
|
|
|
if (_payload_cache.empty()) {
|
|
|
|
|
//这个包是唯一个分片
|
2022-02-02 20:34:50 +08:00
|
|
|
|
_session->onRecv(std::make_shared<mediakit::WebSocketBuffer>(header._opcode, header._fin, std::move(_payload_section)));
|
2020-08-08 12:17:06 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这个包由多个分片组成
|
|
|
|
|
_payload_cache.append(std::move(_payload_section));
|
2022-02-02 20:34:50 +08:00
|
|
|
|
_session->onRecv(std::make_shared<mediakit::WebSocketBuffer>(header._opcode, header._fin, std::move(_payload_cache)));
|
2020-08-08 12:17:06 +08:00
|
|
|
|
_payload_cache.clear();
|
2018-12-20 10:31:31 +08:00
|
|
|
|
break;
|
2020-08-08 12:17:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: break;
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
_payload_section.clear();
|
2018-12-20 10:31:31 +08:00
|
|
|
|
header._mask_flag = flag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-08 12:17:06 +08:00
|
|
|
|
* 发送数据进行websocket协议打包后回调
|
2018-12-20 10:31:31 +08:00
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWebSocketEncodeData(toolkit::Buffer::Ptr buffer) override{
|
2020-11-01 03:41:35 +08:00
|
|
|
|
HttpSessionType::send(std::move(buffer));
|
2018-12-20 10:31:31 +08:00
|
|
|
|
}
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _payload_cache;
|
|
|
|
|
std::string _payload_section;
|
|
|
|
|
std::weak_ptr<toolkit::Server> _weak_server;
|
2022-11-19 09:33:10 +08:00
|
|
|
|
toolkit::Session::Ptr _session;
|
2020-01-15 15:11:42 +08:00
|
|
|
|
Creator _creator;
|
2023-03-11 11:08:14 +08:00
|
|
|
|
toolkit::Ticker _recv_ticker;
|
2018-12-20 10:31:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
template<typename SessionType,typename HttpSessionType = mediakit::HttpSession, mediakit::WebSocketHeader::Type DataType = mediakit::WebSocketHeader::TEXT>
|
|
|
|
|
class WebSocketSession : public WebSocketSessionBase<SessionCreator<SessionType>,HttpSessionType,DataType>{
|
2020-01-15 15:11:42 +08:00
|
|
|
|
public:
|
2022-11-19 09:33:10 +08:00
|
|
|
|
WebSocketSession(const toolkit::Socket::Ptr &pSock) : WebSocketSessionBase<SessionCreator<SessionType>,HttpSessionType,DataType>(pSock){}
|
2023-04-22 21:59:26 +08:00
|
|
|
|
virtual ~WebSocketSession() = default;
|
2020-01-15 15:11:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_WEBSOCKETSESSION_H
|