支持加密型websocket

支持更多websocket命令
This commit is contained in:
xiongziliang 2018-09-27 16:03:53 +08:00
parent 6b0e788319
commit b1951cc3c9
4 changed files with 234 additions and 231 deletions

View File

@ -128,212 +128,6 @@ private:
};
/**
* WebSocket协议
* WebSock协议下的具体业务协议WebSocket协议的Rtmp协议等
* @tparam SessionType TcpSession类
*/
template <typename SessionType>
class WebSocketSession : public HttpSession {
public:
WebSocketSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) : HttpSession(pTh,pSock){}
virtual ~WebSocketSession(){}
//收到eof或其他导致脱离TcpServer事件的回调
void onError(const SockException &err) override{
HttpSession::onError(err);
if(_session){
_session->onError(err);
}
}
//每隔一段时间触发,用来做超时管理
void onManager() override{
HttpSession::onManager();
if(_session){
_session->onManager();
}
}
void attachServer(const TcpServer &server) override{
HttpSession::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){
return;
}
//这是个WebSocket会话而不是普通的Http会话
_firstPacket = false;
_session = std::make_shared<SessionImp>(getIdentifier(),nullptr,_sock);
auto strongServer = _weakServer.lock();
if(strongServer){
_session->attachServer(*strongServer);
}
//此处截取数据并进行websocket协议打包
weak_ptr<WebSocketSession> weakSelf = dynamic_pointer_cast<WebSocketSession>(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((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 {
if(packet._playload_len == recved){
//收到完整的包
if(_remian_data.empty()){
onRecvWholePacket((char *)ptr,len);
}else{
_remian_data.append((char *)ptr,len);
onRecvWholePacket(_remian_data);
_remian_data.clear();
}
} else {
//部分数据
_remian_data.append((char *)ptr,len);
}
}
/**
* websocket协议打包后回调
* @param ptr
* @param len
*/
void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{
_session->realSend(_session->obtainBuffer((char *)ptr,len));
}
/**
* websock数据包
* @param data
* @param len
*/
void onRecvWholePacket(const char *data,uint64_t len){
BufferRaw::Ptr buffer = _session->obtainBuffer(data,len);
_session->onRecv(buffer);
}
/**
* websock数据包
* @param str
*/
void onRecvWholePacket(const string &str){
BufferString::Ptr buffer = std::make_shared<BufferString>(str);
_session->onRecv(buffer);
}
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(){}
/**
* webSocket协议打包
* socket
* @param buf
* @return
*/
int realSend(const Buffer::Ptr &buf){
return SessionType::send(buf);
}
/**
*
* @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;
}
};
typedef WebSocketSession<EchoSession> EchoWebSocketSession;
} /* namespace Http */
} /* namespace ZL */

View File

@ -70,7 +70,7 @@ public:
HttpSession::onRecv(data,len);
}
#endif//defined(__GNUC__) && (__GNUC__ < 5)
private:
protected:
virtual int send(const Buffer::Ptr &buf) override{
TimeTicker();
m_sslBox.onSend(buf->data(), buf->size());
@ -79,6 +79,201 @@ private:
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{
HttpSession::onError(err);
if(_session){
_session->onError(err);
}
}
//每隔一段时间触发,用来做超时管理
void onManager() override{
HttpSession::onManager();
if(_session){
_session->onManager();
}
}
void attachServer(const TcpServer &server) override{
HttpSession::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;
}
};
typedef WebSocketSession<EchoSession> EchoWebSocketSession;
} /* namespace Http */
} /* namespace ZL */

View File

@ -115,6 +115,9 @@ begin_decode:
_mask_offset = 0;
_playload_offset = 0;
onWebSocketDecodeHeader(*this);
if(_playload_len == 0){
onWebSocketDecodeComplete(*this);
}
}
//进入后面逻辑代表已经获取到了webSocket协议头
@ -129,6 +132,8 @@ begin_decode:
onPlayloadData(ptr,playload_slice_len);
if(_playload_offset == _playload_len){
onWebSocketDecodeComplete(*this);
//这是下一个包
remain -= playload_slice_len;
ptr += playload_slice_len;
@ -157,47 +162,48 @@ void WebSocketSplitter::onPlayloadData(uint8_t *ptr, uint64_t len) {
onWebSocketDecodePlayload(*this, _mask_flag ? ptr - len : ptr, len, _playload_offset);
}
void WebSocketSplitter::encode(uint8_t *data, uint64_t len) {
void WebSocketSplitter::encode(const WebSocketHeader &header,uint8_t *data, uint64_t len) {
string ret;
uint8_t byte = _fin << 7 | ((_reserved & 0x07) << 4) | (_opcode & 0x0F) ;
uint8_t byte = header._fin << 7 | ((header._reserved & 0x07) << 4) | (header._opcode & 0x0F) ;
ret.push_back(byte);
_mask_flag = (_mask_flag && _mask.size() >= 4);
byte = _mask_flag << 7;
auto mask_flag = (header._mask_flag && header._mask.size() >= 4);
byte = mask_flag << 7;
_playload_len = len;
if(_playload_len < 126){
byte |= _playload_len;
if(len < 126){
byte |= len;
ret.push_back(byte);
}else if(_playload_len <= 0xFFFF){
}else if(len <= 0xFFFF){
byte |= 126;
ret.push_back(byte);
uint16_t len = htons(_playload_len);
uint16_t len = htons(len);
ret.append((char *)&len,2);
}else{
byte |= 127;
ret.push_back(byte);
uint32_t len_high = htonl(_playload_len >> 32) ;
uint32_t len_low = htonl(_playload_len & 0xFFFFFFFF);
uint32_t len_high = htonl(len >> 32) ;
uint32_t len_low = htonl(len & 0xFFFFFFFF);
ret.append((char *)&len_high,4);
ret.append((char *)&len_low,4);
}
if(_mask_flag){
ret.append((char *)_mask.data(),4);
if(mask_flag){
ret.append((char *)header._mask.data(),4);
}
onWebSocketEncodeData((uint8_t*)ret.data(),ret.size());
if(_mask_flag){
if(len > 0){
if(mask_flag){
uint8_t *ptr = data;
for(int i = 0; i < len ; ++i,++ptr){
*(ptr) ^= _mask[i % 4];
*(ptr) ^= header._mask[i % 4];
}
}
onWebSocketEncodeData(data,len);
}
}

View File

@ -87,25 +87,33 @@ public:
*
* 2onWebSocketEncodeData回调
*
* @param header
* @param data
* @param len
*/
void encode(uint8_t *data,uint64_t len);
void encode(const WebSocketHeader &header,uint8_t *data,uint64_t len);
protected:
/**
* webSocket数据包包头onWebSocketDecodePlayload回调
* @param packet
* @param header
*/
virtual void onWebSocketDecodeHeader(const WebSocketHeader &packet) {};
virtual void onWebSocketDecodeHeader(const WebSocketHeader &header) {};
/**
* webSocket数据包负载
* @param packet
* @param header
* @param ptr
* @param len
* @param recved ()packet._playload_len时则接受完毕
* @param recved ()header._playload_len时则接受完毕
*/
virtual void onWebSocketDecodePlayload(const WebSocketHeader &packet, const uint8_t *ptr, uint64_t len, uint64_t recved) {};
virtual void onWebSocketDecodePlayload(const WebSocketHeader &header, const uint8_t *ptr, uint64_t len, uint64_t recved) {};
/**
* webSocket数据包后回调
* @param header
*/
virtual void onWebSocketDecodeComplete(const WebSocketHeader &header) {};
/**
* websocket数据编码回调