1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml 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#include "qtcpserverconnectionfactory.h"
41
42#include <private/qqmldebugserver_p.h>
43
44#include <QtCore/qplugin.h>
45#include <QtNetwork/qtcpserver.h>
46#include <QtNetwork/qtcpsocket.h>
47
48QT_BEGIN_NAMESPACE
49
50class QTcpServerConnection : public QQmlDebugServerConnection
51{
52 Q_OBJECT
53 Q_DISABLE_COPY(QTcpServerConnection)
54
55public:
56 QTcpServerConnection();
57 ~QTcpServerConnection() override;
58
59 void setServer(QQmlDebugServer *server) override;
60 bool setPortRange(int portFrom, int portTo, bool block, const QString &hostaddress) override;
61 bool setFileName(const QString &fileName, bool block) override;
62
63 bool isConnected() const override;
64 void disconnect() override;
65
66 void waitForConnection() override;
67 void flush() override;
68
69private:
70 void newConnection();
71 bool listen();
72
73 int m_portFrom = 0;
74 int m_portTo = 0;
75 bool m_block = false;
76 QString m_hostaddress;
77 QTcpSocket *m_socket = nullptr;
78 QTcpServer *m_tcpServer = nullptr;
79 QQmlDebugServer *m_debugServer = nullptr;
80};
81
82QTcpServerConnection::QTcpServerConnection() {}
83
84QTcpServerConnection::~QTcpServerConnection()
85{
86 if (isConnected())
87 disconnect();
88}
89
90void QTcpServerConnection::setServer(QQmlDebugServer *server)
91{
92 m_debugServer = server;
93}
94
95bool QTcpServerConnection::isConnected() const
96{
97 return m_socket && m_socket->state() == QTcpSocket::ConnectedState;
98}
99
100void QTcpServerConnection::disconnect()
101{
102 while (m_socket && m_socket->bytesToWrite() > 0) {
103 if (!m_socket->waitForBytesWritten()) {
104 qWarning(msg: "QML Debugger: Failed to send remaining %lld bytes on disconnect.",
105 m_socket->bytesToWrite());
106 break;
107 }
108 }
109
110 m_socket->deleteLater();
111 m_socket = nullptr;
112}
113
114bool QTcpServerConnection::setPortRange(int portFrom, int portTo, bool block,
115 const QString &hostaddress)
116{
117 m_portFrom = portFrom;
118 m_portTo = portTo;
119 m_block = block;
120 m_hostaddress = hostaddress;
121
122 return listen();
123}
124
125bool QTcpServerConnection::setFileName(const QString &fileName, bool block)
126{
127 Q_UNUSED(fileName);
128 Q_UNUSED(block);
129 return false;
130}
131
132void QTcpServerConnection::waitForConnection()
133{
134 m_tcpServer->waitForNewConnection(msec: -1);
135}
136
137void QTcpServerConnection::flush()
138{
139 if (m_socket)
140 m_socket->flush();
141}
142
143bool QTcpServerConnection::listen()
144{
145 m_tcpServer = new QTcpServer(this);
146 QObject::connect(sender: m_tcpServer, signal: &QTcpServer::newConnection,
147 receiver: this, slot: &QTcpServerConnection::newConnection);
148 QHostAddress hostaddress;
149 if (!m_hostaddress.isEmpty()) {
150 if (!hostaddress.setAddress(m_hostaddress)) {
151 hostaddress = QHostAddress::Any;
152 qDebug(msg: "QML Debugger: Incorrect host address provided. So accepting connections "
153 "from any host.");
154 }
155 } else {
156 hostaddress = QHostAddress::Any;
157 }
158 int port = m_portFrom;
159 do {
160 if (m_tcpServer->listen(address: hostaddress, port)) {
161 qDebug(msg: "QML Debugger: Waiting for connection on port %d...", port);
162 break;
163 }
164 ++port;
165 } while (port <= m_portTo);
166 if (port > m_portTo) {
167 if (m_portFrom == m_portTo)
168 qWarning(msg: "QML Debugger: Unable to listen to port %d.", m_portFrom);
169 else
170 qWarning(msg: "QML Debugger: Unable to listen to ports %d - %d.", m_portFrom, m_portTo);
171 return false;
172 } else {
173 return true;
174 }
175}
176
177void QTcpServerConnection::newConnection()
178{
179 if (m_socket && m_socket->peerPort()) {
180 qWarning(msg: "QML Debugger: Another client is already connected.");
181 QTcpSocket *faultyConnection = m_tcpServer->nextPendingConnection();
182 delete faultyConnection;
183 return;
184 }
185
186 delete m_socket;
187 m_socket = m_tcpServer->nextPendingConnection();
188 m_socket->setParent(this);
189 m_debugServer->setDevice(m_socket);
190}
191
192QQmlDebugServerConnection *QTcpServerConnectionFactory::create(const QString &key)
193{
194 return (key == QLatin1String("QTcpServerConnection") ? new QTcpServerConnection : nullptr);
195}
196
197QT_END_NAMESPACE
198
199#include "qtcpserverconnection.moc"
200

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.cpp