|
QtPass 2.0.0
Developer documentation
|
QApplication subclass enforcing single-instance behavior via IPC. More...
#include <singleapplication.h>
Public Slots | |
| void | receiveMessage () |
| Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent. | |
Signals | |
| void | messageAvailable (const QString &message) |
| messageAvailable notification from commandline | |
Public Member Functions | |
| SingleApplication (int &argc, char *argv[], QString uniqueKey) | |
| Construct a SingleApplication with a unique instance key. | |
| auto | isRunning () -> bool |
| Query whether another instance is already running. | |
| auto | sendMessage (const QString &message) -> bool |
| Send a message to the running instance. | |
Private Member Functions | |
| auto | peerIsListening () -> bool |
| Probe whether an instance is actually accepting connections. | |
| void | becomePrimary () |
| Claim the segment and listen; no-op when another instance holds it. | |
| auto | forwardMessage (const QString &message) -> bool |
| Deliver a message over the local socket. | |
Private Attributes | |
| bool | _isRunning |
| true when another instance holds the segment and answers on the socket; this instance should forward and exit. | |
| QString | _uniqueKey |
| Name of both the shared-memory segment and the local socket. | |
| QSharedMemory | sharedMemory |
| The one-byte segment whose existence marks a running instance. | |
| QScopedPointer< QLocalServer > | localServer |
| Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed. | |
Static Private Attributes | |
| static const int | timeout = 1000 |
| Milliseconds to wait for a socket connect or write. | |
| static const int | maxMessageBytes = 64 * 1024 |
| Upper bound for one forwarded message (a command line). | |
| static const int | takeoverProbes = 5 |
| Probes for the winner of a takeover race before giving up on IPC. | |
| static const int | takeoverProbeIntervalMs = 100 |
| Pause between two takeover probes, in milliseconds. | |
QApplication subclass enforcing single-instance behavior via IPC.
Detects whether another instance with the same unique key is running and enables forwarding messages (e.g., command-line arguments) to it.
Definition at line 24 of file singleapplication.h.
| SingleApplication::SingleApplication | ( | int & | argc, |
| char * | argv[], | ||
| QString | uniqueKey ) |
Construct a SingleApplication with a unique instance key.
Attaches to the shared-memory segment named uniqueKey. If an instance answers on the local socket this becomes a secondary and isRunning() turns true; if the segment is stale (left behind by a crash) or absent, this instance claims it and starts listening.
| argc | Reference to program argc forwarded to QApplication. |
| argv | Program argv forwarded to QApplication. |
| uniqueKey | Key used to identify and scope the single instance. |
Definition at line 16 of file singleapplication.cpp.
|
private |
Claim the segment and listen; no-op when another instance holds it.
When the segment is already taken the other launcher is probed a few times (see takeoverProbes) and, if it answers, this becomes a secondary. Otherwise isRunning() stays false and the caller runs without IPC rather than not at all.
Definition at line 82 of file singleapplication.cpp.
|
private |
Deliver a message over the local socket.
| message | Text to send; an empty string is sent as a single NUL so the peer still sees a connection with data. |
Definition at line 122 of file singleapplication.cpp.
| auto SingleApplication::isRunning | ( | ) | -> bool |
Query whether another instance is already running.
Definition at line 62 of file singleapplication.cpp.
|
signal |
messageAvailable notification from commandline
| message | args sent to qtpass executable |
|
private |
Probe whether an instance is actually accepting connections.
Definition at line 142 of file singleapplication.cpp.
|
slot |
Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent.
Each accepted socket is read asynchronously so a peer that connects without sending anything cannot stall the GUI thread; the message is delivered once the peer disconnects and the socket is then deleted.
Definition at line 38 of file singleapplication.cpp.
| auto SingleApplication::sendMessage | ( | const QString & | message | ) | -> bool |
Send a message to the running instance.
When delivery fails the peer is taken to have exited since the constructor probed it: this instance releases its segment and, if it can, becomes the listening instance itself, so isRunning() turns false and the window the caller opens next is reachable by later launches.
| message | Text to deliver (typically forwarded command-line args). |
Definition at line 64 of file singleapplication.cpp.
|
private |
true when another instance holds the segment and answers on the socket; this instance should forward and exit.
Definition at line 103 of file singleapplication.h.
|
private |
Name of both the shared-memory segment and the local socket.
Definition at line 105 of file singleapplication.h.
|
private |
Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed.
Definition at line 112 of file singleapplication.h.
|
staticprivate |
Upper bound for one forwarded message (a command line).
The socket is user-only, but a peer that keeps a connection open and streams data would otherwise grow the per-connection buffer without limit; anything past this is dropped and the peer disconnected.
Definition at line 123 of file singleapplication.h.
|
private |
The one-byte segment whose existence marks a running instance.
Definition at line 107 of file singleapplication.h.
|
staticprivate |
Pause between two takeover probes, in milliseconds.
Definition at line 127 of file singleapplication.h.
|
staticprivate |
Probes for the winner of a takeover race before giving up on IPC.
Definition at line 125 of file singleapplication.h.
|
staticprivate |
Milliseconds to wait for a socket connect or write.
Definition at line 115 of file singleapplication.h.