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 #ifndef QT_NO_NETWORKPROXY
28 #include <QNetworkProxy>
31 #include "qwebsocketsglobal.h"
32 #include "qwebsocketprotocol.h"
33 #include "dataprocessor_p.h"
37 class HandshakeRequest;
38 class HandshakeResponse;
42 class QWebSocketPrivate:public QObject
47 explicit QWebSocketPrivate(QString origin,
48 QWebSocketProtocol::Version version,
49 QWebSocket * const pWebSocket,
51 virtual ~QWebSocketPrivate();
54 QAbstractSocket::SocketError error() const;
55 QString errorString() const;
58 QHostAddress localAddress() const;
59 quint16 localPort() const;
60 QAbstractSocket::PauseModes pauseMode() const;
61 QHostAddress peerAddress() const;
62 QString peerName() const;
63 quint16 peerPort() const;
64 #ifndef QT_NO_NETWORKPROXY
65 QNetworkProxy proxy() const;
66 void setProxy(const QNetworkProxy &networkProxy);
68 qint64 readBufferSize() const;
70 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
71 void setReadBufferSize(qint64 size);
72 void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
73 QVariant socketOption(QAbstractSocket::SocketOption option);
74 QAbstractSocket::SocketState state() const;
76 bool waitForConnected(int msecs = 30000);
77 bool waitForDisconnected(int msecs = 30000);
79 QWebSocketProtocol::Version version();
80 QString resourceName();
86 qint64 write(const char *message); //send data as text
87 qint64 write(const char *message, qint64 maxSize); //send data as text
88 qint64 write(const QString &message); //send data as text
89 qint64 write(const QByteArray &data); //send data as binary
92 virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
93 virtual void open(const QUrl &url, bool mask = true);
100 void stateChanged(QAbstractSocket::SocketState state);
101 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
102 void readChannelFinished();
103 void textFrameReceived(QString frame, bool isLastFrame);
104 void binaryFrameReceived(QByteArray frame, bool isLastFrame);
105 void textMessageReceived(QString message);
106 void binaryMessageReceived(QByteArray message);
107 void error(QAbstractSocket::SocketError error);
108 void pong(quint64 elapsedTime);
112 void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
113 void processHandshake(QTcpSocket *pSocket);
114 void processStateChanged(QAbstractSocket::SocketState socketState);
117 Q_DISABLE_COPY(QWebSocketPrivate)
119 QWebSocket * const q_ptr;
121 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = 0);
122 void setVersion(QWebSocketProtocol::Version version);
123 void setResourceName(QString resourceName);
124 void setRequestUrl(QUrl requestUrl);
125 void setOrigin(QString origin);
126 void setProtocol(QString protocol);
127 void setExtension(QString extension);
128 void enableMasking(bool enable);
129 void setSocketState(QAbstractSocket::SocketState state);
130 void setErrorString(QString errorString);
132 qint64 doWriteData(const QByteArray &data, bool isBinary);
133 qint64 doWriteFrames(const QByteArray &data, bool isBinary);
135 void makeConnections(const QTcpSocket *pTcpSocket);
136 void releaseConnections(const QTcpSocket *pTcpSocket);
138 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
139 QString calculateAcceptKey(const QString &key) const;
140 QString createHandShakeRequest(QString resourceName,
147 static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
148 const HandshakeRequest &request,
149 const HandshakeResponse &response,
150 QObject *parent = 0);
152 quint32 generateMaskingKey() const;
153 QByteArray generateKey() const;
154 quint32 generateRandomNumber() const;
155 qint64 writeFrames(const QList<QByteArray> &frames);
156 qint64 writeFrame(const QByteArray &frame);
158 QTcpSocket *m_pSocket;
159 QString m_errorString;
160 QWebSocketProtocol::Version m_version;
162 QString m_resourceName;
167 QAbstractSocket::SocketState m_socketState;
169 QByteArray m_key; //identification key used in handshake requests
171 bool m_mustMask; //a server must not mask the frames it sends
173 bool m_isClosingHandshakeSent;
174 bool m_isClosingHandshakeReceived;
178 DataProcessor m_dataProcessor;
181 friend class QWebSocketServerPrivate;
182 friend class QWebSocket;
187 #endif // QWEBSOCKET_H