| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/qqmldebugserverconnection_p.h> |
| 5 | |
| 6 | #include <QtCore/qplugin.h> |
| 7 | #include <QtNetwork/qlocalsocket.h> |
| 8 | #include <private/qqmldebugserver_p.h> |
| 9 | |
| 10 | Q_DECLARE_METATYPE(QLocalSocket::LocalSocketError) |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | |
| 15 | class QLocalClientConnection : public QQmlDebugServerConnection |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | Q_DISABLE_COPY(QLocalClientConnection) |
| 19 | |
| 20 | public: |
| 21 | QLocalClientConnection(); |
| 22 | ~QLocalClientConnection() override; |
| 23 | |
| 24 | void setServer(QQmlDebugServer *server) override; |
| 25 | bool setPortRange(int portFrom, int portTo, bool block, const QString &hostaddress) override; |
| 26 | bool setFileName(const QString &filename, bool block) override; |
| 27 | |
| 28 | bool isConnected() const override; |
| 29 | void disconnect() override; |
| 30 | |
| 31 | void waitForConnection() override; |
| 32 | void flush() override; |
| 33 | |
| 34 | private: |
| 35 | void connectionEstablished(); |
| 36 | bool connectToServer(); |
| 37 | |
| 38 | bool m_block = false; |
| 39 | QString m_filename; |
| 40 | QLocalSocket *m_socket = nullptr; |
| 41 | QQmlDebugServer *m_debugServer = nullptr; |
| 42 | }; |
| 43 | |
| 44 | QLocalClientConnection::QLocalClientConnection() { } |
| 45 | |
| 46 | QLocalClientConnection::~QLocalClientConnection() |
| 47 | { |
| 48 | if (isConnected()) |
| 49 | disconnect(); |
| 50 | } |
| 51 | |
| 52 | void QLocalClientConnection::setServer(QQmlDebugServer *server) |
| 53 | { |
| 54 | m_debugServer = server; |
| 55 | } |
| 56 | |
| 57 | bool QLocalClientConnection::isConnected() const |
| 58 | { |
| 59 | return m_socket && m_socket->state() == QLocalSocket::ConnectedState; |
| 60 | } |
| 61 | |
| 62 | void QLocalClientConnection::disconnect() |
| 63 | { |
| 64 | while (m_socket && m_socket->bytesToWrite() > 0) |
| 65 | m_socket->waitForBytesWritten(); |
| 66 | |
| 67 | m_socket->deleteLater(); |
| 68 | m_socket = nullptr; |
| 69 | } |
| 70 | |
| 71 | bool QLocalClientConnection::setPortRange(int portFrom, int portTo, bool block, |
| 72 | const QString &hostaddress) |
| 73 | { |
| 74 | Q_UNUSED(portFrom); |
| 75 | Q_UNUSED(portTo); |
| 76 | Q_UNUSED(block); |
| 77 | Q_UNUSED(hostaddress); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool QLocalClientConnection::setFileName(const QString &filename, bool block) |
| 82 | { |
| 83 | m_filename = filename; |
| 84 | m_block = block; |
| 85 | return connectToServer(); |
| 86 | } |
| 87 | |
| 88 | void QLocalClientConnection::waitForConnection() |
| 89 | { |
| 90 | m_socket->waitForConnected(msecs: -1); |
| 91 | } |
| 92 | |
| 93 | bool QLocalClientConnection::connectToServer() |
| 94 | { |
| 95 | m_socket = new QLocalSocket; |
| 96 | m_socket->setParent(this); |
| 97 | connect(sender: m_socket, signal: &QLocalSocket::connected, |
| 98 | context: this, slot: &QLocalClientConnection::connectionEstablished); |
| 99 | connect(sender: m_socket, signal: static_cast<void(QLocalSocket::*)(QLocalSocket::LocalSocketError)>( |
| 100 | &QLocalSocket::errorOccurred), context: m_socket, slot: [this](QLocalSocket::LocalSocketError) { |
| 101 | m_socket->disconnectFromServer(); |
| 102 | m_socket->connectToServer(name: m_filename); |
| 103 | }, type: Qt::QueuedConnection); |
| 104 | |
| 105 | m_socket->connectToServer(name: m_filename); |
| 106 | qDebug(msg: "QML Debugger: Connecting to socket %s..." , m_filename.toLatin1().constData()); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | void QLocalClientConnection::flush() |
| 111 | { |
| 112 | if (m_socket) |
| 113 | m_socket->flush(); |
| 114 | } |
| 115 | |
| 116 | void QLocalClientConnection::connectionEstablished() |
| 117 | { |
| 118 | m_debugServer->setDevice(m_socket); |
| 119 | } |
| 120 | |
| 121 | class QLocalClientConnectionFactory : public QQmlDebugServerConnectionFactory |
| 122 | { |
| 123 | Q_OBJECT |
| 124 | Q_PLUGIN_METADATA(IID QQmlDebugServerConnectionFactory_iid FILE "qlocalclientconnection.json" ) |
| 125 | Q_INTERFACES(QQmlDebugServerConnectionFactory) |
| 126 | public: |
| 127 | QQmlDebugServerConnection *create(const QString &key) override; |
| 128 | }; |
| 129 | |
| 130 | QQmlDebugServerConnection *QLocalClientConnectionFactory::create(const QString &key) |
| 131 | { |
| 132 | return (key == QLatin1String("QLocalClientConnection" ) ? new QLocalClientConnection : nullptr); |
| 133 | } |
| 134 | |
| 135 | QT_END_NAMESPACE |
| 136 | |
| 137 | #include "qlocalclientconnection.moc" |
| 138 | |