WebSocketSplitter.h 4.17 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * 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.
9
 */
10 11 12 13 14 15 16 17

#ifndef ZLMEDIAKIT_WEBSOCKETSPLITTER_H
#define ZLMEDIAKIT_WEBSOCKETSPLITTER_H

#include <cstdint>
#include <string>
#include <vector>
#include <memory>
18 19 20
#include "Network/Buffer.h"
using namespace std;
using namespace toolkit;
21

22 23 24
//websocket组合包最大不得超过4MB(防止内存爆炸)
#define MAX_WS_PACKET (4 * 1024 * 1024)

xiongziliang committed
25
namespace mediakit {
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class WebSocketHeader {
public:
    typedef std::shared_ptr<WebSocketHeader> Ptr;
    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;
public:
49

50 51 52 53 54 55
    WebSocketHeader() : _mask(4){
        //获取_mask内部buffer的内存地址,该内存是malloc开辟的,地址为随机
        uint64_t ptr = (uint64_t)(&_mask[0]);
        //根据内存地址设置掩码随机数
        _mask.assign((uint8_t*)(&ptr), (uint8_t*)(&ptr) + 4);
    }
56
    virtual ~WebSocketHeader(){}
57

58
public:
59 60 61 62
    bool _fin;
    uint8_t _reserved;
    Type _opcode;
    bool _mask_flag;
63
    size_t _payload_len;
64 65 66
    vector<uint8_t > _mask;
};

67 68 69 70 71 72 73
//websocket协议收到的字符串类型缓存,用户协议层获取该数据传输的方式
class WebSocketBuffer : public BufferString {
public:
    typedef std::shared_ptr<WebSocketBuffer> Ptr;

    template<typename ...ARGS>
    WebSocketBuffer(WebSocketHeader::Type headType, bool fin, ARGS &&...args)
74
            :  BufferString(std::forward<ARGS>(args)...), _fin(fin), _head_type(headType){}
75 76 77 78 79 80 81 82 83

    ~WebSocketBuffer() override {}

    WebSocketHeader::Type headType() const { return _head_type; }

    bool isFinished() const { return _fin; };

private:
    bool _fin;
84
    WebSocketHeader::Type _head_type;
85 86
};

87 88 89 90 91
class WebSocketSplitter : public WebSocketHeader{
public:
    WebSocketSplitter(){}
    virtual ~WebSocketSplitter(){}

92 93
    /**
     * 输入数据以便解包webSocket数据以及处理粘包问题
xiongziliang committed
94
     * 可能触发onWebSocketDecodeHeader和onWebSocketDecodePayload回调
95 96 97
     * @param data 需要解包的数据,可能是不完整的包或多个包
     * @param len 数据长度
     */
xiongziliang committed
98
    void decode(uint8_t *data, size_t len);
99 100 101 102

    /**
     * 编码一个数据包
     * 将触发2次onWebSocketEncodeData回调
103
     * @param header 数据头
104
     * @param buffer 负载数据
105
     */
106
    void encode(const WebSocketHeader &header,const Buffer::Ptr &buffer);
107

108
protected:
109
    /**
xiongziliang committed
110
     * 收到一个webSocket数据包包头,后续将继续触发onWebSocketDecodePayload回调
111
     * @param header 数据包头
112
     */
113
    virtual void onWebSocketDecodeHeader(const WebSocketHeader &header) {};
114 115 116

    /**
     * 收到webSocket数据包负载
117
     * @param header 数据包包头
118 119
     * @param ptr 负载数据指针
     * @param len 负载数据长度
xiongziliang committed
120
     * @param recved 已接收数据长度(包含本次数据长度),等于header._payload_len时则接受完毕
121
     */
xiongziliang committed
122
    virtual void onWebSocketDecodePayload(const WebSocketHeader &header, const uint8_t *ptr, size_t len, size_t recved) {};
123 124 125 126 127 128

    /**
     * 接收到完整的一个webSocket数据包后回调
     * @param header 数据包包头
     */
    virtual void onWebSocketDecodeComplete(const WebSocketHeader &header) {};
129 130 131 132 133 134

    /**
     * websocket数据编码回调
     * @param ptr 数据指针
     * @param len 数据指针长度
     */
135
    virtual void onWebSocketEncodeData(Buffer::Ptr buffer){};
136

137
private:
xiongziliang committed
138
    void onPayloadData(uint8_t *data, size_t len);
139

140 141
private:
    bool _got_header = false;
xiongziliang committed
142 143 144
    int _mask_offset = 0;
    size_t _payload_offset = 0;
    string _remain_data;
145 146
};

xiongziliang committed
147
} /* namespace mediakit */
148

149 150

#endif //ZLMEDIAKIT_WEBSOCKETSPLITTER_H