ZLMediaKit/src/Http/HttpsSession.h

283 lines
8.0 KiB
C
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-19 17:47:07 +08:00
*
2017-09-27 16:20:30 +08:00
* Copyright (c) 2016 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.
2017-04-19 17:47:07 +08:00
*/
#ifndef SRC_HTTP_HTTPSSESSION_H_
#define SRC_HTTP_HTTPSSESSION_H_
#include "HttpSession.h"
#include "Util/SSLBox.h"
#include "Util/TimeTicker.h"
2017-04-25 11:35:41 +08:00
2017-04-19 17:47:07 +08:00
using namespace ZL::Util;
namespace ZL {
namespace Http {
class HttpsSession: public HttpSession {
public:
HttpsSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock):
HttpSession(pTh,pSock){
m_sslBox.setOnEncData([&](const char *data, uint32_t len){
2017-06-09 17:45:13 +08:00
#if defined(__GNUC__) && (__GNUC__ < 5)
2017-05-25 17:59:40 +08:00
public_send(data,len);
2017-06-09 17:45:13 +08:00
#else//defined(__GNUC__) && (__GNUC__ < 5)
HttpSession::send(obtainBuffer(data,len));
2017-06-09 17:45:13 +08:00
#endif//defined(__GNUC__) && (__GNUC__ < 5)
2017-04-19 17:47:07 +08:00
});
m_sslBox.setOnDecData([&](const char *data, uint32_t len){
2017-06-09 17:45:13 +08:00
#if defined(__GNUC__) && (__GNUC__ < 5)
2017-05-25 17:59:40 +08:00
public_onRecv(data,len);
2017-06-09 17:45:13 +08:00
#else//defined(__GNUC__) && (__GNUC__ < 5)
2017-04-19 17:47:07 +08:00
HttpSession::onRecv(data,len);
2017-06-09 17:45:13 +08:00
#endif//defined(__GNUC__) && (__GNUC__ < 5)
2017-04-19 17:47:07 +08:00
});
}
virtual ~HttpsSession(){
//m_sslBox.shutdown();
}
2018-02-23 15:36:51 +08:00
void onRecv(const Buffer::Ptr &pBuf) override{
2017-04-19 17:47:07 +08:00
TimeTicker();
m_sslBox.onRecv(pBuf->data(), pBuf->size());
}
2017-06-09 17:45:13 +08:00
#if defined(__GNUC__) && (__GNUC__ < 5)
2017-05-25 17:59:40 +08:00
int public_send(const char *data, uint32_t len){
return HttpSession::send(obtainBuffer(data,len));
2017-05-25 17:59:40 +08:00
}
void public_onRecv(const char *data, uint32_t len){
HttpSession::onRecv(data,len);
}
2017-06-09 17:45:13 +08:00
#endif//defined(__GNUC__) && (__GNUC__ < 5)
protected:
virtual int send(const Buffer::Ptr &buf) override{
2017-04-19 17:47:07 +08:00
TimeTicker();
m_sslBox.onSend(buf->data(), buf->size());
return buf->size();
2017-04-19 17:47:07 +08:00
}
SSL_Box m_sslBox;
};
/**
* WebSocket协议
* WebSock协议下的具体业务协议WebSocket协议的Rtmp协议等
* @tparam SessionType TcpSession类
*/
template <class SessionType,class HttpSessionType = HttpSession>
class WebSocketSession : public HttpSessionType {
public:
WebSocketSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) : HttpSessionType(pTh,pSock){}
virtual ~WebSocketSession(){}
//收到eof或其他导致脱离TcpServer事件的回调
void onError(const SockException &err) override{
2018-09-27 16:08:24 +08:00
HttpSessionType::onError(err);
if(_session){
_session->onError(err);
}
}
//每隔一段时间触发,用来做超时管理
void onManager() override{
2018-09-27 16:08:24 +08:00
HttpSessionType::onManager();
if(_session){
_session->onManager();
}
}
void attachServer(const TcpServer &server) override{
2018-09-27 16:08:24 +08:00
HttpSessionType::attachServer(server);
_weakServer = const_cast<TcpServer &>(server).shared_from_this();
}
protected:
/**
* webSocket数据包
* @param packet
*/
void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{
//新包,原来的包残余数据清空掉
_remian_data.clear();
if(_firstPacket){
//这是个WebSocket会话而不是普通的Http会话
_firstPacket = false;
_session = std::make_shared<SessionImp>(HttpSessionType::getIdentifier(),nullptr,HttpSessionType::_sock);
auto strongServer = _weakServer.lock();
if(strongServer){
_session->attachServer(*strongServer);
}
//此处截取数据并进行websocket协议打包
weak_ptr<WebSocketSession> weakSelf = dynamic_pointer_cast<WebSocketSession>(HttpSessionType::shared_from_this());
_session->setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){
auto strongSelf = weakSelf.lock();
if(strongSelf){
bool mask_flag = strongSelf->_mask_flag;
strongSelf->_mask_flag = false;
strongSelf->WebSocketSplitter::encode(*strongSelf,(uint8_t *)buf->data(),buf->size());
strongSelf->_mask_flag = mask_flag;
}
return buf->size();
});
}
}
/**
* websocket数据包负载
* @param packet
* @param ptr
* @param len
* @param recved
*/
void onWebSocketDecodePlayload(const WebSocketHeader &packet,const uint8_t *ptr,uint64_t len,uint64_t recved) override {
_remian_data.append((char *)ptr,len);
}
/**
* webSocket数据包后回调
* @param header
*/
void onWebSocketDecodeComplete(const WebSocketHeader &header) override {
switch (header._opcode){
case WebSocketHeader::CLOSE:{
HttpSessionType::encode(header,nullptr,0);
}
break;
case WebSocketHeader::PING:{
const_cast<WebSocketHeader&>(header)._opcode = WebSocketHeader::PONG;
HttpSessionType::encode(header,(uint8_t *)_remian_data.data(),_remian_data.size());
}
break;
case WebSocketHeader::CONTINUATION:{
}
break;
case WebSocketHeader::TEXT:
case WebSocketHeader::BINARY:{
BufferString::Ptr buffer = std::make_shared<BufferString>(_remian_data);
_session->onRecv(buffer);
}
break;
default:
break;
}
_remian_data.clear();
}
/**
* websocket协议打包后回调
* @param ptr
* @param len
*/
void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{
SocketHelper::send((char *)ptr,len);
}
private:
typedef function<int(const Buffer::Ptr &buf)> onBeforeSendCB;
/**
* TcpSession派生类发送数据的截取
* websocket协议的打包
*/
class SessionImp : public SessionType{
public:
SessionImp(const string &identifier,
const std::shared_ptr<ThreadPool> &pTh,
const Socket::Ptr &pSock) :
_identifier(identifier),SessionType(pTh,pSock){}
~SessionImp(){}
/**
*
* @param cb
*/
void setOnBeforeSendCB(const onBeforeSendCB &cb){
_beforeSendCB = cb;
}
protected:
/**
* send函数截取数据
* @param buf
* @return
*/
int send(const Buffer::Ptr &buf) override {
if(_beforeSendCB){
return _beforeSendCB(buf);
}
return SessionType::send(buf);
}
string getIdentifier() const override{
return _identifier;
}
private:
onBeforeSendCB _beforeSendCB;
string _identifier;
};
private:
bool _firstPacket = true;
string _remian_data;
weak_ptr<TcpServer> _weakServer;
std::shared_ptr<SessionImp> _session;
};
/**
*
*/
class EchoSession : public TcpSession {
public:
EchoSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) :
TcpSession(pTh,pSock){
DebugL;
}
virtual ~EchoSession(){
DebugL;
}
void attachServer(const TcpServer &server) override{
DebugL << getIdentifier() << " " << TcpSession::getIdentifier();
}
void onRecv(const Buffer::Ptr &buffer) override {
send(buffer);
}
void onError(const SockException &err) override{
WarnL << err.what();
}
//每隔一段时间触发,用来做超时管理
void onManager() override{
DebugL;
}
};
2018-09-27 16:08:24 +08:00
typedef WebSocketSession<EchoSession,HttpSession> EchoWebSocketSession;
typedef WebSocketSession<EchoSession,HttpsSession> SSLEchoWebSocketSession;
2017-04-19 17:47:07 +08:00
} /* namespace Http */
} /* namespace ZL */
#endif /* SRC_HTTP_HTTPSSESSION_H_ */