tdesktop/Telegram/SourceFiles/mtproto/mtproto_concurrent_sender.h

433 lines
12 KiB
C
Raw Normal View History

2018-06-02 14:29:21 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "base/bytes.h"
#include "base/weak_ptr.h"
#include "base/flat_map.h"
#include "mtproto/core_types.h"
#include "mtproto/details/mtproto_serialized_request.h"
2018-06-02 14:29:21 +00:00
#include <QtCore/QPointer>
2019-12-02 13:10:19 +00:00
#include <rpl/details/callable.h>
#ifndef _DEBUG
#define MTP_SENDER_USE_GENERIC_HANDLERS
#endif // !_DEBUG
2018-06-02 14:29:21 +00:00
namespace MTP {
2021-03-12 12:48:00 +00:00
class Error;
2018-06-02 14:29:21 +00:00
class Instance;
class ConcurrentSender : public base::has_weak_ptr {
template <typename ...Args>
static constexpr bool is_callable_v
= rpl::details::is_callable_v<Args...>;
2018-06-02 14:29:21 +00:00
template <typename Method>
auto with_instance(Method &&method)
-> std::enable_if_t<is_callable_v<Method, not_null<Instance*>>>;
struct Handlers {
2021-03-12 12:48:00 +00:00
FnMut<bool(mtpRequestId requestId, bytes::const_span result)> done;
FnMut<void(mtpRequestId requestId, const Error &error)> fail;
2018-06-02 14:29:21 +00:00
};
enum class FailSkipPolicy {
Simple,
HandleFlood,
HandleAll,
};
class RequestBuilder {
public:
RequestBuilder(const RequestBuilder &other) = delete;
RequestBuilder(RequestBuilder &&other) = default;
RequestBuilder &operator=(const RequestBuilder &other) = delete;
RequestBuilder &operator=(RequestBuilder &&other) = delete;
mtpRequestId send();
protected:
RequestBuilder(
not_null<ConcurrentSender*> sender,
details::SerializedRequest &&serialized) noexcept;
2018-06-02 14:29:21 +00:00
void setToDC(ShiftedDcId dcId) noexcept;
void setCanWait(crl::time ms) noexcept;
2018-06-02 14:29:21 +00:00
template <typename Response, typename InvokeFullDone>
void setDoneHandler(InvokeFullDone &&invoke) noexcept;
template <typename InvokeFullFail>
void setFailHandler(InvokeFullFail &&invoke) noexcept;
void setFailSkipPolicy(FailSkipPolicy policy) noexcept;
void setAfter(mtpRequestId requestId) noexcept;
private:
not_null<ConcurrentSender*> _sender;
details::SerializedRequest _serialized;
2018-06-02 14:29:21 +00:00
ShiftedDcId _dcId = 0;
crl::time _canWait = 0;
2018-06-02 14:29:21 +00:00
Handlers _handlers;
FailSkipPolicy _failSkipPolicy = FailSkipPolicy::Simple;
mtpRequestId _afterRequestId = 0;
};
public:
ConcurrentSender(
QPointer<Instance> weak,
Fn<void(FnMut<void()>)> runner);
2018-06-02 14:29:21 +00:00
template <typename Request>
class SpecificRequestBuilder : public RequestBuilder {
public:
using Result = typename Request::ResponseType;
2018-06-02 14:29:21 +00:00
SpecificRequestBuilder(
const SpecificRequestBuilder &other) = delete;
SpecificRequestBuilder(
SpecificRequestBuilder &&other) = default;
SpecificRequestBuilder &operator=(
const SpecificRequestBuilder &other) = delete;
SpecificRequestBuilder &operator=(
SpecificRequestBuilder &&other) = delete;
[[nodiscard]] SpecificRequestBuilder &toDC(
ShiftedDcId dcId) noexcept;
[[nodiscard]] SpecificRequestBuilder &afterDelay(
crl::time ms) noexcept;
#ifndef MTP_SENDER_USE_GENERIC_HANDLERS
// Allow code completion to show response type.
[[nodiscard]] SpecificRequestBuilder &done(FnMut<void()> &&handler);
[[nodiscard]] SpecificRequestBuilder &done(
FnMut<void(mtpRequestId, Result &&)> &&handler);
[[nodiscard]] SpecificRequestBuilder &done(
FnMut<void(Result &&)> &&handler);
[[nodiscard]] SpecificRequestBuilder &fail(Fn<void()> &&handler);
[[nodiscard]] SpecificRequestBuilder &fail(
2021-03-12 12:48:00 +00:00
Fn<void(mtpRequestId, const Error &)> &&handler);
[[nodiscard]] SpecificRequestBuilder &fail(
2021-03-12 12:48:00 +00:00
Fn<void(const Error &)> &&handler);
#else // !MTP_SENDER_USE_GENERIC_HANDLERS
2018-06-02 14:29:21 +00:00
template <typename Handler>
[[nodiscard]] SpecificRequestBuilder &done(Handler &&handler);
template <typename Handler>
[[nodiscard]] SpecificRequestBuilder &fail(Handler &&handler);
#endif // MTP_SENDER_USE_GENERIC_HANDLERS
2018-06-02 14:29:21 +00:00
[[nodiscard]] SpecificRequestBuilder &handleFloodErrors() noexcept;
[[nodiscard]] SpecificRequestBuilder &handleAllErrors() noexcept;
[[nodiscard]] SpecificRequestBuilder &afterRequest(
mtpRequestId requestId) noexcept;
private:
SpecificRequestBuilder(
not_null<ConcurrentSender*> sender,
Request &&request) noexcept;
friend class ConcurrentSender;
};
class SentRequestWrap {
public:
void cancel();
2018-06-21 00:54:59 +00:00
void detach();
2018-06-02 14:29:21 +00:00
private:
friend class ConcurrentSender;
SentRequestWrap(
not_null<ConcurrentSender*> sender,
mtpRequestId requestId);
2018-06-02 14:29:21 +00:00
not_null<ConcurrentSender*> _sender;
mtpRequestId _requestId = 0;
};
template <
typename Request,
typename = std::enable_if_t<!std::is_reference_v<Request>>,
typename = typename Request::Unboxed>
[[nodiscard]] SpecificRequestBuilder<Request> request(
Request &&request) noexcept;
[[nodiscard]] SentRequestWrap request(mtpRequestId requestId) noexcept;
[[nodiscard]] auto requestCanceller() noexcept;
2018-06-02 14:29:21 +00:00
~ConcurrentSender();
private:
class HandlerMaker;
friend class HandlerMaker;
2018-06-02 14:29:21 +00:00
friend class RequestBuilder;
friend class SentRequestWrap;
void senderRequestRegister(mtpRequestId requestId, Handlers &&handlers);
void senderRequestDone(
mtpRequestId requestId,
bytes::const_span result);
void senderRequestFail(
mtpRequestId requestId,
2021-03-12 12:48:00 +00:00
const Error &error);
2018-06-02 14:29:21 +00:00
void senderRequestCancel(mtpRequestId requestId);
void senderRequestCancelAll();
2018-06-21 00:54:59 +00:00
void senderRequestDetach(mtpRequestId requestId);
2018-06-02 14:29:21 +00:00
const QPointer<Instance> _weak;
const Fn<void(FnMut<void()>)> _runner;
2018-06-02 14:29:21 +00:00
base::flat_map<mtpRequestId, Handlers> _requests;
};
template <typename Result, typename InvokeFullDone>
2018-06-02 14:29:21 +00:00
void ConcurrentSender::RequestBuilder::setDoneHandler(
InvokeFullDone &&invoke
) noexcept {
2018-06-02 14:29:21 +00:00
_handlers.done = [handler = std::move(invoke)](
mtpRequestId requestId,
bytes::const_span result) mutable {
auto from = reinterpret_cast<const mtpPrime*>(result.data());
const auto end = from + result.size() / sizeof(mtpPrime);
Result data;
2019-07-18 14:06:38 +00:00
if (!data.read(from, end)) {
return false;
}
handler(requestId, std::move(data));
2019-07-18 14:06:38 +00:00
return true;
2018-06-02 14:29:21 +00:00
};
}
template <typename InvokeFullFail>
void ConcurrentSender::RequestBuilder::setFailHandler(
InvokeFullFail &&invoke
) noexcept {
2018-06-02 14:29:21 +00:00
_handlers.fail = std::move(invoke);
}
template <typename Request>
ConcurrentSender::SpecificRequestBuilder<Request>::SpecificRequestBuilder(
not_null<ConcurrentSender*> sender,
Request &&request) noexcept
: RequestBuilder(sender, details::SerializedRequest::Serialize(request)) {
2018-06-02 14:29:21 +00:00
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::toDC(
ShiftedDcId dcId
) noexcept -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
setToDC(dcId);
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::afterDelay(
crl::time ms
) noexcept -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
setCanWait(ms);
return *this;
}
#ifndef MTP_SENDER_USE_GENERIC_HANDLERS
// Allow code completion to show response type.
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
FnMut<void(Result &&)> &&handler
) -> SpecificRequestBuilder & {
setDoneHandler<Result>([handler = std::move(handler)](
mtpRequestId requestId,
Result &&result) mutable {
handler(std::move(result));
});
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
FnMut<void(mtpRequestId, Result &&)> &&handler
) -> SpecificRequestBuilder & {
setDoneHandler<Result>(std::move(handler));
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
FnMut<void()> &&handler
) -> SpecificRequestBuilder & {
setDoneHandler<Result>([handler = std::move(handler)](
mtpRequestId requestId,
Result &&result) mutable {
std::move(handler)();
});
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
2021-03-12 12:48:00 +00:00
Fn<void(const Error &)> &&handler
) -> SpecificRequestBuilder & {
setFailHandler([handler = std::move(handler)](
mtpRequestId requestId,
2021-03-12 12:48:00 +00:00
const Error &error) {
handler(error);
});
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
2021-03-12 12:48:00 +00:00
Fn<void(mtpRequestId, const Error &)> &&handler
) -> SpecificRequestBuilder & {
setFailHandler(std::move(handler));
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
Fn<void()> &&handler
) -> SpecificRequestBuilder & {
setFailHandler([handler = std::move(handler)](
mtpRequestId requestId,
2021-03-12 12:48:00 +00:00
const Error &error) {
handler();
});
return *this;
}
#else // !MTP_SENDER_USE_GENERIC_HANDLERS
2018-06-02 14:29:21 +00:00
template <typename Request>
template <typename Handler>
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
Handler &&handler
) -> SpecificRequestBuilder & {
using Result = typename Request::ResponseType;
2018-06-02 14:29:21 +00:00
constexpr auto takesFull = rpl::details::is_callable_plain_v<
Handler,
mtpRequestId,
Result>;
2018-06-02 14:29:21 +00:00
constexpr auto takesResponse = rpl::details::is_callable_plain_v<
Handler,
Result>;
2018-06-02 14:29:21 +00:00
constexpr auto takesNone = rpl::details::is_callable_plain_v<Handler>;
if constexpr (takesFull) {
setDoneHandler<Result>(std::forward<Handler>(handler));
2018-06-02 14:29:21 +00:00
} else if constexpr (takesResponse) {
setDoneHandler<Result>([handler = std::forward<Handler>(handler)](
2018-06-02 14:29:21 +00:00
mtpRequestId requestId,
Result &&result) mutable {
handler(std::move(result));
2018-06-02 14:29:21 +00:00
});
} else if constexpr (takesNone) {
setDoneHandler<Result>([handler = std::forward<Handler>(handler)](
2018-06-02 14:29:21 +00:00
mtpRequestId requestId,
Result &&result) mutable {
handler();
2018-06-02 14:29:21 +00:00
});
} else {
static_assert(false_t(Handler{}), "Bad done handler.");
}
return *this;
}
template <typename Request>
template <typename Handler>
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
Handler &&handler
) -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
constexpr auto takesFull = rpl::details::is_callable_plain_v<
Handler,
mtpRequestId,
2021-03-12 12:48:00 +00:00
Error>;
2018-06-02 14:29:21 +00:00
constexpr auto takesError = rpl::details::is_callable_plain_v<
Handler,
2021-03-12 12:48:00 +00:00
Error>;
2018-06-02 14:29:21 +00:00
constexpr auto takesNone = rpl::details::is_callable_plain_v<Handler>;
if constexpr (takesFull) {
setFailHandler(std::forward<Handler>(handler));
} else if constexpr (takesError) {
setFailHandler([handler = std::forward<Handler>(handler)](
mtpRequestId requestId,
2021-03-12 12:48:00 +00:00
const Error &error) {
handler(error);
2018-06-02 14:29:21 +00:00
});
} else if constexpr (takesNone) {
setFailHandler([handler = std::forward<Handler>(handler)](
mtpRequestId requestId,
2021-03-12 12:48:00 +00:00
const Error &error) {
handler();
2018-06-02 14:29:21 +00:00
});
} else {
static_assert(false_t(Handler{}), "Bad fail handler.");
}
return *this;
}
#endif // MTP_SENDER_USE_GENERIC_HANDLERS
2018-06-02 14:29:21 +00:00
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::handleFloodErrors(
) noexcept -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
setFailSkipPolicy(FailSkipPolicy::HandleFlood);
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::handleAllErrors(
) noexcept -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
setFailSkipPolicy(FailSkipPolicy::HandleAll);
return *this;
}
template <typename Request>
auto ConcurrentSender::SpecificRequestBuilder<Request>::afterRequest(
mtpRequestId requestId
) noexcept -> SpecificRequestBuilder & {
2018-06-02 14:29:21 +00:00
setAfter(requestId);
return *this;
}
inline void ConcurrentSender::SentRequestWrap::cancel() {
_sender->senderRequestCancel(_requestId);
}
2018-06-21 00:54:59 +00:00
inline void ConcurrentSender::SentRequestWrap::detach() {
_sender->senderRequestDetach(_requestId);
}
inline ConcurrentSender::SentRequestWrap::SentRequestWrap(
not_null<ConcurrentSender*> sender,
mtpRequestId requestId
) : _sender(sender)
, _requestId(requestId) {
}
2018-06-02 14:29:21 +00:00
template <typename Request, typename, typename>
inline auto ConcurrentSender::request(Request &&request) noexcept
-> SpecificRequestBuilder<Request> {
return SpecificRequestBuilder<Request>(this, std::move(request));
}
inline auto ConcurrentSender::requestCanceller() noexcept {
return [=](mtpRequestId requestId) {
request(requestId).cancel();
};
}
2018-06-02 14:29:21 +00:00
inline auto ConcurrentSender::request(mtpRequestId requestId) noexcept
-> SentRequestWrap {
return SentRequestWrap(this, requestId);
}
} // namespace MTP