mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-27 04:20:13 +08:00
基本完成webSocket服务器功能
This commit is contained in:
parent
cfdc3653e2
commit
8779ed42e5
@ -1 +1 @@
|
||||
Subproject commit 5445ceca186237173c99a717b1ab6a26fb2ea020
|
||||
Subproject commit 789ede4907f164fe788ad7b8533eddd49c10869d
|
@ -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 */
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user