forked from quickshell/quickshell
		
	io/socketserver: correctly order startup/teardown across generations
Fixes #60
This commit is contained in:
		
							parent
							
								
									95d0af8113
								
							
						
					
					
						commit
						79b2204af8
					
				
					 2 changed files with 25 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -107,7 +107,11 @@ void Socket::flush() {
 | 
			
		|||
 | 
			
		||||
SocketServer::~SocketServer() { this->disableServer(); }
 | 
			
		||||
 | 
			
		||||
void SocketServer::onPostReload() {
 | 
			
		||||
void SocketServer::onReload(QObject* oldInstance) {
 | 
			
		||||
	if (auto* old = qobject_cast<SocketServer*>(oldInstance)) {
 | 
			
		||||
		old->disableServer();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this->postReload = true;
 | 
			
		||||
	if (this->isActivatable()) this->enableServer();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -152,6 +156,8 @@ bool SocketServer::isActivatable() {
 | 
			
		|||
void SocketServer::enableServer() {
 | 
			
		||||
	this->disableServer();
 | 
			
		||||
 | 
			
		||||
	qCDebug(logSocket) << "Enabling socket server" << this << "at" << this->mPath;
 | 
			
		||||
 | 
			
		||||
	this->server = new QLocalServer(this);
 | 
			
		||||
	QObject::connect(
 | 
			
		||||
	    this->server,
 | 
			
		||||
| 
						 | 
				
			
			@ -160,31 +166,38 @@ void SocketServer::enableServer() {
 | 
			
		|||
	    &SocketServer::onNewConnection
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	if (QFile::remove(this->mPath)) {
 | 
			
		||||
		qCWarning(logSocket) << "Deleted existing file at" << this->mPath << "to create socket";
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!this->server->listen(this->mPath)) {
 | 
			
		||||
		qWarning() << "could not start socket server at" << this->mPath;
 | 
			
		||||
		qCWarning(logSocket) << "Could not start socket server at" << this->mPath;
 | 
			
		||||
		this->disableServer();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this->activeTarget = false;
 | 
			
		||||
	this->activePath = this->mPath;
 | 
			
		||||
	emit this->activeStatusChanged();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SocketServer::disableServer() {
 | 
			
		||||
	auto wasActive = this->server != nullptr;
 | 
			
		||||
 | 
			
		||||
	if (this->server != nullptr) {
 | 
			
		||||
	if (wasActive) {
 | 
			
		||||
		qCDebug(logSocket) << "Disabling socket server" << this << "at" << this->activePath;
 | 
			
		||||
		for (auto* socket: this->mSockets) {
 | 
			
		||||
			socket->deleteLater();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		this->mSockets.clear();
 | 
			
		||||
		this->server->close();
 | 
			
		||||
		this->server->deleteLater();
 | 
			
		||||
		this->server = nullptr;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (this->mPath != nullptr) {
 | 
			
		||||
		if (QFile::exists(this->mPath) && !QFile::remove(this->mPath)) {
 | 
			
		||||
			qWarning() << "failed to delete socket file at" << this->mPath;
 | 
			
		||||
		if (!this->activePath.isEmpty()) {
 | 
			
		||||
			if (QFile::exists(this->activePath) && !QFile::remove(this->activePath)) {
 | 
			
		||||
				qWarning() << "Failed to delete socket file at" << this->activePath;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <qcontainerfwd.h>
 | 
			
		||||
#include <qlocalserver.h>
 | 
			
		||||
#include <qlocalsocket.h>
 | 
			
		||||
#include <qloggingcategory.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -90,9 +91,7 @@ private:
 | 
			
		|||
///   }
 | 
			
		||||
/// }
 | 
			
		||||
/// ```
 | 
			
		||||
class SocketServer
 | 
			
		||||
    : public QObject
 | 
			
		||||
    , public PostReloadHook {
 | 
			
		||||
class SocketServer: public Reloadable {
 | 
			
		||||
	Q_OBJECT;
 | 
			
		||||
	/// If the socket server is currently active. Defaults to false.
 | 
			
		||||
	///
 | 
			
		||||
| 
						 | 
				
			
			@ -115,11 +114,11 @@ class SocketServer
 | 
			
		|||
	QML_ELEMENT;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
	explicit SocketServer(QObject* parent = nullptr): QObject(parent) {}
 | 
			
		||||
	explicit SocketServer(QObject* parent = nullptr): Reloadable(parent) {}
 | 
			
		||||
	~SocketServer() override;
 | 
			
		||||
	Q_DISABLE_COPY_MOVE(SocketServer);
 | 
			
		||||
 | 
			
		||||
	void onPostReload() override;
 | 
			
		||||
	void onReload(QObject* oldInstance) override;
 | 
			
		||||
 | 
			
		||||
	[[nodiscard]] bool isActive() const;
 | 
			
		||||
	void setActive(bool active);
 | 
			
		||||
| 
						 | 
				
			
			@ -149,4 +148,5 @@ private:
 | 
			
		|||
	bool activeTarget = false;
 | 
			
		||||
	bool postReload = false;
 | 
			
		||||
	QString mPath;
 | 
			
		||||
	QString activePath;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue