From 23d0c2e01d1061db7adfb8ca521b0114194f16d1 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 18 Apr 2024 04:13:20 -0700 Subject: [PATCH] io/socket: add flush() --- src/io/socket.cpp | 18 +++++++++++++++++- src/io/socket.hpp | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/io/socket.cpp b/src/io/socket.cpp index f5e0b538..a102c7b1 100644 --- a/src/io/socket.cpp +++ b/src/io/socket.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -12,6 +13,8 @@ #include "datastream.hpp" +Q_LOGGING_CATEGORY(logSocket, "quickshell.io.socket", QtWarningMsg); + void Socket::setSocket(QLocalSocket* socket) { if (this->socket != nullptr) this->socket->deleteLater(); this->socket = socket; @@ -22,7 +25,7 @@ void Socket::setSocket(QLocalSocket* socket) { // clang-format off QObject::connect(this->socket, &QLocalSocket::connected, this, &Socket::onSocketConnected); QObject::connect(this->socket, &QLocalSocket::disconnected, this, &Socket::onSocketDisconnected); - QObject::connect(this->socket, &QLocalSocket::errorOccurred, this, &Socket::error); + QObject::connect(this->socket, &QLocalSocket::errorOccurred, this, &Socket::onSocketError); QObject::connect(this->socket, &QLocalSocket::readyRead, this, &DataStream::onBytesAvailable); // clang-format on @@ -45,10 +48,12 @@ void Socket::onSocketConnected() { this->connected = true; this->targetConnected = false; this->disconnecting = false; + qCDebug(logSocket) << "Socket connected:" << this; emit this->connectionStateChanged(); } void Socket::onSocketDisconnected() { + qCDebug(logSocket) << "Socket disconnected:" << this; this->connected = false; this->disconnecting = false; this->socket->deleteLater(); @@ -59,6 +64,11 @@ void Socket::onSocketDisconnected() { if (this->targetConnected) this->connectPathSocket(); } +void Socket::onSocketError(QLocalSocket::LocalSocketError error) { + qCWarning(logSocket) << "Socket error for" << this << error; + emit this->error(error); +} + bool Socket::isConnected() const { return this->connected; } void Socket::setConnected(bool connected) { @@ -89,6 +99,12 @@ void Socket::write(const QString& data) { } } +void Socket::flush() { + if (this->socket != nullptr) { + this->socket->flush(); + } +} + SocketServer::~SocketServer() { this->disableServer(); } void SocketServer::onPostReload() { diff --git a/src/io/socket.hpp b/src/io/socket.hpp index 25b8a1df..75902441 100644 --- a/src/io/socket.hpp +++ b/src/io/socket.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -11,6 +12,8 @@ #include "../core/reload.hpp" #include "datastream.hpp" +Q_DECLARE_LOGGING_CATEGORY(logSocket); + ///! Unix socket listener. class Socket: public DataStream { Q_OBJECT; @@ -30,8 +33,13 @@ public: explicit Socket(QObject* parent = nullptr): DataStream(parent) {} /// Write data to the socket. Does nothing if not connected. + /// + /// Remember to call flush after your last write. Q_INVOKABLE void write(const QString& data); + /// Flush any queued writes to the socket. + Q_INVOKABLE void flush(); + // takes ownership void setSocket(QLocalSocket* socket); @@ -54,6 +62,7 @@ protected: private slots: void onSocketConnected(); void onSocketDisconnected(); + void onSocketError(QLocalSocket::LocalSocketError error); private: void connectPathSocket();