Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
59 lines
1.3 KiB
C++
59 lines
1.3 KiB
C++
// This file is part of Desktop App Toolkit,
|
|
// a set of libraries for developing nice desktop applications.
|
|
//
|
|
// For license and copyright information please follow this link:
|
|
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
|
//
|
|
#pragma once
|
|
|
|
#include "base/file_lock.h"
|
|
|
|
#include <QtNetwork/QLocalServer>
|
|
#include <QtNetwork/QLocalSocket>
|
|
|
|
namespace base {
|
|
|
|
class SingleInstance final {
|
|
public:
|
|
struct Message {
|
|
uint32 id = 0;
|
|
QByteArray data;
|
|
};
|
|
|
|
SingleInstance();
|
|
~SingleInstance();
|
|
|
|
void start(
|
|
const QString &uniqueApplicationName,
|
|
const QString &path,
|
|
Fn<void()> primary,
|
|
Fn<void()> secondary,
|
|
Fn<void()> fail);
|
|
|
|
void send(const QByteArray &command, Fn<void()> done);
|
|
[[nodiscard]] rpl::producer<Message> commands() const;
|
|
void reply(uint32 commandId, QWidget *activate = nullptr);
|
|
|
|
private:
|
|
void clearSocket();
|
|
void clearLock();
|
|
[[nodiscard]] bool closeExisting();
|
|
|
|
void newInstanceConnected();
|
|
void readClient(not_null<QLocalSocket*> client);
|
|
void removeClient(not_null<QLocalSocket*> client);
|
|
|
|
QString _name;
|
|
QLocalServer _server;
|
|
QLocalSocket _socket;
|
|
uint32 _lastMessageId = 0;
|
|
base::flat_map<not_null<QLocalSocket*>, Message> _clients;
|
|
rpl::event_stream<Message> _commands;
|
|
|
|
QFile _lockFile;
|
|
FileLock _lock;
|
|
|
|
};
|
|
|
|
} // namespace base
|