基本完成webSocket服务器功能

This commit is contained in:
xiongziliang 2018-09-21 18:48:35 +08:00
parent cfdc3653e2
commit 8779ed42e5
4 changed files with 130 additions and 9 deletions

@ -1 +1 @@
Subproject commit 5445ceca186237173c99a717b1ab6a26fb2ea020
Subproject commit 789ede4907f164fe788ad7b8533eddd49c10869d

View File

@ -144,12 +144,24 @@ public:
EchoWebSocketSession(const std::shared_ptr<ThreadPool> &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 */

View File

@ -1,8 +1,11 @@
//
//
// Created by xzl on 2018/9/21.
//
#include "WebSocketSplitter.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#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);
}

View File

@ -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);
/**
*
* 2onWebSocketEncodeData回调
*
* @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: