MythTV master
mythwebsocket.h
Go to the documentation of this file.
1#ifndef MYTHWEBSOCKET_H
2#define MYTHWEBSOCKET_H
3
4// Qt
5#include <QTimer>
6#include <QObject>
7
8#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
9#include <QTextCodec>
10#else
11#include <QStringDecoder>
12#endif
13#include <QElapsedTimer>
14
15// MythTV
17
18class QTcpSocket;
19
20class MythWebSocket : public QObject
21{
22 Q_OBJECT
23
24 public:
25 ~MythWebSocket() override;
26 static MythWebSocket* CreateWebsocket(bool Server, QTcpSocket* Socket,
27 MythSocketProtocol Protocol, bool Testing);
28 public slots:
29 void Read();
30 void Write(int64_t Written = 0);
31 void Close();
32 void SendTextFrame(const QString& Text);
33 void SendBinaryFrame(const QByteArray& Data);
34
35 signals:
36 void NewTextMessage(const StringPayload& Text);
37 void NewRawTextMessage(const DataPayloads& Payloads);
38 void NewBinaryMessage(const DataPayloads& Payloads);
39
40 protected:
41 MythWebSocket(bool Server, QTcpSocket* Socket,
42 MythSocketProtocol Protocol, bool Testing);
43
44 private:
45 void PingReceived (DataPayload Payload);
46 void PongReceived (const DataPayload& Payload);
47 void CloseReceived (const DataPayload& Payload);
48 void CheckClose ();
49 void SendClose (WSErrorCode Code, const QString& Message = {});
50 void SendPing ();
51 void SendFrame (WSOpCode Code, const DataPayloads& Payloads);
52
53 enum ReadState : std::uint8_t
54 {
60 };
61
62 QTcpSocket* m_socket { nullptr };
65 bool m_preferRawText { false };
66 bool m_testing { false };
67 QTimer* m_timer { nullptr };
69 bool m_serverSide { false };
70 bool m_closeSent { false };
71 bool m_closeReceived { false };
72 bool m_closing { false };
73
76 int64_t m_messageSize { 0 };
79#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
80 QTextCodec* m_utf8Codec { QTextCodec::codecForName("UTF-8") };
81 QTextCodec::ConverterState* m_utf8CodecState { new QTextCodec::ConverterState };
82#else
83 QStringDecoder *m_toUtf16 { new QStringDecoder };
84#endif
85
88 int64_t m_writeTotal { 0 };
89 QElapsedTimer m_writeTime;
90};
91
92#endif
An implementation of the WebSocket protocol...
Definition: mythwebsocket.h:21
QTimer * m_timer
Definition: mythwebsocket.h:67
WSQueue m_writeQueue
Definition: mythwebsocket.h:87
void Close()
Initiate the close handshake when we are exiting.
DataPayloads m_dataFragments
Definition: mythwebsocket.h:77
void PingReceived(DataPayload Payload)
void SendTextFrame(const QString &Text)
void Write(int64_t Written=0)
static MythWebSocket * CreateWebsocket(bool Server, QTcpSocket *Socket, MythSocketProtocol Protocol, bool Testing)
StringPayload m_string
Definition: mythwebsocket.h:78
void NewRawTextMessage(const DataPayloads &Payloads)
void NewBinaryMessage(const DataPayloads &Payloads)
WSFrame m_currentFrame
Definition: mythwebsocket.h:74
WSOpCode m_messageOpCode
Definition: mythwebsocket.h:75
void SendFrame(WSOpCode Code, const DataPayloads &Payloads)
bool m_preferRawText
Definition: mythwebsocket.h:65
void CloseReceived(const DataPayload &Payload)
QElapsedTimer m_writeTime
Definition: mythwebsocket.h:89
QTcpSocket * m_socket
Definition: mythwebsocket.h:62
void PongReceived(const DataPayload &Payload)
void SendBinaryFrame(const QByteArray &Data)
int64_t m_writeTotal
Definition: mythwebsocket.h:88
void NewTextMessage(const StringPayload &Text)
void SendClose(WSErrorCode Code, const QString &Message={})
MythWebSocket(bool Server, QTcpSocket *Socket, MythSocketProtocol Protocol, bool Testing)
MythSocketProtocol m_protocol
Definition: mythwebsocket.h:63
int64_t m_messageSize
Definition: mythwebsocket.h:76
QStringDecoder * m_toUtf16
Definition: mythwebsocket.h:83
WSOpCode m_formatForProtocol
Definition: mythwebsocket.h:64
ReadState m_readState
Definition: mythwebsocket.h:68
bool m_closeReceived
Definition: mythwebsocket.h:71
~MythWebSocket() override
QByteArray m_lastPingPayload
Definition: mythwebsocket.h:86
std::vector< DataPayload > DataPayloads
std::shared_ptr< MythSharedString > StringPayload
std::shared_ptr< MythSharedData > DataPayload
MythSocketProtocol
@ ProtFrame
std::shared_ptr< MythWebSocketFrame > WSFrame
@ WSOpTextFrame
WSErrorCode
std::deque< DataPayload > WSQueue