3 * @brief Defines the WebSocket class.
5 * \note Currently, only V13 (RFC6455) is supported.
6 * \note Both text and binary websockets are supported.
7 * \note The secure version (wss) is currently not implemented.
8 * @author Kurt Pattyn (pattyn.kurt@gmail.com)
11 #ifndef QWEBSOCKET_P_H
12 #define QWEBSOCKET_P_H
17 // This file is not part of the Qt API. It exists purely as an
18 // implementation detail. This header file may change from version to
19 // version without notice, or even be removed.
25 #include <QAbstractSocket>
26 #include <QHostAddress>
27 #include <QNetworkProxy>
29 #include "qwebsocketsglobal.h"
30 #include "qwebsocketprotocol.h"
31 #include "dataprocessor_p.h"
33 class HandshakeRequest;
34 class HandshakeResponse;
38 class QWebSocketPrivate:public QObject
43 explicit QWebSocketPrivate(QString origin,
44 QWebSocketProtocol::Version version,
45 QWebSocket * const pWebSocket,
47 virtual ~QWebSocketPrivate();
50 QAbstractSocket::SocketError error() const;
51 QString errorString() const;
54 QHostAddress localAddress() const;
55 quint16 localPort() const;
56 QHostAddress peerAddress() const;
57 QString peerName() const;
58 quint16 peerPort() const;
59 QNetworkProxy proxy() const;
60 qint64 readBufferSize() const;
61 void setProxy(const QNetworkProxy &networkProxy);
62 void setReadBufferSize(qint64 size);
63 void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
64 QVariant socketOption(QAbstractSocket::SocketOption option);
65 QAbstractSocket::SocketState state() const;
67 bool waitForConnected(int msecs = 30000);
68 bool waitForDisconnected(int msecs = 30000);
70 QWebSocketProtocol::Version version();
71 QString resourceName();
77 qint64 write(const char *message); //send data as text
78 qint64 write(const char *message, qint64 maxSize); //send data as text
79 qint64 write(const QString &message); //send data as text
80 qint64 write(const QByteArray &data); //send data as binary
83 virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
84 virtual void open(const QUrl &url, bool mask = true);
91 void stateChanged(QAbstractSocket::SocketState state);
92 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
93 void readChannelFinished();
94 void textFrameReceived(QString frame, bool isLastFrame);
95 void binaryFrameReceived(QByteArray frame, bool isLastFrame);
96 void textMessageReceived(QString message);
97 void binaryMessageReceived(QByteArray message);
98 void error(QAbstractSocket::SocketError error);
99 void pong(quint64 elapsedTime);
103 void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
104 void processHandshake(QTcpSocket *pSocket);
105 void processStateChanged(QAbstractSocket::SocketState socketState);
108 Q_DISABLE_COPY(QWebSocketPrivate)
110 QWebSocket * const q_ptr;
112 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = 0);
113 void setVersion(QWebSocketProtocol::Version version);
114 void setResourceName(QString resourceName);
115 void setRequestUrl(QUrl requestUrl);
116 void setOrigin(QString origin);
117 void setProtocol(QString protocol);
118 void setExtension(QString extension);
119 void enableMasking(bool enable);
120 void setSocketState(QAbstractSocket::SocketState state);
121 void setErrorString(QString errorString);
123 qint64 doWriteData(const QByteArray &data, bool isBinary);
124 qint64 doWriteFrames(const QByteArray &data, bool isBinary);
126 void makeConnections(const QTcpSocket *pTcpSocket);
127 void releaseConnections(const QTcpSocket *pTcpSocket);
129 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
130 QString calculateAcceptKey(const QString &key) const;
131 QString createHandShakeRequest(QString resourceName,
138 static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
139 const HandshakeRequest &request,
140 const HandshakeResponse &response,
141 QObject *parent = 0);
143 quint32 generateMaskingKey() const;
144 QByteArray generateKey() const;
145 quint32 generateRandomNumber() const;
146 qint64 writeFrames(const QList<QByteArray> &frames);
147 qint64 writeFrame(const QByteArray &frame);
149 QTcpSocket *m_pSocket;
150 QString m_errorString;
151 QWebSocketProtocol::Version m_version;
153 QString m_resourceName;
158 QAbstractSocket::SocketState m_socketState;
160 QByteArray m_key; //identification key used in handshake requests
162 bool m_mustMask; //a server must not mask the frames it sends
164 bool m_isClosingHandshakeSent;
165 bool m_isClosingHandshakeReceived;
169 DataProcessor m_dataProcessor;
172 friend class QWebSocketServerPrivate;
173 friend class QWebSocket;
176 #endif // QWEBSOCKET_H