|
| 1 | +/* |
| 2 | + * Copyright (C) EdgeTX |
| 3 | + * |
| 4 | + * Based on code named |
| 5 | + * opentx - https://github.com/opentx/opentx |
| 6 | + * th9x - http://code.google.com/p/th9x |
| 7 | + * er9x - http://code.google.com/p/er9x |
| 8 | + * gruvin9x - http://code.google.com/p/gruvin9x |
| 9 | + * |
| 10 | + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html |
| 11 | + * |
| 12 | + * This program is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License version 2 as |
| 14 | + * published by the Free Software Foundation. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "hostserialbackend_localsocket.h" |
| 23 | + |
| 24 | +#include <QLocalServer> |
| 25 | +#include <QLocalSocket> |
| 26 | +#include <QtDebug> |
| 27 | + |
| 28 | +QLocalSocketBackend::QLocalSocketBackend(const QString & socketName, QObject * parent) |
| 29 | + : HostSerialBackend(parent), |
| 30 | + socketName(socketName), |
| 31 | + server(new QLocalServer(this)), |
| 32 | + client(nullptr) |
| 33 | +{ |
| 34 | + connect(server, &QLocalServer::newConnection, |
| 35 | + this, &QLocalSocketBackend::onNewConnection); |
| 36 | +} |
| 37 | + |
| 38 | +QLocalSocketBackend::~QLocalSocketBackend() |
| 39 | +{ |
| 40 | + close(); |
| 41 | +} |
| 42 | + |
| 43 | +bool QLocalSocketBackend::open() |
| 44 | +{ |
| 45 | + if (server->isListening()) |
| 46 | + return true; |
| 47 | + |
| 48 | + // A previous run (or another process) may have left a stale socket |
| 49 | + // file behind on Unix; QLocalServer::removeServer is the documented |
| 50 | + // way to clear it before re-listening. |
| 51 | + QLocalServer::removeServer(socketName); |
| 52 | + |
| 53 | + if (!server->listen(socketName)) { |
| 54 | + emit errorOccurred(tr("Failed to listen on local socket \"%1\": %2") |
| 55 | + .arg(socketName, server->errorString())); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + qDebug() << "Listening on local socket" << server->fullServerName(); |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +void QLocalSocketBackend::close() |
| 64 | +{ |
| 65 | + if (client != nullptr) { |
| 66 | + client->disconnect(this); |
| 67 | + client->disconnectFromServer(); |
| 68 | + client->deleteLater(); |
| 69 | + client = nullptr; |
| 70 | + } |
| 71 | + if (server->isListening()) |
| 72 | + server->close(); |
| 73 | +} |
| 74 | + |
| 75 | +bool QLocalSocketBackend::isOpen() const |
| 76 | +{ |
| 77 | + return server->isListening(); |
| 78 | +} |
| 79 | + |
| 80 | +void QLocalSocketBackend::write(const QByteArray & data) |
| 81 | +{ |
| 82 | + if (client != nullptr && client->state() == QLocalSocket::ConnectedState) |
| 83 | + client->write(data); |
| 84 | +} |
| 85 | + |
| 86 | +QString QLocalSocketBackend::displayName() const |
| 87 | +{ |
| 88 | + return socketName; |
| 89 | +} |
| 90 | + |
| 91 | +QString QLocalSocketBackend::fullServerName() const |
| 92 | +{ |
| 93 | + return server->isListening() ? server->fullServerName() : QString(); |
| 94 | +} |
| 95 | + |
| 96 | +void QLocalSocketBackend::onNewConnection() |
| 97 | +{ |
| 98 | + while (server->hasPendingConnections()) { |
| 99 | + QLocalSocket * incoming = server->nextPendingConnection(); |
| 100 | + if (client != nullptr) { |
| 101 | + // Single-client backend: politely refuse extra connections. |
| 102 | + incoming->disconnectFromServer(); |
| 103 | + incoming->deleteLater(); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + client = incoming; |
| 108 | + connect(client, &QLocalSocket::readyRead, |
| 109 | + this, &QLocalSocketBackend::onClientReadyRead); |
| 110 | + connect(client, &QLocalSocket::disconnected, |
| 111 | + this, &QLocalSocketBackend::onClientDisconnected); |
| 112 | + qDebug() << "Local socket client connected on" << socketName; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void QLocalSocketBackend::onClientReadyRead() |
| 117 | +{ |
| 118 | + if (client == nullptr) |
| 119 | + return; |
| 120 | + emit dataReceived(client->readAll()); |
| 121 | +} |
| 122 | + |
| 123 | +void QLocalSocketBackend::onClientDisconnected() |
| 124 | +{ |
| 125 | + if (client == nullptr) |
| 126 | + return; |
| 127 | + client->deleteLater(); |
| 128 | + client = nullptr; |
| 129 | + qDebug() << "Local socket client disconnected from" << socketName; |
| 130 | +} |
0 commit comments