Renamed qwebsocket.pri to qwebsockets.pri
[contrib/qtwebsockets.git] / src / qwebsocket_p.h
1 /*
2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #ifndef QWEBSOCKET_P_H
21 #define QWEBSOCKET_P_H
22 //
23 //  W A R N I N G
24 //  -------------
25 //
26 // This file is not part of the Qt API.  It exists purely as an
27 // implementation detail.  This header file may change from version to
28 // version without notice, or even be removed.
29 //
30 // We mean it.
31 //
32
33 #include <QUrl>
34 #include <QAbstractSocket>
35 #include <QHostAddress>
36 #ifndef QT_NO_NETWORKPROXY
37 #include <QNetworkProxy>
38 #endif
39 #include <QTime>
40 #include "qwebsocketsglobal.h"
41 #include "qwebsocketprotocol.h"
42 #include "dataprocessor_p.h"
43
44 QT_BEGIN_NAMESPACE
45
46 class HandshakeRequest;
47 class HandshakeResponse;
48 class QTcpSocket;
49 class QWebSocket;
50
51 class QWebSocketPrivate:public QObject
52 {
53         Q_OBJECT
54
55 public:
56         explicit QWebSocketPrivate(QString origin,
57                                                            QWebSocketProtocol::Version version,
58                                                            QWebSocket * const pWebSocket,
59                                                            QObject *parent = 0);
60         virtual ~QWebSocketPrivate();
61
62         void abort();
63         QAbstractSocket::SocketError error() const;
64         QString errorString() const;
65         bool flush();
66         bool isValid();
67         QHostAddress localAddress() const;
68         quint16 localPort() const;
69         QAbstractSocket::PauseModes pauseMode() const;
70         QHostAddress peerAddress() const;
71         QString peerName() const;
72         quint16 peerPort() const;
73 #ifndef QT_NO_NETWORKPROXY
74         QNetworkProxy proxy() const;
75         void setProxy(const QNetworkProxy &networkProxy);
76 #endif
77         qint64 readBufferSize() const;
78         void resume();
79         void setPauseMode(QAbstractSocket::PauseModes pauseMode);
80         void setReadBufferSize(qint64 size);
81         void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
82         QVariant socketOption(QAbstractSocket::SocketOption option);
83         QAbstractSocket::SocketState state() const;
84
85         bool waitForConnected(int msecs = 30000);
86         bool waitForDisconnected(int msecs = 30000);
87
88         QWebSocketProtocol::Version version();
89         QString resourceName();
90         QUrl requestUrl();
91         QString origin();
92         QString protocol();
93         QString extension();
94
95         qint64 write(const char *message);              //send data as text
96         qint64 write(const char *message, qint64 maxSize);              //send data as text
97         qint64 write(const QString &message);   //send data as text
98         qint64 write(const QByteArray &data);   //send data as binary
99
100 public Q_SLOTS:
101         virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
102         virtual void open(const QUrl &url, bool mask = true);
103         void ping();
104
105 private Q_SLOTS:
106         void processData();
107         void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
108         void processHandshake(QTcpSocket *pSocket);
109         void processStateChanged(QAbstractSocket::SocketState socketState);
110
111 private:
112         Q_DISABLE_COPY(QWebSocketPrivate)
113
114         QWebSocket * const q_ptr;
115
116         QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = 0);
117         void setVersion(QWebSocketProtocol::Version version);
118         void setResourceName(QString resourceName);
119         void setRequestUrl(QUrl requestUrl);
120         void setOrigin(QString origin);
121         void setProtocol(QString protocol);
122         void setExtension(QString extension);
123         void enableMasking(bool enable);
124         void setSocketState(QAbstractSocket::SocketState state);
125         void setErrorString(QString errorString);
126
127         qint64 doWriteData(const QByteArray &data, bool isBinary);
128         qint64 doWriteFrames(const QByteArray &data, bool isBinary);
129
130         void makeConnections(const QTcpSocket *pTcpSocket);
131         void releaseConnections(const QTcpSocket *pTcpSocket);
132
133         QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
134         QString calculateAcceptKey(const QString &key) const;
135         QString createHandShakeRequest(QString resourceName,
136                                                                    QString host,
137                                                                    QString origin,
138                                                                    QString extensions,
139                                                                    QString protocols,
140                                                                    QByteArray key);
141
142         static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
143                                                                    const HandshakeRequest &request,
144                                                                    const HandshakeResponse &response,
145                                                                    QObject *parent = 0);
146
147         quint32 generateMaskingKey() const;
148         QByteArray generateKey() const;
149         quint32 generateRandomNumber() const;
150         qint64 writeFrames(const QList<QByteArray> &frames);
151         qint64 writeFrame(const QByteArray &frame);
152
153         QTcpSocket *m_pSocket;
154         QString m_errorString;
155         QWebSocketProtocol::Version m_version;
156         QUrl m_resource;
157         QString m_resourceName;
158         QUrl m_requestUrl;
159         QString m_origin;
160         QString m_protocol;
161         QString m_extension;
162         QAbstractSocket::SocketState m_socketState;
163
164         QByteArray m_key;       //identification key used in handshake requests
165
166         bool m_mustMask;        //a server must not mask the frames it sends
167
168         bool m_isClosingHandshakeSent;
169         bool m_isClosingHandshakeReceived;
170
171         QTime m_pingTimer;
172
173         DataProcessor m_dataProcessor;
174
175
176         friend class QWebSocketServerPrivate;
177         friend class QWebSocket;
178 };
179
180 QT_END_NAMESPACE
181
182 #endif // QWEBSOCKET_H