forked from quickshell/quickshell
feat(socket): add unix socket listener
This commit is contained in:
parent
bb5bc0547a
commit
83a0ec6fc6
10 changed files with 479 additions and 0 deletions
77
src/core/socket.cpp
Normal file
77
src/core/socket.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include "socket.hpp"
|
||||
#include <utility>
|
||||
|
||||
#include <qlocalsocket.h>
|
||||
#include <qobject.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
#include "datastream.hpp"
|
||||
|
||||
void Socket::setSocket(QLocalSocket* socket) {
|
||||
if (this->socket != nullptr) this->socket->deleteLater();
|
||||
|
||||
this->socket = socket;
|
||||
socket->setParent(this);
|
||||
|
||||
if (socket != nullptr) {
|
||||
// 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::readyRead, this, &DataStream::onBytesAvailable);
|
||||
// clang-format on
|
||||
|
||||
if (socket->isOpen()) this->onSocketConnected();
|
||||
}
|
||||
}
|
||||
|
||||
QString Socket::path() const { return this->mPath; }
|
||||
|
||||
void Socket::setPath(QString path) {
|
||||
if ((this->connected && !this->disconnecting) || path == this->mPath) return;
|
||||
this->mPath = std::move(path);
|
||||
emit this->pathChanged();
|
||||
|
||||
if (this->targetConnected && this->socket == nullptr) this->connectPathSocket();
|
||||
}
|
||||
|
||||
void Socket::onSocketConnected() {
|
||||
this->connected = true;
|
||||
this->targetConnected = false;
|
||||
this->disconnecting = false;
|
||||
emit this->connectionStateChanged();
|
||||
}
|
||||
|
||||
void Socket::onSocketDisconnected() {
|
||||
this->connected = false;
|
||||
this->disconnecting = false;
|
||||
this->socket->deleteLater();
|
||||
this->socket = nullptr;
|
||||
emit this->connectionStateChanged();
|
||||
|
||||
if (this->targetConnected) this->connectPathSocket();
|
||||
}
|
||||
|
||||
bool Socket::isConnected() const { return this->connected; }
|
||||
|
||||
void Socket::setConnected(bool connected) {
|
||||
this->targetConnected = connected;
|
||||
|
||||
if (!connected) {
|
||||
if (this->socket != nullptr && !this->disconnecting) {
|
||||
this->disconnecting = true;
|
||||
this->socket->disconnectFromServer();
|
||||
}
|
||||
} else if (this->socket == nullptr) this->connectPathSocket();
|
||||
}
|
||||
|
||||
QIODevice* Socket::ioDevice() const { return this->socket; }
|
||||
|
||||
void Socket::connectPathSocket() {
|
||||
if (!this->mPath.isEmpty()) {
|
||||
auto* socket = new QLocalSocket();
|
||||
socket->setServerName(this->mPath);
|
||||
this->setSocket(socket);
|
||||
this->socket->connectToServer(QIODevice::ReadWrite);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue