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_P_H
41#define QWEBSOCKETSERVER_P_H
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <QtCore/QObject>
54#include <QtCore/QQueue>
55#include <QtCore/QString>
56#include <QtNetwork/QHostAddress>
57#include <private/qobject_p.h>
58#include "qwebsocketserver.h"
59#include "qwebsocket.h"
60
61#ifndef QT_NO_SSL
62#include <QtNetwork/QSslConfiguration>
63#include <QtNetwork/QSslError>
64#endif
65
66QT_BEGIN_NAMESPACE
67
68class QTcpServer;
69class QTcpSocket;
70
71class QWebSocketServerPrivate : public QObjectPrivate
72{
73 Q_DISABLE_COPY(QWebSocketServerPrivate)
74
75public:
76 Q_DECLARE_PUBLIC(QWebSocketServer)
77 enum SslMode
78 {
79 SecureMode = true,
80 NonSecureMode
81 };
82
83 explicit QWebSocketServerPrivate(const QString &serverName, SslMode secureMode);
84 ~QWebSocketServerPrivate() override;
85
86 void init();
87 void close(bool aboutToDestroy = false);
88 QString errorString() const;
89 bool hasPendingConnections() const;
90 bool isListening() const;
91 bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
92 int maxPendingConnections() const;
93 int handshakeTimeout() const {
94 return m_handshakeTimeout;
95 }
96 virtual QWebSocket *nextPendingConnection();
97 void pauseAccepting();
98#ifndef QT_NO_NETWORKPROXY
99 QNetworkProxy proxy() const;
100 void setProxy(const QNetworkProxy &networkProxy);
101#endif
102 void resumeAccepting();
103 QHostAddress serverAddress() const;
104 QWebSocketProtocol::CloseCode serverError() const;
105 quint16 serverPort() const;
106 void setMaxPendingConnections(int numConnections);
107 void setHandshakeTimeout(int msec) {
108 m_handshakeTimeout = msec;
109 }
110 bool setSocketDescriptor(qintptr socketDescriptor);
111 qintptr socketDescriptor() const;
112
113 QList<QWebSocketProtocol::Version> supportedVersions() const;
114 QStringList supportedProtocols() const;
115 QStringList supportedExtensions() const;
116
117 void setServerName(const QString &serverName);
118 QString serverName() const;
119
120 SslMode secureMode() const;
121
122#ifndef QT_NO_SSL
123 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
124 QSslConfiguration sslConfiguration() const;
125#endif
126
127 void setError(QWebSocketProtocol::CloseCode code, const QString &errorString);
128
129 void handleConnection(QTcpSocket *pTcpSocket) const;
130
131private slots:
132 void startHandshakeTimeout(QTcpSocket *pTcpSocket);
133
134private:
135 QTcpServer *m_pTcpServer;
136 QString m_serverName;
137 SslMode m_secureMode;
138 QQueue<QWebSocket *> m_pendingConnections;
139 QWebSocketProtocol::CloseCode m_error;
140 QString m_errorString;
141 int m_maxPendingConnections;
142 int m_handshakeTimeout;
143
144 void addPendingConnection(QWebSocket *pWebSocket);
145 void setErrorFromSocketError(QAbstractSocket::SocketError error,
146 const QString &errorDescription);
147
148 void onNewConnection();
149 void onSocketDisconnected();
150 void handshakeReceived();
151 void finishHandshakeTimeout(QTcpSocket *pTcpSocket);
152};
153
154QT_END_NAMESPACE
155
156#endif // QWEBSOCKETSERVER_P_H
157

source code of qtwebsockets/src/websockets/qwebsocketserver_p.h