| 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 QWEBSOCKETSERVER_H |
| 41 | #define QWEBSOCKETSERVER_H |
| 42 | |
| 43 | #include "QtWebSockets/qwebsockets_global.h" |
| 44 | #include "QtWebSockets/qwebsocketprotocol.h" |
| 45 | |
| 46 | #include <QtCore/QObject> |
| 47 | #include <QtCore/QString> |
| 48 | #include <QtNetwork/QHostAddress> |
| 49 | |
| 50 | #ifndef QT_NO_SSL |
| 51 | #include <QtNetwork/QSslConfiguration> |
| 52 | #include <QtNetwork/QSslError> |
| 53 | #endif |
| 54 | |
| 55 | #if QT_HAS_INCLUDE(<chrono>) |
| 56 | #include <chrono> |
| 57 | #endif |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | class QTcpSocket; |
| 62 | class QWebSocketServerPrivate; |
| 63 | class QWebSocket; |
| 64 | class QWebSocketCorsAuthenticator; |
| 65 | |
| 66 | class Q_WEBSOCKETS_EXPORT QWebSocketServer : public QObject |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | Q_DISABLE_COPY(QWebSocketServer) |
| 70 | Q_DECLARE_PRIVATE(QWebSocketServer) |
| 71 | |
| 72 | public: |
| 73 | enum SslMode { |
| 74 | #ifndef QT_NO_SSL |
| 75 | SecureMode = 0, |
| 76 | #endif |
| 77 | NonSecureMode = 1 |
| 78 | }; |
| 79 | Q_ENUM(SslMode) |
| 80 | |
| 81 | explicit QWebSocketServer(const QString &serverName, SslMode secureMode, |
| 82 | QObject *parent = nullptr); |
| 83 | ~QWebSocketServer() override; |
| 84 | |
| 85 | bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0); |
| 86 | void close(); |
| 87 | |
| 88 | bool isListening() const; |
| 89 | |
| 90 | void setMaxPendingConnections(int numConnections); |
| 91 | int maxPendingConnections() const; |
| 92 | |
| 93 | #if QT_HAS_INCLUDE(<chrono>) || defined(Q_CLANG_QDOC) |
| 94 | void setHandshakeTimeout(std::chrono::milliseconds msec) |
| 95 | { |
| 96 | setHandshakeTimeout(int(msec.count())); |
| 97 | } |
| 98 | std::chrono::milliseconds handshakeTimeout() const |
| 99 | { |
| 100 | return std::chrono::milliseconds(handshakeTimeoutMS()); |
| 101 | } |
| 102 | #endif |
| 103 | void setHandshakeTimeout(int msec); |
| 104 | int handshakeTimeoutMS() const; |
| 105 | |
| 106 | quint16 serverPort() const; |
| 107 | QHostAddress serverAddress() const; |
| 108 | QUrl serverUrl() const; |
| 109 | |
| 110 | SslMode secureMode() const; |
| 111 | |
| 112 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 113 | bool setSocketDescriptor(qintptr socketDescriptor); |
| 114 | qintptr socketDescriptor() const; |
| 115 | bool setNativeDescriptor(qintptr descriptor) { return setSocketDescriptor(descriptor); } |
| 116 | qintptr nativeDescriptor() const { return socketDescriptor(); } |
| 117 | #else // ### Qt 6: Remove leftovers |
| 118 | Q_DECL_DEPRECATED_X("Use setNativeDescriptor" ) bool setSocketDescriptor(int socketDescriptor); |
| 119 | Q_DECL_DEPRECATED_X("Use nativeDescriptor" ) int socketDescriptor() const; |
| 120 | bool setNativeDescriptor(qintptr descriptor); |
| 121 | qintptr nativeDescriptor() const; |
| 122 | #endif // (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 123 | |
| 124 | |
| 125 | bool hasPendingConnections() const; |
| 126 | virtual QWebSocket *nextPendingConnection(); |
| 127 | |
| 128 | QWebSocketProtocol::CloseCode error() const; |
| 129 | QString errorString() const; |
| 130 | |
| 131 | void pauseAccepting(); |
| 132 | void resumeAccepting(); |
| 133 | |
| 134 | void setServerName(const QString &serverName); |
| 135 | QString serverName() const; |
| 136 | |
| 137 | #ifndef QT_NO_NETWORKPROXY |
| 138 | void setProxy(const QNetworkProxy &networkProxy); |
| 139 | QNetworkProxy proxy() const; |
| 140 | #endif |
| 141 | #ifndef QT_NO_SSL |
| 142 | void setSslConfiguration(const QSslConfiguration &sslConfiguration); |
| 143 | QSslConfiguration sslConfiguration() const; |
| 144 | #endif |
| 145 | |
| 146 | QList<QWebSocketProtocol::Version> supportedVersions() const; |
| 147 | |
| 148 | void handleConnection(QTcpSocket *socket) const; |
| 149 | |
| 150 | Q_SIGNALS: |
| 151 | void acceptError(QAbstractSocket::SocketError socketError); |
| 152 | void serverError(QWebSocketProtocol::CloseCode closeCode); |
| 153 | //TODO: should use a delegate iso of a synchronous signal |
| 154 | //see also QTBUG-16251 |
| 155 | void originAuthenticationRequired(QWebSocketCorsAuthenticator *pAuthenticator); |
| 156 | void newConnection(); |
| 157 | #ifndef QT_NO_SSL |
| 158 | void peerVerifyError(const QSslError &error); |
| 159 | void sslErrors(const QList<QSslError> &errors); |
| 160 | void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator); |
| 161 | #endif |
| 162 | void closed(); |
| 163 | }; |
| 164 | |
| 165 | QT_END_NAMESPACE |
| 166 | |
| 167 | #endif // QWEBSOCKETSERVER_H |
| 168 | |