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"
|
2019-11-22 09:15:38 +00:00
|
|
|
#include "mtproto/details/mtproto_serialized_request.h"
|
2018-06-02 14:29:21 +00:00
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
#include <QtCore/QPointer>
|
2019-12-02 13:10:19 +00:00
|
|
|
#include <rpl/details/callable.h>
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
#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>
|
2018-06-17 20:29:46 +00:00
|
|
|
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,
|
2019-11-22 09:15:38 +00:00
|
|
|
details::SerializedRequest &&serialized) noexcept;
|
2018-06-02 14:29:21 +00:00
|
|
|
|
|
|
|
void setToDC(ShiftedDcId dcId) noexcept;
|
2019-02-19 06:57:53 +00:00
|
|
|
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;
|
2019-11-22 09:15:38 +00:00
|
|
|
details::SerializedRequest _serialized;
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId _dcId = 0;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _canWait = 0;
|
2018-06-02 14:29:21 +00:00
|
|
|
|
|
|
|
Handlers _handlers;
|
|
|
|
FailSkipPolicy _failSkipPolicy = FailSkipPolicy::Simple;
|
|
|
|
mtpRequestId _afterRequestId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2020-06-11 16:09:46 +00:00
|
|
|
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:
|
2021-03-12 10:45:13 +00:00
|
|
|
using Result = typename Request::ResponseType;
|
2018-06-09 11:51:22 +00:00
|
|
|
|
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(
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time ms) noexcept;
|
2018-06-09 11:51:22 +00:00
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
#ifndef MTP_SENDER_USE_GENERIC_HANDLERS
|
2018-06-09 11:51:22 +00:00
|
|
|
// Allow code completion to show response type.
|
2018-06-11 18:35:27 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &done(FnMut<void()> &&handler);
|
2018-06-28 16:25:50 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &done(
|
2021-03-12 10:45:13 +00:00
|
|
|
FnMut<void(mtpRequestId, Result &&)> &&handler);
|
2018-06-28 16:25:50 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &done(
|
2021-03-12 10:45:13 +00:00
|
|
|
FnMut<void(Result &&)> &&handler);
|
|
|
|
[[nodiscard]] SpecificRequestBuilder &fail(Fn<void()> &&handler);
|
2018-06-28 16:25:50 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &fail(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(mtpRequestId, const Error &)> &&handler);
|
2018-06-28 16:25:50 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &fail(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error &)> &&handler);
|
2018-06-17 20:29:46 +00:00
|
|
|
#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);
|
2018-06-17 20:29:46 +00:00
|
|
|
#endif // MTP_SENDER_USE_GENERIC_HANDLERS
|
2018-06-09 11:51:22 +00:00
|
|
|
|
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:
|
2018-06-17 20:29:46 +00:00
|
|
|
void cancel();
|
2018-06-21 00:54:59 +00:00
|
|
|
void detach();
|
2018-06-02 14:29:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class ConcurrentSender;
|
2018-06-17 20:29:46 +00:00
|
|
|
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;
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
[[nodiscard]] auto requestCanceller() noexcept;
|
2018-06-02 14:29:21 +00:00
|
|
|
|
|
|
|
~ConcurrentSender();
|
|
|
|
|
|
|
|
private:
|
2021-03-12 10:45:13 +00:00
|
|
|
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
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
const QPointer<Instance> _weak;
|
2018-06-11 18:35:27 +00:00
|
|
|
const Fn<void(FnMut<void()>)> _runner;
|
2018-06-02 14:29:21 +00:00
|
|
|
base::flat_map<mtpRequestId, Handlers> _requests;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
template <typename Result, typename InvokeFullDone>
|
2018-06-02 14:29:21 +00:00
|
|
|
void ConcurrentSender::RequestBuilder::setDoneHandler(
|
2018-06-17 20:29:46 +00:00
|
|
|
InvokeFullDone &&invoke
|
|
|
|
) noexcept {
|
2018-06-02 14:29:21 +00:00
|
|
|
_handlers.done = [handler = std::move(invoke)](
|
|
|
|
mtpRequestId requestId,
|
|
|
|
bytes::const_span result) mutable {
|
2018-06-19 10:42:21 +00:00
|
|
|
auto from = reinterpret_cast<const mtpPrime*>(result.data());
|
|
|
|
const auto end = from + result.size() / sizeof(mtpPrime);
|
2021-03-12 10:45:13 +00:00
|
|
|
Result data;
|
2019-07-18 14:06:38 +00:00
|
|
|
if (!data.read(from, end)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
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(
|
2018-06-17 20:29:46 +00:00
|
|
|
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,
|
2019-11-22 09:15:38 +00:00
|
|
|
Request &&request) noexcept
|
|
|
|
: RequestBuilder(sender, details::SerializedRequest::Serialize(request)) {
|
2018-06-02 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::toDC(
|
|
|
|
ShiftedDcId dcId
|
|
|
|
) noexcept -> SpecificRequestBuilder & {
|
2018-06-02 14:29:21 +00:00
|
|
|
setToDC(dcId);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::afterDelay(
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time ms
|
2018-06-17 20:29:46 +00:00
|
|
|
) noexcept -> SpecificRequestBuilder & {
|
2018-06-02 14:29:21 +00:00
|
|
|
setCanWait(ms);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
#ifndef MTP_SENDER_USE_GENERIC_HANDLERS
|
2018-06-09 11:51:22 +00:00
|
|
|
// Allow code completion to show response type.
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
|
2021-03-12 10:45:13 +00:00
|
|
|
FnMut<void(Result &&)> &&handler
|
2018-06-17 20:29:46 +00:00
|
|
|
) -> SpecificRequestBuilder & {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>([handler = std::move(handler)](
|
2018-06-09 11:51:22 +00:00
|
|
|
mtpRequestId requestId,
|
2021-03-12 10:45:13 +00:00
|
|
|
Result &&result) mutable {
|
|
|
|
handler(std::move(result));
|
2018-06-09 11:51:22 +00:00
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
|
2021-03-12 10:45:13 +00:00
|
|
|
FnMut<void(mtpRequestId, Result &&)> &&handler
|
2018-06-17 20:29:46 +00:00
|
|
|
) -> SpecificRequestBuilder & {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>(std::move(handler));
|
2018-06-09 11:51:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
|
|
|
|
FnMut<void()> &&handler
|
|
|
|
) -> SpecificRequestBuilder & {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>([handler = std::move(handler)](
|
2018-06-09 11:51:22 +00:00
|
|
|
mtpRequestId requestId,
|
2021-03-12 10:45:13 +00:00
|
|
|
Result &&result) mutable {
|
2018-06-09 11:51:22 +00:00
|
|
|
std::move(handler)();
|
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error &)> &&handler
|
2018-06-17 20:29:46 +00:00
|
|
|
) -> SpecificRequestBuilder & {
|
2018-06-09 11:51:22 +00:00
|
|
|
setFailHandler([handler = std::move(handler)](
|
|
|
|
mtpRequestId requestId,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error) {
|
2021-03-12 10:45:13 +00:00
|
|
|
handler(error);
|
2018-06-09 11:51:22 +00:00
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(mtpRequestId, const Error &)> &&handler
|
2018-06-17 20:29:46 +00:00
|
|
|
) -> SpecificRequestBuilder & {
|
2018-06-09 11:51:22 +00:00
|
|
|
setFailHandler(std::move(handler));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::fail(
|
2021-03-12 10:45:13 +00:00
|
|
|
Fn<void()> &&handler
|
2018-06-17 20:29:46 +00:00
|
|
|
) -> SpecificRequestBuilder & {
|
2018-06-09 11:51:22 +00:00
|
|
|
setFailHandler([handler = std::move(handler)](
|
|
|
|
mtpRequestId requestId,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error) {
|
2021-03-12 10:45:13 +00:00
|
|
|
handler();
|
2018-06-09 11:51:22 +00:00
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
2018-06-17 20:29:46 +00:00
|
|
|
#else // !MTP_SENDER_USE_GENERIC_HANDLERS
|
2018-06-02 14:29:21 +00:00
|
|
|
template <typename Request>
|
|
|
|
template <typename Handler>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::done(
|
|
|
|
Handler &&handler
|
|
|
|
) -> SpecificRequestBuilder & {
|
2021-03-12 10:45:13 +00:00
|
|
|
using Result = typename Request::ResponseType;
|
2018-06-02 14:29:21 +00:00
|
|
|
constexpr auto takesFull = rpl::details::is_callable_plain_v<
|
|
|
|
Handler,
|
|
|
|
mtpRequestId,
|
2021-03-12 10:45:13 +00:00
|
|
|
Result>;
|
2018-06-02 14:29:21 +00:00
|
|
|
constexpr auto takesResponse = rpl::details::is_callable_plain_v<
|
|
|
|
Handler,
|
2021-03-12 10:45:13 +00:00
|
|
|
Result>;
|
2018-06-02 14:29:21 +00:00
|
|
|
constexpr auto takesNone = rpl::details::is_callable_plain_v<Handler>;
|
|
|
|
|
|
|
|
if constexpr (takesFull) {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>(std::forward<Handler>(handler));
|
2018-06-02 14:29:21 +00:00
|
|
|
} else if constexpr (takesResponse) {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>([handler = std::forward<Handler>(handler)](
|
2018-06-02 14:29:21 +00:00
|
|
|
mtpRequestId requestId,
|
2021-03-12 10:45:13 +00:00
|
|
|
Result &&result) mutable {
|
|
|
|
handler(std::move(result));
|
2018-06-02 14:29:21 +00:00
|
|
|
});
|
|
|
|
} else if constexpr (takesNone) {
|
2021-03-12 10:45:13 +00:00
|
|
|
setDoneHandler<Result>([handler = std::forward<Handler>(handler)](
|
2018-06-02 14:29:21 +00:00
|
|
|
mtpRequestId requestId,
|
2021-03-12 10:45:13 +00:00
|
|
|
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>
|
2018-06-17 20:29:46 +00:00
|
|
|
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) {
|
2021-03-12 10:45:13 +00:00
|
|
|
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) {
|
2021-03-12 10:45:13 +00:00
|
|
|
handler();
|
2018-06-02 14:29:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
static_assert(false_t(Handler{}), "Bad fail handler.");
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
#endif // MTP_SENDER_USE_GENERIC_HANDLERS
|
2018-06-09 11:51:22 +00:00
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::handleFloodErrors(
|
|
|
|
) noexcept -> SpecificRequestBuilder & {
|
2018-06-02 14:29:21 +00:00
|
|
|
setFailSkipPolicy(FailSkipPolicy::HandleFlood);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::handleAllErrors(
|
|
|
|
) noexcept -> SpecificRequestBuilder & {
|
2018-06-02 14:29:21 +00:00
|
|
|
setFailSkipPolicy(FailSkipPolicy::HandleAll);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
2018-06-17 20:29:46 +00:00
|
|
|
auto ConcurrentSender::SpecificRequestBuilder<Request>::afterRequest(
|
|
|
|
mtpRequestId requestId
|
|
|
|
) noexcept -> SpecificRequestBuilder & {
|
2018-06-02 14:29:21 +00:00
|
|
|
setAfter(requestId);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
inline void ConcurrentSender::SentRequestWrap::cancel() {
|
|
|
|
_sender->senderRequestCancel(_requestId);
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:54:59 +00:00
|
|
|
inline void ConcurrentSender::SentRequestWrap::detach() {
|
|
|
|
_sender->senderRequestDetach(_requestId);
|
|
|
|
}
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-06-17 20:29:46 +00:00
|
|
|
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
|