io/socket: add flush()

This commit is contained in:
outfoxxed 2024-04-18 04:13:20 -07:00
parent a06af243ad
commit 23d0c2e01d
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
2 changed files with 26 additions and 1 deletions

View file

@ -5,6 +5,7 @@
#include <qlocalserver.h> #include <qlocalserver.h>
#include <qlocalsocket.h> #include <qlocalsocket.h>
#include <qlogging.h> #include <qlogging.h>
#include <qloggingcategory.h>
#include <qobject.h> #include <qobject.h>
#include <qqmlcomponent.h> #include <qqmlcomponent.h>
#include <qqmlengine.h> #include <qqmlengine.h>
@ -12,6 +13,8 @@
#include "datastream.hpp" #include "datastream.hpp"
Q_LOGGING_CATEGORY(logSocket, "quickshell.io.socket", QtWarningMsg);
void Socket::setSocket(QLocalSocket* socket) { void Socket::setSocket(QLocalSocket* socket) {
if (this->socket != nullptr) this->socket->deleteLater(); if (this->socket != nullptr) this->socket->deleteLater();
this->socket = socket; this->socket = socket;
@ -22,7 +25,7 @@ void Socket::setSocket(QLocalSocket* socket) {
// clang-format off // clang-format off
QObject::connect(this->socket, &QLocalSocket::connected, this, &Socket::onSocketConnected); QObject::connect(this->socket, &QLocalSocket::connected, this, &Socket::onSocketConnected);
QObject::connect(this->socket, &QLocalSocket::disconnected, this, &Socket::onSocketDisconnected); 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); QObject::connect(this->socket, &QLocalSocket::readyRead, this, &DataStream::onBytesAvailable);
// clang-format on // clang-format on
@ -45,10 +48,12 @@ void Socket::onSocketConnected() {
this->connected = true; this->connected = true;
this->targetConnected = false; this->targetConnected = false;
this->disconnecting = false; this->disconnecting = false;
qCDebug(logSocket) << "Socket connected:" << this;
emit this->connectionStateChanged(); emit this->connectionStateChanged();
} }
void Socket::onSocketDisconnected() { void Socket::onSocketDisconnected() {
qCDebug(logSocket) << "Socket disconnected:" << this;
this->connected = false; this->connected = false;
this->disconnecting = false; this->disconnecting = false;
this->socket->deleteLater(); this->socket->deleteLater();
@ -59,6 +64,11 @@ void Socket::onSocketDisconnected() {
if (this->targetConnected) this->connectPathSocket(); 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; } bool Socket::isConnected() const { return this->connected; }
void Socket::setConnected(bool 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(); } SocketServer::~SocketServer() { this->disableServer(); }
void SocketServer::onPostReload() { void SocketServer::onPostReload() {

View file

@ -2,6 +2,7 @@
#include <qlocalserver.h> #include <qlocalserver.h>
#include <qlocalsocket.h> #include <qlocalsocket.h>
#include <qloggingcategory.h>
#include <qobject.h> #include <qobject.h>
#include <qqmlcomponent.h> #include <qqmlcomponent.h>
#include <qqmlintegration.h> #include <qqmlintegration.h>
@ -11,6 +12,8 @@
#include "../core/reload.hpp" #include "../core/reload.hpp"
#include "datastream.hpp" #include "datastream.hpp"
Q_DECLARE_LOGGING_CATEGORY(logSocket);
///! Unix socket listener. ///! Unix socket listener.
class Socket: public DataStream { class Socket: public DataStream {
Q_OBJECT; Q_OBJECT;
@ -30,8 +33,13 @@ public:
explicit Socket(QObject* parent = nullptr): DataStream(parent) {} explicit Socket(QObject* parent = nullptr): DataStream(parent) {}
/// Write data to the socket. Does nothing if not connected. /// 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); Q_INVOKABLE void write(const QString& data);
/// Flush any queued writes to the socket.
Q_INVOKABLE void flush();
// takes ownership // takes ownership
void setSocket(QLocalSocket* socket); void setSocket(QLocalSocket* socket);
@ -54,6 +62,7 @@ protected:
private slots: private slots:
void onSocketConnected(); void onSocketConnected();
void onSocketDisconnected(); void onSocketDisconnected();
void onSocketError(QLocalSocket::LocalSocketError error);
private: private:
void connectPathSocket(); void connectPathSocket();