mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2024-12-26 23:41:32 +08:00
Provide API for blocking sendMessage (#154)
This commit is contained in:
parent
afbdb73365
commit
611e48abbb
@ -235,9 +235,10 @@ QString SingleApplication::currentUser() const
|
|||||||
* Sends message to the Primary Instance.
|
* Sends message to the Primary Instance.
|
||||||
* @param message The message to send.
|
* @param message The message to send.
|
||||||
* @param timeout the maximum timeout in milliseconds for blocking functions.
|
* @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.
|
* @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 );
|
Q_D( SingleApplication );
|
||||||
|
|
||||||
@ -248,7 +249,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
|
|||||||
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
|
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return d->writeConfirmedMessage( timeout, message );
|
return d->writeConfirmedMessage( timeout, message, sendMode );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,14 +124,24 @@ public:
|
|||||||
*/
|
*/
|
||||||
QString currentUser() const;
|
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.
|
* @brief Sends a message to the primary instance. Returns true on success.
|
||||||
* @param {int} timeout - Timeout for connecting
|
* @param {int} timeout - Timeout for connecting
|
||||||
|
* @param {SendMode} sendMode - Mode of operation.
|
||||||
* @returns {bool}
|
* @returns {bool}
|
||||||
* @note sendMessage() will return false if invoked from the primary
|
* @note sendMessage() will return false if invoked from the primary
|
||||||
* instance.
|
* 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.
|
* @brief Get the set user data.
|
||||||
|
@ -283,7 +283,7 @@ void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
|
|||||||
sock->putChar('\n');
|
sock->putChar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg)
|
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, SingleApplication::SendMode sendMode)
|
||||||
{
|
{
|
||||||
QElapsedTimer time;
|
QElapsedTimer time;
|
||||||
time.start();
|
time.start();
|
||||||
@ -301,7 +301,13 @@ bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArra
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Frame 2: The message
|
// 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 )
|
bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg )
|
||||||
|
@ -85,7 +85,7 @@ public:
|
|||||||
void readInitMessageBody(QLocalSocket *socket);
|
void readInitMessageBody(QLocalSocket *socket);
|
||||||
void writeAck(QLocalSocket *sock);
|
void writeAck(QLocalSocket *sock);
|
||||||
bool writeConfirmedFrame(int msecs, const QByteArray &msg);
|
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();
|
static void randomSleep();
|
||||||
void addAppData(const QString &data);
|
void addAppData(const QString &data);
|
||||||
QStringList appData() const;
|
QStringList appData() const;
|
||||||
|
Loading…
Reference in New Issue
Block a user