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(); }
 | 
					SocketServer::~SocketServer() { this->disableServer(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void SocketServer::onPostReload() {
 | 
					void SocketServer::onReload(QObject* oldInstance) {
 | 
				
			||||||
 | 
						if (auto* old = qobject_cast<SocketServer*>(oldInstance)) {
 | 
				
			||||||
 | 
							old->disableServer();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this->postReload = true;
 | 
						this->postReload = true;
 | 
				
			||||||
	if (this->isActivatable()) this->enableServer();
 | 
						if (this->isActivatable()) this->enableServer();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -152,6 +156,8 @@ bool SocketServer::isActivatable() {
 | 
				
			||||||
void SocketServer::enableServer() {
 | 
					void SocketServer::enableServer() {
 | 
				
			||||||
	this->disableServer();
 | 
						this->disableServer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						qCDebug(logSocket) << "Enabling socket server" << this << "at" << this->mPath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this->server = new QLocalServer(this);
 | 
						this->server = new QLocalServer(this);
 | 
				
			||||||
	QObject::connect(
 | 
						QObject::connect(
 | 
				
			||||||
	    this->server,
 | 
						    this->server,
 | 
				
			||||||
| 
						 | 
					@ -160,31 +166,38 @@ void SocketServer::enableServer() {
 | 
				
			||||||
	    &SocketServer::onNewConnection
 | 
						    &SocketServer::onNewConnection
 | 
				
			||||||
	);
 | 
						);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (QFile::remove(this->mPath)) {
 | 
				
			||||||
 | 
							qCWarning(logSocket) << "Deleted existing file at" << this->mPath << "to create socket";
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!this->server->listen(this->mPath)) {
 | 
						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->disableServer();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this->activeTarget = false;
 | 
						this->activeTarget = false;
 | 
				
			||||||
 | 
						this->activePath = this->mPath;
 | 
				
			||||||
	emit this->activeStatusChanged();
 | 
						emit this->activeStatusChanged();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void SocketServer::disableServer() {
 | 
					void SocketServer::disableServer() {
 | 
				
			||||||
	auto wasActive = this->server != nullptr;
 | 
						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) {
 | 
							for (auto* socket: this->mSockets) {
 | 
				
			||||||
			socket->deleteLater();
 | 
								socket->deleteLater();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this->mSockets.clear();
 | 
							this->mSockets.clear();
 | 
				
			||||||
 | 
							this->server->close();
 | 
				
			||||||
		this->server->deleteLater();
 | 
							this->server->deleteLater();
 | 
				
			||||||
		this->server = nullptr;
 | 
							this->server = nullptr;
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (this->mPath != nullptr) {
 | 
							if (!this->activePath.isEmpty()) {
 | 
				
			||||||
		if (QFile::exists(this->mPath) && !QFile::remove(this->mPath)) {
 | 
								if (QFile::exists(this->activePath) && !QFile::remove(this->activePath)) {
 | 
				
			||||||
			qWarning() << "failed to delete socket file at" << this->mPath;
 | 
									qWarning() << "Failed to delete socket file at" << this->activePath;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <qcontainerfwd.h>
 | 
				
			||||||
#include <qlocalserver.h>
 | 
					#include <qlocalserver.h>
 | 
				
			||||||
#include <qlocalsocket.h>
 | 
					#include <qlocalsocket.h>
 | 
				
			||||||
#include <qloggingcategory.h>
 | 
					#include <qloggingcategory.h>
 | 
				
			||||||
| 
						 | 
					@ -90,9 +91,7 @@ private:
 | 
				
			||||||
///   }
 | 
					///   }
 | 
				
			||||||
/// }
 | 
					/// }
 | 
				
			||||||
/// ```
 | 
					/// ```
 | 
				
			||||||
class SocketServer
 | 
					class SocketServer: public Reloadable {
 | 
				
			||||||
    : public QObject
 | 
					 | 
				
			||||||
    , public PostReloadHook {
 | 
					 | 
				
			||||||
	Q_OBJECT;
 | 
						Q_OBJECT;
 | 
				
			||||||
	/// If the socket server is currently active. Defaults to false.
 | 
						/// If the socket server is currently active. Defaults to false.
 | 
				
			||||||
	///
 | 
						///
 | 
				
			||||||
| 
						 | 
					@ -115,11 +114,11 @@ class SocketServer
 | 
				
			||||||
	QML_ELEMENT;
 | 
						QML_ELEMENT;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
	explicit SocketServer(QObject* parent = nullptr): QObject(parent) {}
 | 
						explicit SocketServer(QObject* parent = nullptr): Reloadable(parent) {}
 | 
				
			||||||
	~SocketServer() override;
 | 
						~SocketServer() override;
 | 
				
			||||||
	Q_DISABLE_COPY_MOVE(SocketServer);
 | 
						Q_DISABLE_COPY_MOVE(SocketServer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void onPostReload() override;
 | 
						void onReload(QObject* oldInstance) override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] bool isActive() const;
 | 
						[[nodiscard]] bool isActive() const;
 | 
				
			||||||
	void setActive(bool active);
 | 
						void setActive(bool active);
 | 
				
			||||||
| 
						 | 
					@ -149,4 +148,5 @@ private:
 | 
				
			||||||
	bool activeTarget = false;
 | 
						bool activeTarget = false;
 | 
				
			||||||
	bool postReload = false;
 | 
						bool postReload = false;
 | 
				
			||||||
	QString mPath;
 | 
						QString mPath;
 | 
				
			||||||
 | 
						QString activePath;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue