1/****************************************************************************
2**
3** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWebSockets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QWEBSOCKET_H
41#define QWEBSOCKET_H
42
43#include <QtCore/QUrl>
44#include <QtNetwork/QAbstractSocket>
45#include <QtNetwork/QNetworkRequest>
46#ifndef QT_NO_NETWORKPROXY
47#include <QtNetwork/QNetworkProxy>
48#endif
49#ifndef QT_NO_SSL
50#include <QtNetwork/QSslError>
51#include <QtNetwork/QSslConfiguration>
52#endif
53#include "QtWebSockets/qwebsockets_global.h"
54#include "QtWebSockets/qwebsocketprotocol.h"
55
56QT_BEGIN_NAMESPACE
57
58class QTcpSocket;
59class QWebSocketPrivate;
60class QMaskGenerator;
61
62class Q_WEBSOCKETS_EXPORT QWebSocket : public QObject
63{
64 Q_OBJECT
65 Q_DISABLE_COPY(QWebSocket)
66 Q_DECLARE_PRIVATE(QWebSocket)
67
68public:
69 explicit QWebSocket(const QString &origin = QString(),
70 QWebSocketProtocol::Version version = QWebSocketProtocol::VersionLatest,
71 QObject *parent = nullptr);
72 ~QWebSocket() override;
73
74 void abort();
75 QAbstractSocket::SocketError error() const;
76 QString errorString() const;
77 bool flush();
78 bool isValid() const;
79 QHostAddress localAddress() const;
80 quint16 localPort() const;
81 QAbstractSocket::PauseModes pauseMode() const;
82 QHostAddress peerAddress() const;
83 QString peerName() const;
84 quint16 peerPort() const;
85#ifndef QT_NO_NETWORKPROXY
86 QNetworkProxy proxy() const;
87 void setProxy(const QNetworkProxy &networkProxy);
88#endif
89 void setMaskGenerator(const QMaskGenerator *maskGenerator);
90 const QMaskGenerator *maskGenerator() const;
91 qint64 readBufferSize() const;
92 void setReadBufferSize(qint64 size);
93
94 void resume();
95 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
96
97 QAbstractSocket::SocketState state() const;
98
99 QWebSocketProtocol::Version version() const;
100 QString resourceName() const;
101 QUrl requestUrl() const;
102 QNetworkRequest request() const;
103 QString origin() const;
104 QWebSocketProtocol::CloseCode closeCode() const;
105 QString closeReason() const;
106
107 qint64 sendTextMessage(const QString &message);
108 qint64 sendBinaryMessage(const QByteArray &data);
109
110#ifndef QT_NO_SSL
111 void ignoreSslErrors(const QList<QSslError> &errors);
112 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
113 QSslConfiguration sslConfiguration() const;
114#endif
115
116 qint64 bytesToWrite() const;
117
118public Q_SLOTS:
119 void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CloseCodeNormal,
120 const QString &reason = QString());
121 void open(const QUrl &url);
122 void open(const QNetworkRequest &request);
123 void ping(const QByteArray &payload = QByteArray());
124#ifndef QT_NO_SSL
125 void ignoreSslErrors();
126#endif
127
128Q_SIGNALS:
129 void aboutToClose();
130 void connected();
131 void disconnected();
132 void stateChanged(QAbstractSocket::SocketState state);
133#ifndef QT_NO_NETWORKPROXY
134 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
135#endif
136 void readChannelFinished();
137 void textFrameReceived(const QString &frame, bool isLastFrame);
138 void binaryFrameReceived(const QByteArray &frame, bool isLastFrame);
139 void textMessageReceived(const QString &message);
140 void binaryMessageReceived(const QByteArray &message);
141 void error(QAbstractSocket::SocketError error);
142 void pong(quint64 elapsedTime, const QByteArray &payload);
143 void bytesWritten(qint64 bytes);
144
145#ifndef QT_NO_SSL
146 void sslErrors(const QList<QSslError> &errors);
147 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
148#endif
149
150private:
151 QWebSocket(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version,
152 QObject *parent = nullptr);
153};
154
155QT_END_NAMESPACE
156
157#endif // QWEBSOCKET_H
158