ZLMediaKit/src/Http/WebSocketSplitter.h

136 lines
4.1 KiB
C
Raw Normal View History

2018-09-25 09:55:41 +08:00
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2018-09-21 16:11:09 +08:00
#ifndef ZLMEDIAKIT_WEBSOCKETSPLITTER_H
#define ZLMEDIAKIT_WEBSOCKETSPLITTER_H
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
using namespace std;
2018-09-25 09:55:41 +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
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;
2018-09-21 18:48:35 +08:00
public:
WebSocketHeader() : _mask(4){}
virtual ~WebSocketHeader(){}
2018-09-21 16:11:09 +08:00
public:
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(){}
2018-09-21 18:48:35 +08:00
/**
* 便webSocket数据以及处理粘包问题
* onWebSocketDecodeHeader和onWebSocketDecodePlayload回调
* @param data
* @param len
*/
2018-09-21 16:11:09 +08:00
void decode(uint8_t *data,uint64_t len);
2018-09-21 18:48:35 +08:00
/**
*
* 2onWebSocketEncodeData回调
*
* @param header
2018-09-21 18:48:35 +08:00
* @param data
* @param len
*/
2018-09-29 14:54:49 +08:00
void encode(const WebSocketHeader &header,uint8_t *data,const uint64_t len);
2018-09-21 16:11:09 +08:00
protected:
2018-09-21 18:48:35 +08:00
/**
* webSocket数据包包头onWebSocketDecodePlayload回调
* @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
* @param recved ()header._playload_len时则接受完毕
2018-09-21 18:48:35 +08:00
*/
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) {};
2018-09-21 18:48:35 +08:00
/**
* websocket数据编码回调
* @param ptr
* @param len
*/
virtual void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len){};
2018-09-21 16:11:09 +08:00
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;
};
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