Initial commit: Crown Qt client/server project
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#include "AppInstanceManager.h"
|
||||
#include "Global.h"
|
||||
#include "Utils.h"
|
||||
static int number = Utils::instance().get_number_of_application_name();
|
||||
AppInstanceManager* AppInstanceManager::instance() {
|
||||
static AppInstanceManager manager;
|
||||
return &manager;
|
||||
}
|
||||
|
||||
AppInstanceManager::AppInstanceManager(QObject *parent) : QObject(parent) {
|
||||
m_server = new QLocalServer(this);
|
||||
if (m_server->listen("crown_server_instance" + QString::number(number))) {
|
||||
connect(m_server, &QLocalServer::newConnection, [this]() {
|
||||
QLocalSocket *socket = m_server->nextPendingConnection();
|
||||
connect(socket, &QLocalSocket::readyRead, [socket, this]() {
|
||||
if (socket->readAll() == "ACTIVATE") {
|
||||
emit raiseRequested(); // 触发激活信号
|
||||
}
|
||||
socket->deleteLater();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void AppInstanceManager::notifyActivate() {
|
||||
QLocalSocket socket;
|
||||
socket.connectToServer("crown_server_instance" + QString::number(number));
|
||||
if (socket.waitForConnected(300)) {
|
||||
socket.write("ACTIVATE");
|
||||
socket.waitForBytesWritten();
|
||||
}
|
||||
}
|
||||
|
||||
bool AppInstanceManager::isAlreadyRunning() {
|
||||
QLocalSocket socket;
|
||||
socket.connectToServer("crown_server_instance" + QString::number(number)); // 固定名称,不要用版本号
|
||||
if (socket.waitForConnected(500)) {
|
||||
socket.write("ACTIVATE"); // 发送激活指令
|
||||
socket.waitForBytesWritten();
|
||||
return true; // 已存在实例
|
||||
}
|
||||
return false; // 无其他实例
|
||||
}
|
||||
Reference in New Issue
Block a user