| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Ford Motor Company |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtRemoteObjects 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 "qconnection_local_backend_p.h" |
| 41 | |
| 42 | QT_BEGIN_NAMESPACE |
| 43 | |
| 44 | LocalClientIo::LocalClientIo(QObject *parent) |
| 45 | : ClientIoDevice(parent) |
| 46 | , m_socket(new QLocalSocket(this)) |
| 47 | { |
| 48 | connect(sender: m_socket, signal: &QLocalSocket::readyRead, receiver: this, slot: &ClientIoDevice::readyRead); |
| 49 | connect(sender: m_socket, signal: &QLocalSocket::errorOccurred, receiver: this, slot: &LocalClientIo::onError); |
| 50 | connect(sender: m_socket, signal: &QLocalSocket::stateChanged, receiver: this, slot: &LocalClientIo::onStateChanged); |
| 51 | } |
| 52 | |
| 53 | LocalClientIo::~LocalClientIo() |
| 54 | { |
| 55 | close(); |
| 56 | } |
| 57 | |
| 58 | QIODevice *LocalClientIo::connection() const |
| 59 | { |
| 60 | return m_socket; |
| 61 | } |
| 62 | |
| 63 | void LocalClientIo::doClose() |
| 64 | { |
| 65 | if (m_socket->isOpen()) { |
| 66 | connect(sender: m_socket, signal: &QLocalSocket::disconnected, receiver: this, slot: &QObject::deleteLater); |
| 67 | m_socket->disconnectFromServer(); |
| 68 | } else { |
| 69 | this->deleteLater(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void LocalClientIo::doDisconnectFromServer() |
| 74 | { |
| 75 | m_socket->disconnectFromServer(); |
| 76 | } |
| 77 | |
| 78 | void LocalClientIo::connectToServer() |
| 79 | { |
| 80 | if (!isOpen()) |
| 81 | m_socket->connectToServer(name: url().path()); |
| 82 | } |
| 83 | |
| 84 | bool LocalClientIo::isOpen() const |
| 85 | { |
| 86 | return !isClosing() && (m_socket->state() == QLocalSocket::ConnectedState |
| 87 | || m_socket->state() == QLocalSocket::ConnectingState); |
| 88 | } |
| 89 | |
| 90 | void LocalClientIo::onError(QLocalSocket::LocalSocketError error) |
| 91 | { |
| 92 | qCDebug(QT_REMOTEOBJECT) << "onError" << error << m_socket->serverName(); |
| 93 | |
| 94 | switch (error) { |
| 95 | case QLocalSocket::ServerNotFoundError: |
| 96 | case QLocalSocket::UnknownSocketError: |
| 97 | case QLocalSocket::PeerClosedError: |
| 98 | //Host not there, wait and try again |
| 99 | emit shouldReconnect(this); |
| 100 | break; |
| 101 | case QLocalSocket::ConnectionError: |
| 102 | case QLocalSocket::ConnectionRefusedError: |
| 103 | //... TODO error reporting |
| 104 | #ifdef Q_OS_UNIX |
| 105 | emit shouldReconnect(this); |
| 106 | #endif |
| 107 | break; |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void LocalClientIo::onStateChanged(QLocalSocket::LocalSocketState state) |
| 114 | { |
| 115 | if (state == QLocalSocket::ClosingState && !isClosing()) { |
| 116 | m_socket->abort(); |
| 117 | emit shouldReconnect(this); |
| 118 | } |
| 119 | if (state == QLocalSocket::ConnectedState) |
| 120 | initializeDataStream(); |
| 121 | } |
| 122 | |
| 123 | LocalServerIo::LocalServerIo(QLocalSocket *conn, QObject *parent) |
| 124 | : ServerIoDevice(parent), m_connection(conn) |
| 125 | { |
| 126 | m_connection->setParent(this); |
| 127 | connect(sender: conn, signal: &QIODevice::readyRead, receiver: this, slot: &ServerIoDevice::readyRead); |
| 128 | connect(sender: conn, signal: &QLocalSocket::disconnected, receiver: this, slot: &ServerIoDevice::disconnected); |
| 129 | } |
| 130 | |
| 131 | QIODevice *LocalServerIo::connection() const |
| 132 | { |
| 133 | return m_connection; |
| 134 | } |
| 135 | |
| 136 | void LocalServerIo::doClose() |
| 137 | { |
| 138 | m_connection->disconnectFromServer(); |
| 139 | } |
| 140 | |
| 141 | LocalServerImpl::LocalServerImpl(QObject *parent) |
| 142 | : QConnectionAbstractServer(parent) |
| 143 | { |
| 144 | connect(sender: &m_server, signal: &QLocalServer::newConnection, receiver: this, slot: &QConnectionAbstractServer::newConnection); |
| 145 | } |
| 146 | |
| 147 | LocalServerImpl::~LocalServerImpl() |
| 148 | { |
| 149 | m_server.close(); |
| 150 | } |
| 151 | |
| 152 | ServerIoDevice *LocalServerImpl::configureNewConnection() |
| 153 | { |
| 154 | if (!m_server.isListening()) |
| 155 | return nullptr; |
| 156 | |
| 157 | return new LocalServerIo(m_server.nextPendingConnection(), this); |
| 158 | } |
| 159 | |
| 160 | bool LocalServerImpl::hasPendingConnections() const |
| 161 | { |
| 162 | return m_server.hasPendingConnections(); |
| 163 | } |
| 164 | |
| 165 | QUrl LocalServerImpl::address() const |
| 166 | { |
| 167 | QUrl result; |
| 168 | result.setPath(path: m_server.serverName()); |
| 169 | result.setScheme(QRemoteObjectStringLiterals::local()); |
| 170 | |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | bool LocalServerImpl::listen(const QUrl &address) |
| 175 | { |
| 176 | #ifdef Q_OS_UNIX |
| 177 | bool res = m_server.listen(name: address.path()); |
| 178 | if (!res) { |
| 179 | QLocalServer::removeServer(name: address.path()); |
| 180 | res = m_server.listen(name: address.path()); |
| 181 | } |
| 182 | return res; |
| 183 | #else |
| 184 | return m_server.listen(address.path()); |
| 185 | #endif |
| 186 | } |
| 187 | |
| 188 | QAbstractSocket::SocketError LocalServerImpl::serverError() const |
| 189 | { |
| 190 | return m_server.serverError(); |
| 191 | } |
| 192 | |
| 193 | void LocalServerImpl::close() |
| 194 | { |
| 195 | m_server.close(); |
| 196 | } |
| 197 | |
| 198 | QT_END_NAMESPACE |
| 199 | |