Commit c747ee47 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

create a nicer api for blocking

don't use bool, use some enum
parent a00d153c
Pipeline #159207 passed with stage
in 5 minutes and 38 seconds
......@@ -567,7 +567,9 @@ int main(int argc, char **argv)
/**
* try to send message, return success
*/
return !app.sendMessage(QJsonDocument::fromVariant(QVariant(message)).toJson(), 1000, needToBlock);
return !app.sendMessage(QJsonDocument::fromVariant(QVariant(message)).toJson(),
1000,
needToBlock ? SingleApplication::BlockingSend : SingleApplication::SendOptions());
}
/**
......
......@@ -238,7 +238,7 @@ QString SingleApplication::currentUser() const
* @param block block until the primary application exits
* @return true if the message was sent successfuly, false otherwise.
*/
bool SingleApplication::sendMessage( const QByteArray &message, int timeout, bool block )
bool SingleApplication::sendMessage( const QByteArray &message, int timeout, SendOptions options )
{
Q_D( SingleApplication );
......@@ -249,7 +249,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout, boo
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
return false;
return d->writeConfirmedMessage( timeout, message, block );
return d->writeConfirmedMessage( timeout, message, options );
}
/**
......
......@@ -124,6 +124,18 @@ public:
*/
QString currentUser() const;
/**
* @brief Mode of operation of sendMessage.
* @enum
*/
enum SendMode {
/**
* Should the sendMessage call block until the primary instance is terminated?
*/
BlockingSend = 1 << 0,
};
Q_DECLARE_FLAGS(SendOptions, SendMode)
/**
* @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting
......@@ -132,7 +144,7 @@ public:
* @note sendMessage() will return false if invoked from the primary
* instance.
*/
bool sendMessage( const QByteArray &message, int timeout = 100, bool block = false );
bool sendMessage( const QByteArray &message, int timeout = 100, SendOptions options = {} );
/**
* @brief Get the set user data.
......
......@@ -283,7 +283,7 @@ void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
sock->putChar('\n');
}
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, bool block)
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, SingleApplication::SendOptions options)
{
QElapsedTimer time;
time.start();
......@@ -304,7 +304,7 @@ bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArra
const bool result = writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
// block if needed
if (socket && block)
if (socket && (options & SingleApplication::BlockingSend))
socket->waitForDisconnected(-1);
return result;
......
......@@ -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 block = false);
bool writeConfirmedMessage(int msecs, const QByteArray &msg, SingleApplication::SendOptions options = {});
static void randomSleep();
void addAppData(const QString &data);
QStringList appData() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment