hyperion.ng
WebSocketClient.h
1 #pragma once
2 
3 #include <utils/Logger.h>
4 #include "webserver/WebSocketUtils.h"
5 #include <QJsonObject>
6 
7 class QTcpSocket;
8 class JsonAPI;
9 
10 class WebSocketClient : public QObject {
11  Q_OBJECT
12 public:
13  WebSocketClient(QByteArray socketKey, QTcpSocket* sock, QObject* parent);
14 
16  {
17  bool fin;
18  quint8 opCode;
19  bool masked;
20  quint64 payloadLength;
21  char key[4];
22  };
23 
24 private:
25  QTcpSocket* _socket;
26  QByteArray _secWebSocketKey;
27  Logger* _log;
28  JsonAPI* _jsonAPI;
29 
30  void getWsFrameHeader(WebSocketHeader* header);
31  void sendClose(int status, QString reason = "");
32 // void handleBinaryMessage(QByteArray &data);
33  qint64 sendMessage_Raw(const char* data, quint64 size);
34  qint64 sendMessage_Raw(QByteArray &data);
35  QByteArray makeFrameHeader(quint8 opCode, quint64 payloadLength, bool lastFrame);
36 
38  QByteArray _receiveBuffer;
39 
41  QByteArray _wsReceiveBuffer;
42  quint8 _maskKey[4];
43 
44  bool _onContinuation = false;
45 
46  // true when data is missing for parsing
47  bool _notEnoughData = false;
48 
49  // websocket header store
50  WebSocketHeader _wsh;
51 
52  // masks for fields in the basic header
53  static uint8_t const BHB0_OPCODE = 0x0F;
54  static uint8_t const BHB0_RSV3 = 0x10;
55  static uint8_t const BHB0_RSV2 = 0x20;
56  static uint8_t const BHB0_RSV1 = 0x40;
57  static uint8_t const BHB0_FIN = 0x80;
58 
59  static uint8_t const BHB1_PAYLOAD = 0x7F;
60  static uint8_t const BHB1_MASK = 0x80;
61 
62  static uint8_t const payload_size_code_16bit = 0x7E; // 126
63  static uint8_t const payload_size_code_64bit = 0x7F; // 127
64 
65  static const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2; //maximum size of a frame when sending a message
66 
67 private slots:
68  void handleWebSocketFrame(void);
69  qint64 sendMessage(QJsonObject obj);
70 };
Definition: Logger.h:32
Definition: WebSocketClient.h:10
Definition: WebSocketClient.h:15
Definition: JsonAPI.h:16