2017-11-20 00:06:45 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <utils/Logger.h>
|
2019-07-12 16:54:26 +02:00
|
|
|
#include "WebSocketUtils.h"
|
2017-11-20 00:06:45 +01:00
|
|
|
|
|
|
|
class QTcpSocket;
|
2019-07-12 16:54:26 +02:00
|
|
|
|
|
|
|
class QtHttpRequest;
|
|
|
|
class Hyperion;
|
2018-12-27 23:11:32 +01:00
|
|
|
class JsonAPI;
|
2017-11-20 00:06:45 +01:00
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
class WebSocketClient : public QObject
|
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2020-08-08 13:09:15 +02:00
|
|
|
WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, bool localConnection, QObject* parent);
|
2017-11-20 00:06:45 +01:00
|
|
|
|
|
|
|
struct WebSocketHeader
|
|
|
|
{
|
|
|
|
bool fin;
|
2019-07-21 15:03:50 +02:00
|
|
|
quint8 opCode;
|
2017-11-20 00:06:45 +01:00
|
|
|
bool masked;
|
|
|
|
quint64 payloadLength;
|
|
|
|
char key[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
QTcpSocket* _socket;
|
|
|
|
Logger* _log;
|
2019-07-12 16:54:26 +02:00
|
|
|
Hyperion* _hyperion;
|
2018-12-27 23:11:32 +01:00
|
|
|
JsonAPI* _jsonAPI;
|
2017-11-20 00:06:45 +01:00
|
|
|
|
|
|
|
void getWsFrameHeader(WebSocketHeader* header);
|
|
|
|
void sendClose(int status, QString reason = "");
|
2019-07-12 16:54:26 +02:00
|
|
|
void handleBinaryMessage(QByteArray &data);
|
2017-11-20 00:06:45 +01:00
|
|
|
qint64 sendMessage_Raw(const char* data, quint64 size);
|
|
|
|
qint64 sendMessage_Raw(QByteArray &data);
|
|
|
|
QByteArray makeFrameHeader(quint8 opCode, quint64 payloadLength, bool lastFrame);
|
|
|
|
|
|
|
|
/// The buffer used for reading data from the socket
|
|
|
|
QByteArray _receiveBuffer;
|
|
|
|
|
|
|
|
/// buffer for websockets multi frame receive
|
|
|
|
QByteArray _wsReceiveBuffer;
|
|
|
|
quint8 _maskKey[4];
|
|
|
|
|
|
|
|
bool _onContinuation = false;
|
|
|
|
|
|
|
|
// true when data is missing for parsing
|
|
|
|
bool _notEnoughData = false;
|
|
|
|
|
|
|
|
// websocket header store
|
|
|
|
WebSocketHeader _wsh;
|
|
|
|
|
2021-02-23 20:38:54 +01:00
|
|
|
//opCode of first frame (in case of fragmented frames)
|
|
|
|
quint8 _frameOpCode;
|
|
|
|
|
2017-11-20 00:06:45 +01:00
|
|
|
// masks for fields in the basic header
|
|
|
|
static uint8_t const BHB0_OPCODE = 0x0F;
|
|
|
|
static uint8_t const BHB0_RSV3 = 0x10;
|
|
|
|
static uint8_t const BHB0_RSV2 = 0x20;
|
|
|
|
static uint8_t const BHB0_RSV1 = 0x40;
|
|
|
|
static uint8_t const BHB0_FIN = 0x80;
|
|
|
|
|
|
|
|
static uint8_t const BHB1_PAYLOAD = 0x7F;
|
|
|
|
static uint8_t const BHB1_MASK = 0x80;
|
|
|
|
|
|
|
|
static uint8_t const payload_size_code_16bit = 0x7E; // 126
|
|
|
|
static uint8_t const payload_size_code_64bit = 0x7F; // 127
|
|
|
|
|
|
|
|
static const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2; //maximum size of a frame when sending a message
|
|
|
|
|
|
|
|
private slots:
|
2020-08-08 13:09:15 +02:00
|
|
|
void handleWebSocketFrame();
|
2017-11-20 00:06:45 +01:00
|
|
|
qint64 sendMessage(QJsonObject obj);
|
|
|
|
};
|