2017-03-22 15:38:40 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-03-22 15:38:40 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-03-22 15:38:40 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "base/variant.h"
|
2017-03-22 15:38:40 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
|
|
|
|
class Instance;
|
|
|
|
Instance *MainInstance();
|
|
|
|
|
|
|
|
class Sender {
|
|
|
|
class RequestBuilder {
|
|
|
|
public:
|
|
|
|
RequestBuilder(const RequestBuilder &other) = delete;
|
|
|
|
RequestBuilder &operator=(const RequestBuilder &other) = delete;
|
|
|
|
RequestBuilder &operator=(RequestBuilder &&other) = delete;
|
|
|
|
|
|
|
|
protected:
|
2018-06-04 15:35:11 +00:00
|
|
|
using FailPlainHandler = FnMut<void(const RPCError &error)>;
|
|
|
|
using FailRequestIdHandler = FnMut<void(const RPCError &error, mtpRequestId requestId)>;
|
2017-03-22 15:38:40 +00:00
|
|
|
enum class FailSkipPolicy {
|
|
|
|
Simple,
|
|
|
|
HandleFlood,
|
|
|
|
HandleAll,
|
|
|
|
};
|
|
|
|
template <typename Response>
|
|
|
|
struct DonePlainPolicy {
|
2018-06-04 15:35:11 +00:00
|
|
|
using Callback = FnMut<void(const Response &result)>;
|
2017-03-22 15:38:40 +00:00
|
|
|
static void handle(Callback &&handler, mtpRequestId requestId, Response &&result) {
|
|
|
|
handler(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
template <typename Response>
|
|
|
|
struct DoneRequestIdPolicy {
|
2018-06-04 15:35:11 +00:00
|
|
|
using Callback = FnMut<void(const Response &result, mtpRequestId requestId)>;
|
2017-03-22 15:38:40 +00:00
|
|
|
static void handle(Callback &&handler, mtpRequestId requestId, Response &&result) {
|
|
|
|
handler(result, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
template <typename Response, template <typename> typename PolicyTemplate>
|
|
|
|
class DoneHandler : public RPCAbstractDoneHandler {
|
|
|
|
using Policy = PolicyTemplate<Response>;
|
|
|
|
using Callback = typename Policy::Callback;
|
|
|
|
|
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
DoneHandler(not_null<Sender*> sender, Callback handler) : _sender(sender), _handler(std::move(handler)) {
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end) override {
|
|
|
|
auto handler = std::move(_handler);
|
2017-08-14 12:48:11 +00:00
|
|
|
_sender->senderRequestHandled(requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
auto result = Response();
|
|
|
|
result.read(from, end);
|
|
|
|
Policy::handle(std::move(handler), requestId, std::move(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Sender*> _sender;
|
2017-03-22 15:38:40 +00:00
|
|
|
Callback _handler;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FailPlainPolicy {
|
2018-06-04 15:35:11 +00:00
|
|
|
using Callback = FnMut<void(const RPCError &error)>;
|
2017-03-22 15:38:40 +00:00
|
|
|
static void handle(Callback &&handler, mtpRequestId requestId, const RPCError &error) {
|
|
|
|
handler(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
struct FailRequestIdPolicy {
|
2018-06-04 15:35:11 +00:00
|
|
|
using Callback = FnMut<void(const RPCError &error, mtpRequestId requestId)>;
|
2017-03-22 15:38:40 +00:00
|
|
|
static void handle(Callback &&handler, mtpRequestId requestId, const RPCError &error) {
|
|
|
|
handler(error, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
template <typename Policy>
|
|
|
|
class FailHandler : public RPCAbstractFailHandler {
|
|
|
|
using Callback = typename Policy::Callback;
|
|
|
|
|
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
FailHandler(not_null<Sender*> sender, Callback handler, FailSkipPolicy skipPolicy)
|
2017-03-22 15:38:40 +00:00
|
|
|
: _sender(sender)
|
|
|
|
, _handler(std::move(handler))
|
|
|
|
, _skipPolicy(skipPolicy) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(mtpRequestId requestId, const RPCError &error) override {
|
|
|
|
if (_skipPolicy == FailSkipPolicy::Simple) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (_skipPolicy == FailSkipPolicy::HandleFlood) {
|
|
|
|
if (MTP::isDefaultHandledError(error) && !MTP::isFloodError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto handler = std::move(_handler);
|
2017-08-14 12:48:11 +00:00
|
|
|
_sender->senderRequestHandled(requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
Policy::handle(std::move(handler), requestId, error);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Sender*> _sender;
|
2017-03-22 15:38:40 +00:00
|
|
|
Callback _handler;
|
|
|
|
FailSkipPolicy _skipPolicy = FailSkipPolicy::Simple;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
explicit RequestBuilder(not_null<Sender*> sender) noexcept : _sender(sender) {
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
RequestBuilder(RequestBuilder &&other) = default;
|
|
|
|
|
|
|
|
void setToDC(ShiftedDcId dcId) noexcept {
|
|
|
|
_dcId = dcId;
|
|
|
|
}
|
|
|
|
void setCanWait(TimeMs ms) noexcept {
|
|
|
|
_canWait = ms;
|
|
|
|
}
|
|
|
|
void setDoneHandler(RPCDoneHandlerPtr &&handler) noexcept {
|
|
|
|
_done = std::move(handler);
|
|
|
|
}
|
|
|
|
void setFailHandler(FailPlainHandler &&handler) noexcept {
|
|
|
|
_fail = std::move(handler);
|
|
|
|
}
|
|
|
|
void setFailHandler(FailRequestIdHandler &&handler) noexcept {
|
|
|
|
_fail = std::move(handler);
|
|
|
|
}
|
|
|
|
void setFailSkipPolicy(FailSkipPolicy policy) noexcept {
|
|
|
|
_failSkipPolicy = policy;
|
|
|
|
}
|
|
|
|
void setAfter(mtpRequestId requestId) noexcept {
|
|
|
|
_afterRequestId = requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShiftedDcId takeDcId() const noexcept {
|
|
|
|
return _dcId;
|
|
|
|
}
|
|
|
|
TimeMs takeCanWait() const noexcept {
|
|
|
|
return _canWait;
|
|
|
|
}
|
|
|
|
RPCDoneHandlerPtr takeOnDone() noexcept {
|
|
|
|
return std::move(_done);
|
|
|
|
}
|
|
|
|
RPCFailHandlerPtr takeOnFail() {
|
|
|
|
if (auto handler = base::get_if<FailPlainHandler>(&_fail)) {
|
2017-12-18 09:07:18 +00:00
|
|
|
return std::make_shared<FailHandler<FailPlainPolicy>>(_sender, std::move(*handler), _failSkipPolicy);
|
2017-03-22 15:38:40 +00:00
|
|
|
} else if (auto handler = base::get_if<FailRequestIdHandler>(&_fail)) {
|
2017-12-18 09:07:18 +00:00
|
|
|
return std::make_shared<FailHandler<FailRequestIdPolicy>>(_sender, std::move(*handler), _failSkipPolicy);
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
return RPCFailHandlerPtr();
|
|
|
|
}
|
|
|
|
mtpRequestId takeAfter() const noexcept {
|
|
|
|
return _afterRequestId;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Sender*> sender() const noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
return _sender;
|
|
|
|
}
|
|
|
|
void registerRequest(mtpRequestId requestId) {
|
2017-08-14 12:48:11 +00:00
|
|
|
_sender->senderRequestRegister(requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Sender*> _sender;
|
2017-03-22 15:38:40 +00:00
|
|
|
ShiftedDcId _dcId = 0;
|
|
|
|
TimeMs _canWait = 0;
|
|
|
|
RPCDoneHandlerPtr _done;
|
|
|
|
base::variant<FailPlainHandler, FailRequestIdHandler> _fail;
|
|
|
|
FailSkipPolicy _failSkipPolicy = FailSkipPolicy::Simple;
|
|
|
|
mtpRequestId _afterRequestId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2017-05-30 16:58:13 +00:00
|
|
|
Sender() noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request>
|
|
|
|
class SpecificRequestBuilder : public RequestBuilder {
|
|
|
|
private:
|
|
|
|
friend class Sender;
|
2017-08-17 08:31:24 +00:00
|
|
|
SpecificRequestBuilder(not_null<Sender*> sender, Request &&request) noexcept : RequestBuilder(sender), _request(std::move(request)) {
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
SpecificRequestBuilder(SpecificRequestBuilder &&other) = default;
|
|
|
|
|
|
|
|
public:
|
2017-10-13 19:07:04 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &toDC(ShiftedDcId dcId) noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setToDC(dcId);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &afterDelay(TimeMs ms) noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setCanWait(ms);
|
|
|
|
return *this;
|
|
|
|
}
|
2018-06-04 15:35:11 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &done(FnMut<void(const typename Request::ResponseType &result)> callback) {
|
2017-12-18 09:07:18 +00:00
|
|
|
setDoneHandler(std::make_shared<DoneHandler<typename Request::ResponseType, DonePlainPolicy>>(sender(), std::move(callback)));
|
2017-03-22 15:38:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2018-06-04 15:35:11 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &done(FnMut<void(const typename Request::ResponseType &result, mtpRequestId requestId)> callback) {
|
2017-12-18 09:07:18 +00:00
|
|
|
setDoneHandler(std::make_shared<DoneHandler<typename Request::ResponseType, DoneRequestIdPolicy>>(sender(), std::move(callback)));
|
2017-03-22 15:38:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2018-06-04 15:35:11 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &fail(FnMut<void(const RPCError &error)> callback) noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setFailHandler(std::move(callback));
|
|
|
|
return *this;
|
|
|
|
}
|
2018-06-04 15:35:11 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &fail(FnMut<void(const RPCError &error, mtpRequestId requestId)> callback) noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setFailHandler(std::move(callback));
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &handleFloodErrors() noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setFailSkipPolicy(FailSkipPolicy::HandleFlood);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &handleAllErrors() noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setFailSkipPolicy(FailSkipPolicy::HandleAll);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder &afterRequest(mtpRequestId requestId) noexcept {
|
2017-03-22 15:38:40 +00:00
|
|
|
setAfter(requestId);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId send() {
|
2017-12-18 12:40:15 +00:00
|
|
|
const auto id = MainInstance()->send(
|
|
|
|
_request,
|
|
|
|
takeOnDone(),
|
|
|
|
takeOnFail(),
|
|
|
|
takeDcId(),
|
|
|
|
takeCanWait(),
|
|
|
|
takeAfter());
|
2017-03-22 15:38:40 +00:00
|
|
|
registerRequest(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Request _request;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class SentRequestWrap {
|
|
|
|
private:
|
|
|
|
friend class Sender;
|
2017-08-17 08:31:24 +00:00
|
|
|
SentRequestWrap(not_null<Sender*> sender, mtpRequestId requestId) : _sender(sender), _requestId(requestId) {
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void cancel() {
|
2017-08-14 12:48:11 +00:00
|
|
|
_sender->senderRequestCancel(_requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Sender*> _sender;
|
2017-03-22 15:38:40 +00:00
|
|
|
mtpRequestId _requestId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
template <
|
|
|
|
typename Request,
|
|
|
|
typename = std::enable_if_t<!std::is_reference_v<Request>>,
|
|
|
|
typename = typename Request::Unboxed>
|
2017-10-13 19:07:04 +00:00
|
|
|
[[nodiscard]] SpecificRequestBuilder<Request> request(Request &&request) noexcept;
|
2017-03-22 15:38:40 +00:00
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
[[nodiscard]] SentRequestWrap request(mtpRequestId requestId) noexcept;
|
2017-03-22 15:38:40 +00:00
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
[[nodiscard]] auto requestCanceller() noexcept {
|
2017-08-14 12:48:11 +00:00
|
|
|
return [this](mtpRequestId requestId) {
|
|
|
|
request(requestId).cancel();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
void requestSendDelayed() {
|
2017-05-30 16:58:13 +00:00
|
|
|
MainInstance()->sendAnything();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
void requestCancellingDiscard() {
|
|
|
|
for (auto &request : _requests) {
|
|
|
|
request.handled();
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Instance*> requestMTP() const {
|
2017-05-30 16:58:13 +00:00
|
|
|
return MainInstance();
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
private:
|
|
|
|
class RequestWrap {
|
|
|
|
public:
|
2017-10-29 15:32:01 +00:00
|
|
|
RequestWrap(
|
|
|
|
Instance *instance,
|
|
|
|
mtpRequestId requestId) noexcept
|
|
|
|
: _id(requestId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestWrap(const RequestWrap &other) = delete;
|
|
|
|
RequestWrap &operator=(const RequestWrap &other) = delete;
|
|
|
|
RequestWrap(RequestWrap &&other) : _id(base::take(other._id)) {
|
|
|
|
}
|
|
|
|
RequestWrap &operator=(RequestWrap &&other) {
|
2017-10-30 19:24:20 +00:00
|
|
|
if (_id != other._id) {
|
|
|
|
cancelRequest();
|
|
|
|
_id = base::take(other._id);
|
|
|
|
}
|
2017-10-29 15:32:01 +00:00
|
|
|
return *this;
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId id() const noexcept {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
void handled() const noexcept {
|
|
|
|
}
|
|
|
|
|
|
|
|
~RequestWrap() {
|
2017-10-29 15:32:01 +00:00
|
|
|
cancelRequest();
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-29 15:32:01 +00:00
|
|
|
void cancelRequest() {
|
|
|
|
if (_id) {
|
|
|
|
if (auto instance = MainInstance()) {
|
|
|
|
instance->cancel(_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
mtpRequestId _id = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RequestWrapComparator {
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
|
|
|
|
struct helper {
|
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
|
|
|
|
helper() = default;
|
|
|
|
helper(const helper &other) = default;
|
|
|
|
helper(mtpRequestId requestId) noexcept : requestId(requestId) {
|
|
|
|
}
|
|
|
|
helper(const RequestWrap &request) noexcept : requestId(request.id()) {
|
|
|
|
}
|
|
|
|
bool operator<(helper other) const {
|
|
|
|
return requestId < other.requestId;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
bool operator()(const helper &&lhs, const helper &&rhs) const {
|
|
|
|
return lhs < rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Request>
|
|
|
|
friend class SpecialRequestBuilder;
|
|
|
|
friend class RequestBuilder;
|
|
|
|
friend class RequestWrap;
|
|
|
|
friend class SentRequestWrap;
|
|
|
|
|
2017-08-14 12:48:11 +00:00
|
|
|
void senderRequestRegister(mtpRequestId requestId) {
|
2017-05-30 16:58:13 +00:00
|
|
|
_requests.emplace(MainInstance(), requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
2017-08-14 12:48:11 +00:00
|
|
|
void senderRequestHandled(mtpRequestId requestId) {
|
2017-03-22 15:38:40 +00:00
|
|
|
auto it = _requests.find(requestId);
|
|
|
|
if (it != _requests.cend()) {
|
|
|
|
it->handled();
|
|
|
|
_requests.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 12:48:11 +00:00
|
|
|
void senderRequestCancel(mtpRequestId requestId) {
|
2017-03-22 15:38:40 +00:00
|
|
|
auto it = _requests.find(requestId);
|
|
|
|
if (it != _requests.cend()) {
|
|
|
|
_requests.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
base::flat_set<RequestWrap, RequestWrapComparator> _requests;
|
2017-03-22 15:38:40 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Request, typename, typename>
|
|
|
|
Sender::SpecificRequestBuilder<Request> Sender::request(Request &&request) noexcept {
|
2017-03-31 15:50:02 +00:00
|
|
|
return SpecificRequestBuilder<Request>(this, std::move(request));
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Sender::SentRequestWrap Sender::request(mtpRequestId requestId) noexcept {
|
2017-03-31 15:50:02 +00:00
|
|
|
return SentRequestWrap(this, requestId);
|
2017-03-22 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MTP
|