Torc  0.1
torcwebsocketreader.h
Go to the documentation of this file.
1 #ifndef TORCWEBSOCKETREADER_H
2 #define TORCWEBSOCKETREADER_H
3 
4 // Qt
5 #include <QTcpSocket>
6 
7 // Torc
8 #define TORC_JSON_RPC QStringLiteral("torc.json-rpc")
9 
11 {
12  friend class TorcWebSocket;
13 
14  public:
15  enum OpCode
16  {
18  OpText = 0x1,
19  OpBinary = 0x2,
20  OpReserved3 = 0x3,
21  OpReserved4 = 0x4,
22  OpReserved5 = 0x5,
23  OpReserved6 = 0x6,
24  OpReserved7 = 0x7,
25  OpClose = 0x8,
26  OpPing = 0x9,
27  OpPong = 0xA,
28  OpReservedB = 0xB,
29  OpReservedC = 0xC,
30  OpReservedD = 0xD,
31  OpReservedE = 0xE,
33  };
34 
35  enum CloseCode
36  {
37  CloseNormal = 1000,
43  CloseAbnormal = 1006,
50  };
51 
52  enum WSVersion
53  {
55  Version0 = 0,
56  Version4 = 4,
57  Version5 = 5,
58  Version6 = 6,
59  Version7 = 7,
60  Version8 = 8,
61  Version13 = 13
62  };
63 
65  {
66  SubProtocolNone = (0 << 0),
67  SubProtocolJSONRPC = (1 << 0)
68  };
69  Q_FLAGS(WSSubProtocol)
70  Q_DECLARE_FLAGS(WSSubProtocols, WSSubProtocol)
71 
72  protected:
73  static QString OpCodeToString (OpCode Code);
74  static QString CloseCodeToString (CloseCode Code);
75  static OpCode FormatForSubProtocol (WSSubProtocol Protocol);
76  static QString SubProtocolsToString (WSSubProtocols Protocols);
77  static WSSubProtocols SubProtocolsFromString (const QString &Protocols);
78  static QList<WSSubProtocol> SubProtocolsFromPrioritisedString (const QString &Protocols);
79 
80  TorcWebSocketReader(QTcpSocket &Socket, WSSubProtocol Protocol, bool ServerSide);
81  ~TorcWebSocketReader() = default;
82 
83  const QByteArray& GetPayload (void);
84  void Reset (void);
85  bool CloseSent (void);
86  void SendFrame (OpCode Code, QByteArray &Payload);
87  void InitiateClose (CloseCode Close, const QString &Reason);
88  bool Read (void);
89  void EnableEcho (void);
90  void SetSubProtocol (WSSubProtocol Protocol);
91 
92  private:
93  void HandlePing (QByteArray &Payload);
94  void HandlePong (QByteArray &Payload);
95  void HandleCloseRequest (QByteArray &Close);
96 
97  private:
98  enum ReadState
99  {
100  ReadHeader,
101  Read16BitLength,
102  Read64BitLength,
103  ReadMask,
104  ReadPayload
105  };
106 
107  QTcpSocket &m_socket;
108  bool m_serverSide;
109  bool m_closeSent;
110  bool m_closeReceived;
111  bool m_echoTest;
112  WSSubProtocol m_subProtocol;
113  OpCode m_subProtocolFrameFormat;
114 
115  // Read state
116  ReadState m_readState;
117  OpCode m_frameOpCode;
118  bool m_frameFinalFragment;
119  bool m_frameMasked;
120  bool m_haveBufferedPayload;
121  QByteArray m_bufferedPayload;
122  OpCode m_bufferedPayloadOpCode;
123  quint64 m_framePayloadLength;
124  quint64 m_framePayloadReadPosition;
125  QByteArray m_frameMask;
126  QByteArray m_framePayload;
127 };
128 
129 Q_DECLARE_OPERATORS_FOR_FLAGS(TorcWebSocketReader::WSSubProtocols)
130 #endif // TORCWEBSOCKETREADER_H
void SetSubProtocol(WSSubProtocol Protocol)
static QList< WSSubProtocol > SubProtocolsFromPrioritisedString(const QString &Protocols)
Parse a prioritised list of supported WebSocket sub-protocols.
const QByteArray & GetPayload(void)
Overlays the Websocket protocol over a QTcpSocket.
Definition: torcwebsocket.h:23
static QString SubProtocolsToString(WSSubProtocols Protocols)
Convert SubProtocols to HTTP readable string.
void SendFrame(OpCode Code, QByteArray &Payload)
static QString CloseCodeToString(CloseCode Code)
Convert CloseCode to human readable string.
static WSSubProtocols SubProtocolsFromString(const QString &Protocols)
Parse supported WSSubProtocols from Protocols.
void InitiateClose(CloseCode Close, const QString &Reason)
static OpCode FormatForSubProtocol(WSSubProtocol Protocol)
static QString OpCodeToString(OpCode Code)
Convert OpCode to human readable string.