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 "qlocalclientconnectionfactory.h"
41
42#include <QtCore/qplugin.h>
43#include <QtNetwork/qlocalsocket.h>
44#include <private/qqmldebugserver_p.h>
45
46Q_DECLARE_METATYPE(QLocalSocket::LocalSocketError)
47
48QT_BEGIN_NAMESPACE
49
50
51class QLocalClientConnection : public QQmlDebugServerConnection
52{
53 Q_OBJECT
54 Q_DISABLE_COPY(QLocalClientConnection)
55
56public:
57 QLocalClientConnection();
58 ~QLocalClientConnection() override;
59
60 void setServer(QQmlDebugServer *server) override;
61 bool setPortRange(int portFrom, int portTo, bool block, const QString &hostaddress) override;
62 bool setFileName(const QString &filename, bool block) override;
63
64 bool isConnected() const override;
65 void disconnect() override;
66
67 void waitForConnection() override;
68 void flush() override;
69
70private:
71 void connectionEstablished();
72 bool connectToServer();
73
74 bool m_block = false;
75 QString m_filename;
76 QLocalSocket *m_socket = nullptr;
77 QQmlDebugServer *m_debugServer = nullptr;
78};
79
80QLocalClientConnection::QLocalClientConnection() { }
81
82QLocalClientConnection::~QLocalClientConnection()
83{
84 if (isConnected())
85 disconnect();
86}
87
88void QLocalClientConnection::setServer(QQmlDebugServer *server)
89{
90 m_debugServer = server;
91}
92
93bool QLocalClientConnection::isConnected() const
94{
95 return m_socket && m_socket->state() == QLocalSocket::ConnectedState;
96}
97
98void QLocalClientConnection::disconnect()
99{
100 while (m_socket && m_socket->bytesToWrite() > 0)
101 m_socket->waitForBytesWritten();
102
103 m_socket->deleteLater();
104 m_socket = nullptr;
105}
106
107bool QLocalClientConnection::setPortRange(int portFrom, int portTo, bool block,
108 const QString &hostaddress)
109{
110 Q_UNUSED(portFrom);
111 Q_UNUSED(portTo);
112 Q_UNUSED(block);
113 Q_UNUSED(hostaddress);
114 return false;
115}
116
117bool QLocalClientConnection::setFileName(const QString &filename, bool block)
118{
119 m_filename = filename;
120 m_block = block;
121 return connectToServer();
122}
123
124void QLocalClientConnection::waitForConnection()
125{
126 m_socket->waitForConnected(msecs: -1);
127}
128
129bool QLocalClientConnection::connectToServer()
130{
131 m_socket = new QLocalSocket;
132 m_socket->setParent(this);
133 connect(sender: m_socket, signal: &QLocalSocket::connected,
134 receiver: this, slot: &QLocalClientConnection::connectionEstablished);
135 connect(sender: m_socket, signal: static_cast<void(QLocalSocket::*)(QLocalSocket::LocalSocketError)>(
136 &QLocalSocket::errorOccurred), context: m_socket, slot: [this](QLocalSocket::LocalSocketError) {
137 m_socket->disconnectFromServer();
138 m_socket->connectToServer(name: m_filename);
139 }, type: Qt::QueuedConnection);
140
141 m_socket->connectToServer(name: m_filename);
142 qDebug(msg: "QML Debugger: Connecting to socket %s...", m_filename.toLatin1().constData());
143 return true;
144}
145
146void QLocalClientConnection::flush()
147{
148 if (m_socket)
149 m_socket->flush();
150}
151
152void QLocalClientConnection::connectionEstablished()
153{
154 m_debugServer->setDevice(m_socket);
155}
156
157QQmlDebugServerConnection *QLocalClientConnectionFactory::create(const QString &key)
158{
159 return (key == QLatin1String("QLocalClientConnection") ? new QLocalClientConnection : nullptr);
160}
161
162QT_END_NAMESPACE
163
164#include "qlocalclientconnection.moc"
165

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.cpp