ZLMediaKit/src/Http/WebSocketSplitter.h

149 lines
4.2 KiB
C++
Raw Normal View History

2018-09-25 09:55:41 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-09-25 09:55:41 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-09-25 09:55:41 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2018-09-25 09:55:41 +08:00
*/
2018-09-21 16:11:09 +08:00
#ifndef ZLMEDIAKIT_WEBSOCKETSPLITTER_H
#define ZLMEDIAKIT_WEBSOCKETSPLITTER_H
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
#include "Network/Buffer.h"
2018-09-25 09:55:41 +08:00
//websocket组合包最大不得超过4MB(防止内存爆炸)
#define MAX_WS_PACKET (4 * 1024 * 1024)
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-09-25 09:55:41 +08:00
2018-09-21 16:11:09 +08:00
class WebSocketHeader {
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<WebSocketHeader>;
2018-09-21 16:11:09 +08:00
typedef enum {
CONTINUATION = 0x0,
TEXT = 0x1,
BINARY = 0x2,
RSV3 = 0x3,
RSV4 = 0x4,
RSV5 = 0x5,
RSV6 = 0x6,
RSV7 = 0x7,
CLOSE = 0x8,
PING = 0x9,
PONG = 0xA,
CONTROL_RSVB = 0xB,
CONTROL_RSVC = 0xC,
CONTROL_RSVD = 0xD,
CONTROL_RSVE = 0xE,
CONTROL_RSVF = 0xF
} Type;
2018-09-21 18:48:35 +08:00
public:
2020-05-25 14:36:07 +08:00
WebSocketHeader() : _mask(4){
//获取_mask内部buffer的内存地址该内存是malloc开辟的地址为随机
uint64_t ptr = (uint64_t)(&_mask[0]);
//根据内存地址设置掩码随机数
_mask.assign((uint8_t*)(&ptr), (uint8_t*)(&ptr) + 4);
}
virtual ~WebSocketHeader() = default;
2018-09-21 16:11:09 +08:00
public:
bool _fin;
uint8_t _reserved;
Type _opcode;
bool _mask_flag;
size_t _payload_len;
std::vector<uint8_t > _mask;
2018-09-21 16:11:09 +08:00
};
//websocket协议收到的字符串类型缓存用户协议层获取该数据传输的方式
class WebSocketBuffer : public toolkit::BufferString {
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<WebSocketBuffer>;
template<typename ...ARGS>
WebSocketBuffer(WebSocketHeader::Type headType, bool fin, ARGS &&...args)
: toolkit::BufferString(std::forward<ARGS>(args)...), _fin(fin), _head_type(headType){}
~WebSocketBuffer() override = default;
WebSocketHeader::Type headType() const { return _head_type; }
bool isFinished() const { return _fin; };
private:
bool _fin;
WebSocketHeader::Type _head_type;
};
2018-09-21 16:11:09 +08:00
class WebSocketSplitter : public WebSocketHeader{
public:
WebSocketSplitter() = default;
virtual ~WebSocketSplitter() = default;
2018-09-21 16:11:09 +08:00
2018-09-21 18:48:35 +08:00
/**
* 便webSocket数据以及处理粘包问题
2020-05-25 13:51:00 +08:00
* onWebSocketDecodeHeader和onWebSocketDecodePayload回调
2018-09-21 18:48:35 +08:00
* @param data
* @param len
*/
2021-01-17 20:15:08 +08:00
void decode(uint8_t *data, size_t len);
2018-09-21 18:48:35 +08:00
/**
*
* 2onWebSocketEncodeData回调
* @param header
* @param buffer
2018-09-21 18:48:35 +08:00
*/
void encode(const WebSocketHeader &header,const toolkit::Buffer::Ptr &buffer);
2018-09-21 16:11:09 +08:00
protected:
2018-09-21 18:48:35 +08:00
/**
2020-05-25 13:51:00 +08:00
* webSocket数据包包头onWebSocketDecodePayload回调
* @param header
2018-09-21 18:48:35 +08:00
*/
virtual void onWebSocketDecodeHeader(const WebSocketHeader &header) {};
2018-09-21 18:48:35 +08:00
/**
* webSocket数据包负载
* @param header
2018-09-21 18:48:35 +08:00
* @param ptr
* @param len
2020-05-25 13:51:00 +08:00
* @param recved ()header._payload_len时则接受完毕
2018-09-21 18:48:35 +08:00
*/
2021-01-17 20:15:08 +08:00
virtual void onWebSocketDecodePayload(const WebSocketHeader &header, const uint8_t *ptr, size_t len, size_t recved) {};
/**
* webSocket数据包后回调
* @param header
*/
virtual void onWebSocketDecodeComplete(const WebSocketHeader &header) {};
2018-09-21 18:48:35 +08:00
/**
* websocket数据编码回调
* @param ptr
* @param len
*/
virtual void onWebSocketEncodeData(toolkit::Buffer::Ptr buffer){};
2018-09-21 16:11:09 +08:00
private:
2021-01-17 20:15:08 +08:00
void onPayloadData(uint8_t *data, size_t len);
2018-09-21 16:11:09 +08:00
private:
bool _got_header = false;
2021-01-17 20:15:08 +08:00
int _mask_offset = 0;
size_t _payload_offset = 0;
std::string _remain_data;
2018-09-21 16:11:09 +08:00
};
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2018-09-25 09:55:41 +08:00
2018-09-21 16:11:09 +08:00
#endif //ZLMEDIAKIT_WEBSOCKETSPLITTER_H