mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 07:15:35 +08:00
49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
// SPDX-FileCopyrightText: Copyright (C) 2020 swift Project Community / Contributors
|
|
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
|
|
|
|
//! \file
|
|
|
|
#include "blackmisc/sharedstate/dbus/duplex.h"
|
|
|
|
namespace BlackMisc::SharedState::DBus
|
|
{
|
|
IDuplex::IDuplex(QObject *parent) : QObject(parent)
|
|
{
|
|
connect(this, &IDuplex::replyReceived, this, [this](const QString &, const CVariant ¶m, quint32 token) {
|
|
const auto it = m_submittedRequests.find(token);
|
|
if (it == m_submittedRequests.end()) { return; }
|
|
it->setResult(param);
|
|
m_submittedRequests.erase(it);
|
|
});
|
|
}
|
|
|
|
QFuture<CVariant> IDuplex::submitRequest(const QString &channel, const CVariant ¶m)
|
|
{
|
|
const auto token = getToken();
|
|
auto future = m_submittedRequests.insert(token, {})->future();
|
|
submitRequest(channel, param, token);
|
|
return future;
|
|
}
|
|
|
|
QFuture<CVariant> IDuplex::receiveRequest(const QString &channel, const BlackMisc::CVariant ¶m)
|
|
{
|
|
const auto token = getToken();
|
|
auto future = m_receivedRequests.insert(token, {})->future();
|
|
emit requestReceived(channel, param, token, QPrivateSignal {});
|
|
return future;
|
|
}
|
|
|
|
void IDuplex::reply(const BlackMisc::CVariant ¶m, quint32 token)
|
|
{
|
|
const auto it = m_receivedRequests.find(token);
|
|
if (it == m_receivedRequests.end()) { return; }
|
|
it->setResult(param);
|
|
m_receivedRequests.erase(it);
|
|
}
|
|
|
|
quint32 IDuplex::getToken()
|
|
{
|
|
return m_token++;
|
|
}
|
|
}
|