From c7aa5ed5443575df855ac8aa08d677c1887adb66 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 17 Jun 2018 21:29:46 +0100 Subject: [PATCH] _DEBUG-test ConcurrentSender generic handlers. --- .../SourceFiles/mtproto/concurrent_sender.h | 138 +++++++++++------- 1 file changed, 84 insertions(+), 54 deletions(-) diff --git a/Telegram/SourceFiles/mtproto/concurrent_sender.h b/Telegram/SourceFiles/mtproto/concurrent_sender.h index f8a5df8c81..7bb9d2f789 100644 --- a/Telegram/SourceFiles/mtproto/concurrent_sender.h +++ b/Telegram/SourceFiles/mtproto/concurrent_sender.h @@ -13,6 +13,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/flat_map.h" #include "mtproto/core_types.h" +#ifndef _DEBUG +#define MTP_SENDER_USE_GENERIC_HANDLERS +#endif // !_DEBUG + class RPCError; namespace MTP { @@ -21,7 +25,8 @@ class Instance; class ConcurrentSender : public base::has_weak_ptr { template - static constexpr bool is_callable_v = rpl::details::is_callable_v; + static constexpr bool is_callable_v + = rpl::details::is_callable_v; template auto with_instance(Method &&method) @@ -101,7 +106,7 @@ public: [[nodiscard]] SpecificRequestBuilder &afterDelay( TimeMs ms) noexcept; -#ifdef _DEBUG +#ifndef MTP_SENDER_USE_GENERIC_HANDLERS // Allow code completion to show response type. [[nodiscard]] SpecificRequestBuilder &done(FnMut &&handler); [[nodiscard]] SpecificRequestBuilder &done(FnMut &&handler); [[nodiscard]] SpecificRequestBuilder &fail(FnMut &&handler); -#else // _DEBUG +#else // !MTP_SENDER_USE_GENERIC_HANDLERS template [[nodiscard]] SpecificRequestBuilder &done(Handler &&handler); template [[nodiscard]] SpecificRequestBuilder &fail(Handler &&handler); -#endif // _DEBUG +#endif // MTP_SENDER_USE_GENERIC_HANDLERS [[nodiscard]] SpecificRequestBuilder &handleFloodErrors() noexcept; [[nodiscard]] SpecificRequestBuilder &handleAllErrors() noexcept; @@ -142,14 +147,14 @@ public: class SentRequestWrap { public: - void cancel() { - _sender->senderRequestCancel(_requestId); - } + void cancel(); private: friend class ConcurrentSender; - SentRequestWrap(not_null sender, mtpRequestId requestId) : _sender(sender), _requestId(requestId) { - } + SentRequestWrap( + not_null sender, + mtpRequestId requestId); + not_null _sender; mtpRequestId _requestId = 0; @@ -164,11 +169,7 @@ public: [[nodiscard]] SentRequestWrap request(mtpRequestId requestId) noexcept; - [[nodiscard]] auto requestCanceller() noexcept { - return [=](mtpRequestId requestId) { - request(requestId).cancel(); - }; - } + [[nodiscard]] auto requestCanceller() noexcept; ~ConcurrentSender(); @@ -197,7 +198,8 @@ private: template void ConcurrentSender::RequestBuilder::setDoneHandler( - InvokeFullDone &&invoke) noexcept { + InvokeFullDone &&invoke +) noexcept { _handlers.done = [handler = std::move(invoke)]( mtpRequestId requestId, bytes::const_span result) mutable { @@ -214,37 +216,40 @@ void ConcurrentSender::RequestBuilder::setDoneHandler( template void ConcurrentSender::RequestBuilder::setFailHandler( - InvokeFullFail &&invoke) noexcept { + InvokeFullFail &&invoke +) noexcept { _handlers.fail = std::move(invoke); } template ConcurrentSender::SpecificRequestBuilder::SpecificRequestBuilder( not_null sender, - Request &&request) noexcept -: RequestBuilder(sender, mtpRequestData::serialize(request)) { + Request &&request +) noexcept : RequestBuilder(sender, mtpRequestData::serialize(request)) { } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::toDC( - ShiftedDcId dcId) noexcept -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::toDC( + ShiftedDcId dcId +) noexcept -> SpecificRequestBuilder & { setToDC(dcId); return *this; } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::afterDelay( - TimeMs ms) noexcept -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::afterDelay( + TimeMs ms +) noexcept -> SpecificRequestBuilder & { setCanWait(ms); return *this; } -#ifdef _DEBUG +#ifndef MTP_SENDER_USE_GENERIC_HANDLERS // Allow code completion to show response type. template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::done( - FnMut &&handler) --> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::done( + FnMut &&handler +) -> SpecificRequestBuilder & { setDoneHandler([handler = std::move(handler)]( mtpRequestId requestId, Response &&result) mutable { @@ -254,16 +259,17 @@ template } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::done( - FnMut &&handler) --> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::done( + FnMut &&handler +) -> SpecificRequestBuilder & { setDoneHandler(std::move(handler)); return *this; } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::done( - FnMut &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::done( + FnMut &&handler +) -> SpecificRequestBuilder & { setDoneHandler([handler = std::move(handler)]( mtpRequestId requestId, Response &&result) mutable { @@ -273,8 +279,9 @@ template } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::done( - FnMut &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::done( + FnMut &&handler +) -> SpecificRequestBuilder & { setDoneHandler([handler = std::move(handler)]( mtpRequestId requestId, Response &&result) mutable { @@ -284,8 +291,9 @@ template } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::fail( - FnMut &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::fail( + FnMut &&handler +) -> SpecificRequestBuilder & { setFailHandler([handler = std::move(handler)]( mtpRequestId requestId, RPCError &&error) mutable { @@ -295,16 +303,17 @@ template } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::fail( - FnMut &&handler) --> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::fail( + FnMut &&handler +) -> SpecificRequestBuilder & { setFailHandler(std::move(handler)); return *this; } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::fail( - FnMut &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::fail( + FnMut &&handler +) -> SpecificRequestBuilder & { setFailHandler([handler = std::move(handler)]( mtpRequestId requestId, RPCError &&error) mutable { @@ -314,8 +323,9 @@ template } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::fail( - FnMut &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::fail( + FnMut &&handler +) -> SpecificRequestBuilder & { setFailHandler([handler = std::move(handler)]( mtpRequestId requestId, RPCError &&error) mutable { @@ -323,11 +333,12 @@ template }); return *this; } -#else // _DEBUG +#else // !MTP_SENDER_USE_GENERIC_HANDLERS template template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::done( - Handler &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::done( + Handler &&handler +) -> SpecificRequestBuilder & { using Response = typename Request::ResponseType; constexpr auto takesFull = rpl::details::is_callable_plain_v< Handler, @@ -369,8 +380,9 @@ template template template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::fail( - Handler &&handler) -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::fail( + Handler &&handler +) -> SpecificRequestBuilder & { constexpr auto takesFull = rpl::details::is_callable_plain_v< Handler, mtpRequestId, @@ -409,35 +421,53 @@ template return *this; } -#endif // _DEBUG +#endif // MTP_SENDER_USE_GENERIC_HANDLERS template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::handleFloodErrors() noexcept --> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::handleFloodErrors( +) noexcept -> SpecificRequestBuilder & { setFailSkipPolicy(FailSkipPolicy::HandleFlood); return *this; } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::handleAllErrors() noexcept --> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::handleAllErrors( +) noexcept -> SpecificRequestBuilder & { setFailSkipPolicy(FailSkipPolicy::HandleAll); return *this; } template -[[nodiscard]] auto ConcurrentSender::SpecificRequestBuilder::afterRequest( - mtpRequestId requestId) noexcept -> SpecificRequestBuilder & { +auto ConcurrentSender::SpecificRequestBuilder::afterRequest( + mtpRequestId requestId +) noexcept -> SpecificRequestBuilder & { setAfter(requestId); return *this; } +inline void ConcurrentSender::SentRequestWrap::cancel() { + _sender->senderRequestCancel(_requestId); +} + +inline ConcurrentSender::SentRequestWrap::SentRequestWrap( + not_null sender, + mtpRequestId requestId +) : _sender(sender) +, _requestId(requestId) { +} + template inline auto ConcurrentSender::request(Request &&request) noexcept -> SpecificRequestBuilder { return SpecificRequestBuilder(this, std::move(request)); } +inline auto ConcurrentSender::requestCanceller() noexcept { + return [=](mtpRequestId requestId) { + request(requestId).cancel(); + }; +} + inline auto ConcurrentSender::request(mtpRequestId requestId) noexcept -> SentRequestWrap { return SentRequestWrap(this, requestId);