2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // int64_t
|
2015-07-13 00:32:48 +00:00
|
|
|
#include <memory>
|
2020-07-28 15:56:40 +00:00
|
|
|
#include <optional>
|
2020-07-27 04:30:58 +00:00
|
|
|
#include <string_view>
|
2023-06-30 19:36:08 +00:00
|
|
|
#include <unordered_map>
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureInterface>
|
2020-07-28 15:56:40 +00:00
|
|
|
#include <QNetworkRequest>
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2022-02-08 03:56:04 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <libtransmission/quark.h>
|
2015-07-13 00:32:48 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
class QNetworkAccessManager;
|
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
using TrVariantPtr = std::shared_ptr<tr_variant>;
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_DECLARE_METATYPE(TrVariantPtr)
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_session;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
struct RpcResponse
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString result;
|
|
|
|
TrVariantPtr args;
|
|
|
|
bool success = false;
|
|
|
|
QNetworkReply::NetworkError networkError = QNetworkReply::NoError;
|
2016-04-19 20:41:59 +00:00
|
|
|
};
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_DECLARE_METATYPE(QFutureInterface<RpcResponse>)
|
2016-04-19 20:41:59 +00:00
|
|
|
|
|
|
|
// The response future -- the RPC engine returns one for each request made.
|
2020-05-20 01:32:51 +00:00
|
|
|
using RpcResponseFuture = QFuture<RpcResponse>;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class RpcClient : public QObject
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(RpcClient)
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
2020-11-01 21:47:57 +00:00
|
|
|
explicit RpcClient(QObject* parent = nullptr);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void stop();
|
|
|
|
void start(tr_session* session);
|
2017-04-20 16:02:19 +00:00
|
|
|
void start(QUrl const& url);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isLocal() const;
|
2017-04-20 16:02:19 +00:00
|
|
|
QUrl const& url() const;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture exec(tr_quark method, tr_variant* args);
|
2020-07-27 04:30:58 +00:00
|
|
|
RpcResponseFuture exec(std::string_view method, tr_variant* args);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
signals:
|
|
|
|
void httpAuthenticationRequired();
|
|
|
|
void dataReadProgress();
|
|
|
|
void dataSendProgress();
|
2017-04-20 16:02:19 +00:00
|
|
|
void networkResponse(QNetworkReply::NetworkError code, QString const& message);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
private slots:
|
|
|
|
void networkRequestFinished(QNetworkReply* reply);
|
|
|
|
void localRequestFinished(TrVariantPtr response);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
private:
|
|
|
|
RpcResponseFuture sendRequest(TrVariantPtr json);
|
|
|
|
QNetworkAccessManager* networkAccessManager();
|
|
|
|
int64_t getNextTag();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void sendNetworkRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise);
|
|
|
|
void sendLocalRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise, int64_t tag);
|
2022-11-15 16:25:12 +00:00
|
|
|
[[nodiscard]] int64_t parseResponseTag(tr_variant& response) const;
|
|
|
|
[[nodiscard]] RpcResponse parseResponseData(tr_variant& response) const;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
static void localSessionCallback(tr_session* s, tr_variant* response, void* vself) noexcept;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-07-28 15:56:40 +00:00
|
|
|
std::optional<QNetworkRequest> request_;
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_session* session_ = {};
|
|
|
|
QString session_id_;
|
|
|
|
QUrl url_;
|
|
|
|
QNetworkAccessManager* nam_ = {};
|
2023-06-30 19:36:08 +00:00
|
|
|
std::unordered_map<int64_t, QFutureInterface<RpcResponse>> local_requests_;
|
2020-05-27 21:53:12 +00:00
|
|
|
int64_t next_tag_ = {};
|
2022-08-04 13:44:18 +00:00
|
|
|
bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE");
|
2014-12-27 20:03:10 +00:00
|
|
|
};
|