diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7d271f8d..91d94bf7 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -99,8 +99,7 @@ get_mime_type(const char* name) { } -HttpSession::HttpSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : - TcpSession(pTh, pSock) { +HttpSession::HttpSession(const Socket::Ptr &pSock) : TcpSession(pSock) { //设置10秒发送缓存 pSock->setSendBufSecond(10); diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 6a3ddfd0..db232f92 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -51,7 +51,7 @@ public: const KeyValue &headerOut, const string &contentOut)> HttpResponseInvoker; - HttpSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock); + HttpSession(const Socket::Ptr &pSock); virtual ~HttpSession(); virtual void onRecv(const Buffer::Ptr &) override; diff --git a/src/Http/HttpsSession.h b/src/Http/HttpsSession.h deleted file mode 100644 index b05db6bc..00000000 --- a/src/Http/HttpsSession.h +++ /dev/null @@ -1,280 +0,0 @@ -/* - * MIT License - * - * 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. - */ - -#ifndef SRC_HTTP_HTTPSSESSION_H_ -#define SRC_HTTP_HTTPSSESSION_H_ - -#include "HttpSession.h" -#include "Util/SSLBox.h" -#include "Util/TimeTicker.h" - -using namespace toolkit; - -namespace mediakit { - -template -class TcpSessionWithSSL: public TcpSessionType { -public: - TcpSessionWithSSL(const std::shared_ptr &pTh, const Socket::Ptr &pSock): - TcpSessionType(pTh,pSock){ - _sslBox.setOnEncData([&](const char *data, uint32_t len){ - public_send(data,len); - }); - _sslBox.setOnDecData([&](const char *data, uint32_t len){ - public_onRecv(data,len); - }); - } - virtual ~TcpSessionWithSSL(){ - //_sslBox.shutdown(); - } - void onRecv(const Buffer::Ptr &pBuf) override{ - _sslBox.onRecv(pBuf->data(), pBuf->size()); - } - - int public_send(const char *data, uint32_t len){ - return TcpSessionType::send(TcpSessionType::obtainBuffer(data,len)); - } - void public_onRecv(const char *data, uint32_t len){ - TcpSessionType::onRecv(TcpSessionType::obtainBuffer(data,len)); - } -protected: - virtual int send(const Buffer::Ptr &buf) override{ - _sslBox.onSend(buf->data(), buf->size()); - return buf->size(); - } -private: - SSL_Box _sslBox; -}; - -typedef TcpSessionWithSSL HttpsSession; - - -/** -* 通过该模板类可以透明化WebSocket协议, -* 用户只要实现WebSock协议下的具体业务协议,譬如基于WebSocket协议的Rtmp协议等 -* @tparam SessionType 业务协议的TcpSession类 -*/ -template -class WebSocketSession : public HttpSessionType { -public: - WebSocketSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : HttpSessionType(pTh,pSock){} - virtual ~WebSocketSession(){} - - //收到eof或其他导致脱离TcpServer事件的回调 - void onError(const SockException &err) override{ - HttpSessionType::onError(err); - if(_session){ - _session->onError(err); - } - } - //每隔一段时间触发,用来做超时管理 - void onManager() override{ - if(_session){ - _session->onManager(); - }else{ - HttpSessionType::onManager(); - } - } - - void attachServer(const TcpServer &server) override{ - HttpSessionType::attachServer(server); - _weakServer = const_cast(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(HttpSessionType::getIdentifier(),nullptr,HttpSessionType::_sock); - - auto strongServer = _weakServer.lock(); - if(strongServer){ - _session->attachServer(*strongServer); - } - - //此处截取数据并进行websocket协议打包 - weak_ptr weakSelf = dynamic_pointer_cast(HttpSessionType::shared_from_this()); - _session->setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){ - auto strongSelf = weakSelf.lock(); - if(strongSelf){ - WebSocketHeader header; - header._fin = true; - header._reserved = 0; - header._opcode = WebSocketHeader::TEXT; - header._mask_flag = false; - strongSelf->WebSocketSplitter::encode(header,(uint8_t *)buf->data(),buf->size()); - } - 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_in) override { - WebSocketHeader& header = const_cast(header_in); - auto flag = header._mask_flag; - header._mask_flag = false; - - switch (header._opcode){ - case WebSocketHeader::CLOSE:{ - HttpSessionType::encode(header,nullptr,0); - } - break; - case WebSocketHeader::PING:{ - const_cast(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(_remian_data); - _session->onRecv(buffer); - } - break; - default: - break; - } - _remian_data.clear(); - header._mask_flag = flag; - } - - /** - * 发送数据进行websocket协议打包后回调 - * @param ptr - * @param len - */ - void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{ - SocketHelper::send((char *)ptr,len); - } -private: - typedef function onBeforeSendCB; - /** - * 该类实现了TcpSession派生类发送数据的截取 - * 目的是发送业务数据前进行websocket协议的打包 - */ - class SessionImp : public SessionType{ - public: - SessionImp(const string &identifier, - const std::shared_ptr &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 _weakServer; - std::shared_ptr _session; -}; - -/** -* 回显会话 -*/ -class EchoSession : public TcpSession { -public: - EchoSession(const std::shared_ptr &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; - } -}; - - -typedef WebSocketSession EchoWebSocketSession; -typedef WebSocketSession SSLEchoWebSocketSession; - - -} /* namespace mediakit */ - -#endif /* SRC_HTTP_HTTPSSESSION_H_ */ diff --git a/src/Http/WebSocketSession.h b/src/Http/WebSocketSession.h index cf57fbd7..e349c93e 100644 --- a/src/Http/WebSocketSession.h +++ b/src/Http/WebSocketSession.h @@ -37,7 +37,7 @@ template class WebSocketSession : public HttpSessionType { public: - WebSocketSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : HttpSessionType(pTh,pSock){} + WebSocketSession(const Socket::Ptr &pSock) : HttpSessionType(pSock){} virtual ~WebSocketSession(){} //收到eof或其他导致脱离TcpServer事件的回调 @@ -72,7 +72,7 @@ protected: if(_firstPacket){ //这是个WebSocket会话而不是普通的Http会话 _firstPacket = false; - _session = std::make_shared(HttpSessionType::getIdentifier(),nullptr,HttpSessionType::_sock); + _session = std::make_shared(HttpSessionType::getIdentifier(),HttpSessionType::_sock); auto strongServer = _weakServer.lock(); if(strongServer){ @@ -159,10 +159,8 @@ private: */ class SessionImp : public SessionType{ public: - SessionImp(const string &identifier, - const std::shared_ptr &pTh, - const Socket::Ptr &pSock) : - _identifier(identifier),SessionType(pTh,pSock){} + SessionImp(const string &identifier,const Socket::Ptr &pSock) : + _identifier(identifier),SessionType(pSock){} ~SessionImp(){} @@ -204,8 +202,7 @@ private: */ class EchoSession : public TcpSession { public: - EchoSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : - TcpSession(pTh,pSock){ + EchoSession(const Socket::Ptr &pSock) : TcpSession(pSock){ DebugL; } virtual ~EchoSession(){ diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index fdb34c7b..f5d8277f 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -33,8 +33,7 @@ namespace mediakit { static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE; -RtmpSession::RtmpSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : - TcpSession(pTh, pSock) { +RtmpSession::RtmpSession(const Socket::Ptr &pSock) : TcpSession(pSock) { DebugL << get_peer_ip(); //设置10秒发送缓存 pSock->setSendBufSecond(10); diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 5091d300..542e564d 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -44,7 +44,7 @@ namespace mediakit { class RtmpSession: public TcpSession ,public RtmpProtocol , public MediaSourceEvent{ public: typedef std::shared_ptr Ptr; - RtmpSession(const std::shared_ptr &_th, const Socket::Ptr &_sock); + RtmpSession(const Socket::Ptr &_sock); virtual ~RtmpSession(); void onRecv(const Buffer::Ptr &pBuf) override; void onError(const SockException &err) override; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index f052b697..4f434819 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -72,7 +72,7 @@ static recursive_mutex g_mtxGetter; static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE; -RtspSession::RtspSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : TcpSession(pTh, pSock) { +RtspSession::RtspSession(const Socket::Ptr &pSock) : TcpSession(pSock) { //设置10秒发送缓存 pSock->setSendBufSecond(10); //设置15秒发送超时时间 diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 2cc73f48..16daae81 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -74,7 +74,7 @@ public: //在请求明文密码时如果提供md5密码者则会导致认证失败 typedef std::function onAuth; - RtspSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock); + RtspSession(const Socket::Ptr &pSock); virtual ~RtspSession(); void onRecv(const Buffer::Ptr &pBuf) override; void onError(const SockException &err) override; diff --git a/src/Shell/ShellSession.cpp b/src/Shell/ShellSession.cpp index 110e03f9..41bafdef 100644 --- a/src/Shell/ShellSession.cpp +++ b/src/Shell/ShellSession.cpp @@ -33,9 +33,7 @@ using namespace toolkit; namespace mediakit { -ShellSession::ShellSession(const std::shared_ptr &_th, - const Socket::Ptr &_sock) : - TcpSession(_th, _sock) { +ShellSession::ShellSession(const Socket::Ptr &_sock) : TcpSession(_sock) { pleaseInputUser(); } diff --git a/src/Shell/ShellSession.h b/src/Shell/ShellSession.h index ac5859c4..a76340c7 100644 --- a/src/Shell/ShellSession.h +++ b/src/Shell/ShellSession.h @@ -37,7 +37,7 @@ namespace mediakit { class ShellSession: public TcpSession { public: - ShellSession(const std::shared_ptr &_th, const Socket::Ptr &_sock); + ShellSession(const Socket::Ptr &_sock); virtual ~ShellSession(); void onRecv(const Buffer::Ptr &) override;