Replaced write() methods with send() methods to comply with the QAbstractSocket API
[contrib/qtwebsockets.git] / src / qwebsocket_p.h
1 /**
2  * @file websocket.h
3  * @brief Defines the WebSocket class.
4  *
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)
9  */
10
11 #ifndef QWEBSOCKET_P_H
12 #define QWEBSOCKET_P_H
13 //
14 //  W A R N I N G
15 //  -------------
16 //
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.
20 //
21 // We mean it.
22 //
23
24 #include <QUrl>
25 #include <QAbstractSocket>
26 #include <QHostAddress>
27 #include <QNetworkProxy>
28 #include <QTime>
29 #include "qwebsocketsglobal.h"
30 #include "qwebsocketprotocol.h"
31 #include "dataprocessor_p.h"
32
33 class HandshakeRequest;
34 class HandshakeResponse;
35 class QTcpSocket;
36 class QWebSocket;
37
38 class QWebSocketPrivate:public QObject
39 {
40         Q_OBJECT
41
42 public:
43         explicit QWebSocketPrivate(QString origin,
44                                                            QWebSocketProtocol::Version version,
45                                                            QWebSocket * const pWebSocket,
46                                                            QObject *parent = 0);
47         virtual ~QWebSocketPrivate();
48
49         void abort();
50         QAbstractSocket::SocketError error() const;
51         QString errorString() const;
52         bool flush();
53         bool isValid();
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;
66
67         bool waitForConnected(int msecs = 30000);
68         bool waitForDisconnected(int msecs = 30000);
69
70         QWebSocketProtocol::Version version();
71         QString resourceName();
72         QUrl requestUrl();
73         QString origin();
74         QString protocol();
75         QString extension();
76
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
81
82 public Q_SLOTS:
83         virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
84         virtual void open(const QUrl &url, bool mask = true);
85         void ping();
86
87 Q_SIGNALS:
88         void aboutToClose();
89         void connected();
90         void disconnected();
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);
100
101 private Q_SLOTS:
102         void processData();
103         void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
104         void processHandshake(QTcpSocket *pSocket);
105         void processStateChanged(QAbstractSocket::SocketState socketState);
106
107 private:
108         Q_DISABLE_COPY(QWebSocketPrivate)
109
110         QWebSocket * const q_ptr;
111
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);
122
123         qint64 doWriteData(const QByteArray &data, bool isBinary);
124         qint64 doWriteFrames(const QByteArray &data, bool isBinary);
125
126         void makeConnections(const QTcpSocket *pTcpSocket);
127         void releaseConnections(const QTcpSocket *pTcpSocket);
128
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,
132                                                                    QString host,
133                                                                    QString origin,
134                                                                    QString extensions,
135                                                                    QString protocols,
136                                                                    QByteArray key);
137
138         static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
139                                                                    const HandshakeRequest &request,
140                                                                    const HandshakeResponse &response,
141                                                                    QObject *parent = 0);
142
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);
148
149         QTcpSocket *m_pSocket;
150         QString m_errorString;
151         QWebSocketProtocol::Version m_version;
152         QUrl m_resource;
153         QString m_resourceName;
154         QUrl m_requestUrl;
155         QString m_origin;
156         QString m_protocol;
157         QString m_extension;
158         QAbstractSocket::SocketState m_socketState;
159
160         QByteArray m_key;       //identification key used in handshake requests
161
162         bool m_mustMask;        //a server must not mask the frames it sends
163
164         bool m_isClosingHandshakeSent;
165         bool m_isClosingHandshakeReceived;
166
167         QTime m_pingTimer;
168
169         DataProcessor m_dataProcessor;
170
171
172         friend class QWebSocketServerPrivate;
173         friend class QWebSocket;
174 };
175
176 #endif // QWEBSOCKET_H