From 611e48abbbcaea7e59352c277db0ca2c24369eb8 Mon Sep 17 00:00:00 2001 From: Christoph Cullmann Date: Tue, 5 Apr 2022 22:46:42 +0200 Subject: Provide API for blocking sendMessage (#154) --- singleapplication.cpp | 5 +++-- singleapplication.h | 12 +++++++++++- singleapplication_p.cpp | 10 ++++++++-- singleapplication_p.h | 2 +- 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 @@ -124,14 +124,24 @@ 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(msecs - time.elapsed()), msg ); + const bool result = writeConfirmedFrame( static_cast(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; -- cgit v1.2.1