forked from quickshell/quickshell
io/socket: add flush()
This commit is contained in:
parent
a06af243ad
commit
23d0c2e01d
|
@ -5,6 +5,7 @@
|
|||
#include <qlocalserver.h>
|
||||
#include <qlocalsocket.h>
|
||||
#include <qlogging.h>
|
||||
#include <qloggingcategory.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlcomponent.h>
|
||||
#include <qqmlengine.h>
|
||||
|
@ -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() {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <qlocalserver.h>
|
||||
#include <qlocalsocket.h>
|
||||
#include <qloggingcategory.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlcomponent.h>
|
||||
#include <qqmlintegration.h>
|
||||
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue