WebSocketSplitter.h 3.24 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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
#include "Network/Buffer.h"
19

20 21
using namespace std;
using namespace toolkit;
22

xiongziliang committed
23
namespace mediakit {
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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:
47 48 49
    WebSocketHeader() : _mask(4){}
    virtual ~WebSocketHeader(){}
public:
50 51 52 53 54 55 56 57 58 59 60 61 62
    bool _fin;
    uint8_t _reserved;
    Type _opcode;
    bool _mask_flag;
    uint64_t _playload_len;
    vector<uint8_t > _mask;
};

class WebSocketSplitter : public WebSocketHeader{
public:
    WebSocketSplitter(){}
    virtual ~WebSocketSplitter(){}

63 64 65 66 67 68
    /**
     * 输入数据以便解包webSocket数据以及处理粘包问题
     * 可能触发onWebSocketDecodeHeader和onWebSocketDecodePlayload回调
     * @param data 需要解包的数据,可能是不完整的包或多个包
     * @param len 数据长度
     */
69
    void decode(uint8_t *data,uint64_t len);
70 71 72 73

    /**
     * 编码一个数据包
     * 将触发2次onWebSocketEncodeData回调
74
     * @param header 数据头
75
     * @param buffer 负载数据
76
     */
77
    void encode(const WebSocketHeader &header,const Buffer::Ptr &buffer);
78
protected:
79 80
    /**
     * 收到一个webSocket数据包包头,后续将继续触发onWebSocketDecodePlayload回调
81
     * @param header 数据包头
82
     */
83
    virtual void onWebSocketDecodeHeader(const WebSocketHeader &header) {};
84 85 86

    /**
     * 收到webSocket数据包负载
87
     * @param header 数据包包头
88 89
     * @param ptr 负载数据指针
     * @param len 负载数据长度
90
     * @param recved 已接收数据长度(包含本次数据长度),等于header._playload_len时则接受完毕
91
     */
92 93 94 95 96 97 98 99
    virtual void onWebSocketDecodePlayload(const WebSocketHeader &header, const uint8_t *ptr, uint64_t len, uint64_t recved) {};


    /**
     * 接收到完整的一个webSocket数据包后回调
     * @param header 数据包包头
     */
    virtual void onWebSocketDecodeComplete(const WebSocketHeader &header) {};
100 101 102 103 104 105

    /**
     * websocket数据编码回调
     * @param ptr 数据指针
     * @param len 数据指针长度
     */
106
    virtual void onWebSocketEncodeData(const Buffer::Ptr &buffer){};
107 108 109 110 111 112 113 114 115
private:
    void onPlayloadData(uint8_t *data,uint64_t len);
private:
    string _remain_data;
    int _mask_offset = 0;
    bool _got_header = false;
    uint64_t _playload_offset = 0;
};

xiongziliang committed
116
} /* namespace mediakit */
117

118 119

#endif //ZLMEDIAKIT_WEBSOCKETSPLITTER_H