Torc  0.1
torcwebsocket.h
Go to the documentation of this file.
1 #ifndef TORCWEBSOCKET_H
2 #define TORCWEBSOCKET_H
3 
4 // Qt
5 #include <QUrl>
6 #include <QTimer>
7 #include <QObject>
8 #include <QSslSocket>
9 #include <QHostAddress>
10 
11 // Torc
12 #include "torchttpreader.h"
13 #include "torcwebsocketreader.h"
14 #include "torcqthread.h"
15 
16 class TorcHTTPRequest;
17 class TorcRPCRequest;
19 
20 #define HTTP_SOCKET_TIMEOUT 30000 // 30 seconds of inactivity
21 #define FULL_SOCKET_TIMEOUT 300000 // 5 minutes of inactivity
22 
23 class TorcWebSocket : public QSslSocket
24 {
25  Q_OBJECT
26 
27  public:
29  {
30  DisconnectedSt = (0 << 0),
31  ConnectingTo = (1 << 1),
32  ConnectedTo = (1 << 2),
33  Upgrading = (1 << 3),
34  Upgraded = (1 << 4),
35  Disconnecting = (1 << 5),
36  ErroredSt = (1 << 6)
37  };
38 
39  public:
40  TorcWebSocket(TorcWebSocketThread* Parent, qintptr SocketDescriptor, bool Secure);
41  TorcWebSocket(TorcWebSocketThread* Parent, const QHostAddress &Address, quint16 Port, bool Secure,
44 
45  static QVariantList GetSupportedSubProtocols (void);
46  bool IsSecure (void);
47 
48  signals:
49  void ConnectionEstablished (void);
50  void ConnectionUpgraded (void);
51  void Disconnected (void);
52  void Disconnect (void);
53 
54  public slots:
55  void Start (void);
56  void CloseSocket (void);
57  void PropertyChanged (void);
58  bool HandleNotification (const QString &Method);
59  void RemoteRequest (TorcRPCRequest *Request);
60  void CancelRequest (TorcRPCRequest *Request);
61 
62  // SSL
63  void Encrypted (void);
64  void SSLErrors (const QList<QSslError> &Errors);
65 
66  protected slots:
67  void ReadyRead (void);
68  void Connected (void);
69  void Error (QAbstractSocket::SocketError);
70  void SubscriberDeleted (QObject *Subscriber);
71  void TimedOut (void);
72  void BytesWritten (qint64);
73 
74  protected:
75  bool event (QEvent *Event) override;
76 
77  private:
78  Q_DISABLE_COPY(TorcWebSocket)
79  void SetState (SocketState State);
80  void HandleUpgradeRequest (TorcHTTPRequest &Request);
81  void SendHandshake (void);
82  void ReadHandshake (void);
83  void ReadHTTP (void);
84  void ProcessPayload (const QByteArray &Payload);
85 
86  private:
87  TorcWebSocketThread *m_parent;
88  QString m_debug;
89  bool m_secure;
90  SocketState m_socketState;
91  qintptr m_socketDescriptor;
92  QTimer m_watchdogTimer;
93  TorcHTTPReader m_reader;
94  TorcWebSocketReader m_wsReader;
95  bool m_authenticated;
96  QString m_challengeResponse;
97  QHostAddress m_address;
98  quint16 m_port;
99  bool m_serverSide;
100 
101  TorcWebSocketReader::WSSubProtocol m_subProtocol;
102  TorcWebSocketReader::OpCode m_subProtocolFrameFormat;
103 
104  int m_currentRequestID;
105  QMap<int,TorcRPCRequest*> m_currentRequests;
106  QMap<int,int> m_requestTimers;
107 
108  QMultiMap<QString,QObject*> m_subscribers; // client side
109 };
110 
111 #endif // TORCWEBSOCKET_H
void BytesWritten(qint64)
A class to encapsulate an incoming HTTP request.
void SubscriberDeleted(QObject *Subscriber)
A subscriber object has been deleted.
void Connected(void)
Overlays the Websocket protocol over a QTcpSocket.
Definition: torcwebsocket.h:23
A convenience class to read HTTP requests from a QTcpSocket.
Definition: torchttpreader.h:8
void ConnectionUpgraded(void)
bool IsSecure(void)
bool HandleNotification(const QString &Method)
void Disconnect(void)
void CancelRequest(TorcRPCRequest *Request)
Cancel a Remote Procedure Call.
A class encapsulating a Remote Procedure Call.
TorcWebSocket(TorcWebSocketThread *Parent, qintptr SocketDescriptor, bool Secure)
void SSLErrors(const QList< QSslError > &Errors)
void CloseSocket(void)
void Disconnected(void)
bool event(QEvent *Event) override
static QVariantList GetSupportedSubProtocols(void)
Return a list of supported WebSocket sub-protocols.
void Encrypted(void)
void ReadyRead(void)
Process incoming data.
Wraps a TorcQThread around a TorcWebsocket.
void Error(QAbstractSocket::SocketError)
void TimedOut(void)
void PropertyChanged(void)
Receives notifications when a property for a subscribed service has changed.
void RemoteRequest(TorcRPCRequest *Request)
Initiate a Remote Procedure Call.
void Start(void)
Initialise the websocket once its parent thread is ready.
void ConnectionEstablished(void)