aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Cullmann <christoph@cullmann.io>2022-04-05 22:46:42 +0200
committerGitHub <noreply@github.com>2022-04-05 23:46:42 +0300
commit611e48abbbcaea7e59352c277db0ca2c24369eb8 (patch)
treeaf45f4ddced1b25c125ce2a440a830c8fe9dcef0
parentMerge pull request #153 from itay-grudev/lts-only (diff)
downloadsingleapplication-611e48abbbcaea7e59352c277db0ca2c24369eb8.tar.xz
Provide API for blocking sendMessage (#154)
-rw-r--r--singleapplication.cpp5
-rw-r--r--singleapplication.h12
-rw-r--r--singleapplication_p.cpp10
-rw-r--r--singleapplication_p.h2
4 files changed, 23 insertions, 6 deletions
diff --git a/singleapplication.cpp b/singleapplication.cpp
index 09e264e..1234ff9 100644
--- a/singleapplication.cpp
+++ b/singleapplication.cpp
@@ -235,9 +235,10 @@ QString SingleApplication::currentUser() const
* Sends message to the Primary Instance.
* @param message The message to send.
* @param timeout the maximum timeout in milliseconds for blocking functions.
+ * @param sendMode mode of operation
* @return true if the message was sent successfuly, false otherwise.
*/
-bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
+bool SingleApplication::sendMessage( const QByteArray &message, int timeout, SendMode sendMode )
{
Q_D( SingleApplication );
@@ -248,7 +249,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
return false;
- return d->writeConfirmedMessage( timeout, message );
+ return d->writeConfirmedMessage( timeout, message, sendMode );
}
/**
diff --git a/singleapplication.h b/singleapplication.h
index 91cabf9..5565bb8 100644
--- a/singleapplication.h
+++ b/singleapplication.h
@@ -125,13 +125,23 @@ public:
QString currentUser() const;
/**
+ * @brief Mode of operation of sendMessage.
+ * @enum
+ */
+ enum SendMode {
+ NonBlocking, /** Do not wait for the primary instance termination and return immediately */
+ BlockUntilPrimaryExit, /** Wait until the primary instance is terminated */
+ };
+
+ /**
* @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting
+ * @param {SendMode} sendMode - Mode of operation.
* @returns {bool}
* @note sendMessage() will return false if invoked from the primary
* instance.
*/
- bool sendMessage( const QByteArray &message, int timeout = 100 );
+ bool sendMessage( const QByteArray &message, int timeout = 100, SendMode sendMode = NonBlocking );
/**
* @brief Get the set user data.
diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp
index a037f71..2a369bc 100644
--- a/singleapplication_p.cpp
+++ b/singleapplication_p.cpp
@@ -283,7 +283,7 @@ void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
sock->putChar('\n');
}
-bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg)
+bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, SingleApplication::SendMode sendMode)
{
QElapsedTimer time;
time.start();
@@ -301,7 +301,13 @@ bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArra
return false;
// Frame 2: The message
- return writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
+ const bool result = writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
+
+ // Block if needed
+ if (socket && sendMode == SingleApplication::BlockUntilPrimaryExit)
+ socket->waitForDisconnected(-1);
+
+ return result;
}
bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg )
diff --git a/singleapplication_p.h b/singleapplication_p.h
index 58507cf..30a2b51 100644
--- a/singleapplication_p.h
+++ b/singleapplication_p.h
@@ -85,7 +85,7 @@ public:
void readInitMessageBody(QLocalSocket *socket);
void writeAck(QLocalSocket *sock);
bool writeConfirmedFrame(int msecs, const QByteArray &msg);
- bool writeConfirmedMessage(int msecs, const QByteArray &msg);
+ bool writeConfirmedMessage(int msecs, const QByteArray &msg, SingleApplication::SendMode sendMode = SingleApplication::NonBlocking);
static void randomSleep();
void addAppData(const QString &data);
QStringList appData() const;