From 8779ed42e52d3e540ba492df641cde73d56acc1b Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 21 Sep 2018 18:48:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90webSocket?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZLToolKit | 2 +- src/Http/HttpSession.h | 16 ++++++- src/Http/WebSocketSplitter.cpp | 79 ++++++++++++++++++++++++++++++++-- src/Http/WebSocketSplitter.h | 42 ++++++++++++++++-- 4 files changed, 130 insertions(+), 9 deletions(-) diff --git a/ZLToolKit b/ZLToolKit index 5445ceca..789ede49 160000 --- a/ZLToolKit +++ b/ZLToolKit @@ -1 +1 @@ -Subproject commit 5445ceca186237173c99a717b1ab6a26fb2ea020 +Subproject commit 789ede4907f164fe788ad7b8533eddd49c10869d diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index abcff1e8..0ce29da9 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -144,12 +144,24 @@ public: EchoWebSocketSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : HttpSession(pTh,pSock){}; virtual ~EchoWebSocketSession(){}; protected: - void onWebSocketHeader(const WebSocketHeader &packet) override{ + void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{ DebugL << packet._playload_len; }; - void onWebSocketPlayload(const WebSocketHeader &packet,const uint8_t *ptr,uint64_t len,uint64_t recved) override { + void onWebSocketDecodePlayload(const WebSocketHeader &packet,const uint8_t *ptr,uint64_t len,uint64_t recved) override { DebugL << string((char *)ptr,len) << " " << recved; + + //webSocket服务器不允许对数据进行掩码加密 + bool mask_flag = _mask_flag; + _mask_flag = false; + WebSocketSplitter::encode((uint8_t *)ptr,len); + _mask_flag = true; + }; + + void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{ + send((char *)ptr,len); + }; + }; } /* namespace Http */ diff --git a/src/Http/WebSocketSplitter.cpp b/src/Http/WebSocketSplitter.cpp index 6d3d7753..81896979 100644 --- a/src/Http/WebSocketSplitter.cpp +++ b/src/Http/WebSocketSplitter.cpp @@ -1,8 +1,11 @@ -// +// // Created by xzl on 2018/9/21. // #include "WebSocketSplitter.h" +#include +#include +#include #include "Util/logger.h" #include "Util/util.h" using namespace ZL::Util; @@ -84,7 +87,7 @@ begin_decode: _got_header = true; _mask_offset = 0; _playload_offset = 0; - onWebSocketHeader(*this); + onWebSocketDecodeHeader(*this); } uint64_t remain = len - (ptr - data); @@ -121,5 +124,75 @@ void WebSocketSplitter::onPlayloadData(uint8_t *ptr, uint64_t len) { } _mask_offset = (_mask_offset + len) % 4; } - onWebSocketPlayload(*this,_mask_flag ? ptr - len : ptr,len,_playload_offset); + onWebSocketDecodePlayload(*this, _mask_flag ? ptr - len : ptr, len, _playload_offset); } + +void WebSocketSplitter::encode(uint8_t *data, uint64_t len) { + string ret; + + uint8_t byte = _fin << 7 | ((_reserved & 0x07) << 4) | (_opcode & 0x0F) ; + ret.push_back(byte); + + _mask_flag = (_mask_flag && _mask.size() >= 4); + byte = _mask_flag << 7; + + _playload_len = len; + if(_playload_len < 126){ + byte |= _playload_len; + ret.push_back(byte); + }else if(_playload_len <= 0xFFFF){ + byte |= 126; + ret.push_back(byte); + + uint16_t len = htons(_playload_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); + ret.append((char *)&len_high,4); + ret.append((char *)&len_low,4); + } + if(_mask_flag){ + ret.append((char *)_mask.data(),4); + } + + onWebSocketEncodeData((uint8_t*)ret.data(),ret.size()); + + if(_mask_flag){ + uint8_t *ptr = data; + for(int i = 0; i < len ; ++i,++ptr){ + *(ptr) ^= _mask[i % 4]; + } + } + onWebSocketEncodeData(data,len); + +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Http/WebSocketSplitter.h b/src/Http/WebSocketSplitter.h index 84f026fb..6b9af2fe 100644 --- a/src/Http/WebSocketSplitter.h +++ b/src/Http/WebSocketSplitter.h @@ -1,4 +1,4 @@ -// +// // Created by xzl on 2018/9/21. // @@ -32,6 +32,9 @@ public: CONTROL_RSVE = 0xE, CONTROL_RSVF = 0xF } Type; +public: + WebSocketHeader() : _mask(4){} + virtual ~WebSocketHeader(){} public: bool _fin; uint8_t _reserved; @@ -46,11 +49,44 @@ public: WebSocketSplitter(){} virtual ~WebSocketSplitter(){} + /** + * 输入数据以便解包webSocket数据以及处理粘包问题 + * 可能触发onWebSocketDecodeHeader和onWebSocketDecodePlayload回调 + * @param data 需要解包的数据,可能是不完整的包或多个包 + * @param len 数据长度 + */ void decode(uint8_t *data,uint64_t len); + + /** + * 编码一个数据包 + * 将触发2次onWebSocketEncodeData回调 + * 第一次是数据头,第二次是负载数据 + * @param data 负载数据 + * @param len 负载数据长度 + */ void encode(uint8_t *data,uint64_t len); protected: - virtual void onWebSocketHeader(const WebSocketHeader &packet) {}; - virtual void onWebSocketPlayload(const WebSocketHeader &packet,const uint8_t *ptr,uint64_t len,uint64_t recved) {}; + /** + * 收到一个webSocket数据包包头,后续将继续触发onWebSocketDecodePlayload回调 + * @param packet 数据包头 + */ + virtual void onWebSocketDecodeHeader(const WebSocketHeader &packet) {}; + + /** + * 收到webSocket数据包负载 + * @param packet 数据包包头 + * @param ptr 负载数据指针 + * @param len 负载数据长度 + * @param recved 已接收数据长度,等于packet._playload_len时则接受完毕 + */ + virtual void onWebSocketDecodePlayload(const WebSocketHeader &packet, const uint8_t *ptr, uint64_t len, uint64_t recved) {}; + + /** + * websocket数据编码回调 + * @param ptr 数据指针 + * @param len 数据指针长度 + */ + virtual void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len){}; private: void onPlayloadData(uint8_t *data,uint64_t len); private: