Files
transmission/qt/RpcClient.h
T
Charles Kerr 140958a8a1 refactor: no macros in transmission.h (#8099)
* refactor: replace TR_RPC_SESSION_ID_HEADER macro with TrRpcSessionIdHeader constant

refactor: replace TR_RPC_RPC_VERSION_HEADER macro with TrRpcVersionHeader constant

* refactor: remove macro TR_DEFAULT_RPC_PORT_STR

* refactor: remove macro TR_DEFAULT_PEER_PORT_STR

* refactor: remove macro TR_DEFAULT_PEER_LIMIT_GLOBAL_STR

* refactor: remove macro TR_DEFAULT_PEER_LIMIT_TORRENT_STR

* refactor: remove macro TR_DEFAULT_PEER_SOCKET_TOS_STR

* refactor: replace DEFAULT_BLOCKLIST_FILENAME macro with TrDefaultBlocklistFilename constant

* refactor: rename TrHttpServerDefaultBasePath to TrDefaultHttpServerBasePath for consistency with other defaults

* refactor: group constants together near the top of transmission.h

* refactor: hardcode string lengths to avoid FTBFS on older C++17 compilers

* refactor: move macros to the tr_getopt clients

* refactor: explicitly specify the parameter index to avoid passing in TrRpcSessionIdHeader twice

* refactor: add an error message to new static_asserts
2026-01-14 07:52:57 -06:00

111 lines
3.1 KiB
C++

// This file Copyright © Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#pragma once
#include <cstdint> // int64_t
#include <memory>
#include <optional>
#include <string_view>
#include <unordered_map>
#include <QFuture>
#include <QFutureInterface>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QObject>
#include <QString>
#include <QUrl>
#include <libtransmission/transmission.h>
#include <libtransmission/api-compat.h>
#include <libtransmission/quark.h>
#include <libtransmission/variant.h>
class QNetworkAccessManager;
using TrVariantPtr = std::shared_ptr<tr_variant>;
Q_DECLARE_METATYPE(TrVariantPtr)
extern "C"
{
struct tr_session;
}
struct RpcResponse
{
QString errmsg;
TrVariantPtr args;
bool success = false;
QNetworkReply::NetworkError networkError = QNetworkReply::NoError;
};
Q_DECLARE_METATYPE(QFutureInterface<RpcResponse>)
// The response future -- the RPC engine returns one for each request made.
using RpcResponseFuture = QFuture<RpcResponse>;
class RpcClient : public QObject
{
Q_OBJECT
public:
explicit RpcClient(QObject* parent = nullptr);
RpcClient(RpcClient&&) = delete;
RpcClient(RpcClient const&) = delete;
RpcClient& operator=(RpcClient&&) = delete;
RpcClient& operator=(RpcClient const&) = delete;
[[nodiscard]] constexpr auto const& url() const noexcept
{
return url_;
}
[[nodiscard]] constexpr auto isLocal() const noexcept
{
return session_ != nullptr || url_is_loopback_;
}
void stop();
void start(tr_session* session);
void start(QUrl const& url);
RpcResponseFuture exec(tr_quark method, tr_variant* args);
signals:
void httpAuthenticationRequired();
void dataReadProgress();
void dataSendProgress();
void networkResponse(QNetworkReply::NetworkError code, QString const& message);
private slots:
void networkRequestFinished(QNetworkReply* reply);
void localRequestFinished(TrVariantPtr response);
private:
QByteArray const SessionIdHeaderName = std::data(TrRpcSessionIdHeader);
QByteArray const VersionHeaderName = std::data(TrRpcVersionHeader);
QNetworkAccessManager* networkAccessManager();
void sendNetworkRequest(QByteArray const& body, QFutureInterface<RpcResponse> const& promise);
void sendLocalRequest(tr_variant& req, QFutureInterface<RpcResponse> const& promise, int64_t id);
[[nodiscard]] int64_t parseResponseId(tr_variant& response) const;
[[nodiscard]] RpcResponse parseResponseData(tr_variant& response) const;
// TODO: change this default in 5.0.0-beta.1
static auto constexpr DefaultNetworkStyle = libtransmission::api_compat::Style::Tr4;
libtransmission::api_compat::Style network_style_ = DefaultNetworkStyle;
tr_session* session_ = {};
QByteArray session_id_;
QUrl url_;
QNetworkAccessManager* nam_ = {};
std::unordered_map<int64_t, QFutureInterface<RpcResponse>> local_requests_;
bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE");
bool url_is_loopback_ = false;
};