|
| 1 | +#include "ipc.hpp" |
| 2 | +#include <functional> |
| 3 | + |
| 4 | +#include <qlocalserver.h> |
| 5 | +#include <qlocalsocket.h> |
| 6 | +#include <qlogging.h> |
| 7 | +#include <qloggingcategory.h> |
| 8 | +#include <qobject.h> |
| 9 | + |
| 10 | +#include "generation.hpp" |
| 11 | +#include "paths.hpp" |
| 12 | + |
| 13 | +namespace qs::ipc { |
| 14 | + |
| 15 | +Q_LOGGING_CATEGORY(logIpc, "quickshell.ipc", QtWarningMsg); |
| 16 | + |
| 17 | +IpcServer::IpcServer(const QString& path) { |
| 18 | + QObject::connect(&this->server, &QLocalServer::newConnection, this, &IpcServer::onNewConnection); |
| 19 | + |
| 20 | + QLocalServer::removeServer(path); |
| 21 | + |
| 22 | + if (!this->server.listen(path)) { |
| 23 | + qCCritical(logIpc) << "Failed to start IPC server on path" << path; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + qCInfo(logIpc) << "Started IPC server on path" << path; |
| 28 | +} |
| 29 | + |
| 30 | +void IpcServer::start() { |
| 31 | + if (auto* run = QsPaths::instance()->instanceRunDir()) { |
| 32 | + auto path = run->filePath("ipc.sock"); |
| 33 | + new IpcServer(path); |
| 34 | + } else { |
| 35 | + qCCritical(logIpc |
| 36 | + ) << "Could not start IPC server as the instance runtime path could not be created."; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void IpcServer::onNewConnection() { |
| 41 | + while (auto* connection = this->server.nextPendingConnection()) { |
| 42 | + new IpcServerConnection(connection, this); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +IpcServerConnection::IpcServerConnection(QLocalSocket* socket, IpcServer* server) |
| 47 | + : QObject(server) |
| 48 | + , socket(socket) { |
| 49 | + socket->setParent(this); |
| 50 | + this->stream.setDevice(socket); |
| 51 | + QObject::connect(socket, &QLocalSocket::disconnected, this, &IpcServerConnection::onDisconnected); |
| 52 | + QObject::connect(socket, &QLocalSocket::readyRead, this, &IpcServerConnection::onReadyRead); |
| 53 | + |
| 54 | + qCInfo(logIpc) << "New IPC connection" << this; |
| 55 | +} |
| 56 | + |
| 57 | +void IpcServerConnection::onDisconnected() { |
| 58 | + qCInfo(logIpc) << "IPC connection disconnected" << this; |
| 59 | +} |
| 60 | + |
| 61 | +void IpcServerConnection::onReadyRead() { |
| 62 | + this->stream.startTransaction(); |
| 63 | + |
| 64 | + this->stream.startTransaction(); |
| 65 | + auto command = IpcCommand::Unknown; |
| 66 | + this->stream >> command; |
| 67 | + if (!this->stream.commitTransaction()) return; |
| 68 | + |
| 69 | + switch (command) { |
| 70 | + case IpcCommand::Kill: |
| 71 | + qInfo() << "Exiting due to IPC request."; |
| 72 | + EngineGeneration::currentGeneration()->quit(); |
| 73 | + break; |
| 74 | + default: |
| 75 | + qCCritical(logIpc) << "Received invalid IPC command from" << this; |
| 76 | + this->socket->disconnectFromServer(); |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + if (!this->stream.commitTransaction()) return; |
| 81 | +} |
| 82 | + |
| 83 | +IpcClient::IpcClient(const QString& path) { |
| 84 | + QObject::connect(&this->socket, &QLocalSocket::connected, this, &IpcClient::connected); |
| 85 | + QObject::connect(&this->socket, &QLocalSocket::disconnected, this, &IpcClient::disconnected); |
| 86 | + QObject::connect(&this->socket, &QLocalSocket::errorOccurred, this, &IpcClient::onError); |
| 87 | + |
| 88 | + this->socket.connectToServer(path); |
| 89 | + this->stream.setDevice(&this->socket); |
| 90 | +} |
| 91 | + |
| 92 | +bool IpcClient::isConnected() const { return this->socket.isValid(); } |
| 93 | + |
| 94 | +void IpcClient::waitForConnected() { this->socket.waitForConnected(); } |
| 95 | +void IpcClient::waitForDisconnected() { this->socket.waitForDisconnected(); } |
| 96 | + |
| 97 | +void IpcClient::kill() { |
| 98 | + qCDebug(logIpc) << "Sending kill command..."; |
| 99 | + this->stream << IpcCommand::Kill; |
| 100 | + this->socket.flush(); |
| 101 | +} |
| 102 | + |
| 103 | +void IpcClient::onError(QLocalSocket::LocalSocketError error) { |
| 104 | + qCCritical(logIpc) << "Socket Error" << error; |
| 105 | +} |
| 106 | + |
| 107 | +bool IpcClient::connect(const QString& id, const std::function<void(IpcClient& client)>& callback) { |
| 108 | + auto path = QsPaths::ipcPath(id); |
| 109 | + auto client = IpcClient(path); |
| 110 | + qCDebug(logIpc) << "Connecting to instance" << id << "at" << path; |
| 111 | + |
| 112 | + client.waitForConnected(); |
| 113 | + if (!client.isConnected()) return false; |
| 114 | + qCDebug(logIpc) << "Connected."; |
| 115 | + |
| 116 | + callback(client); |
| 117 | + return true; |
| 118 | +} |
| 119 | +} // namespace qs::ipc |
0 commit comments