2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/net/DcId.h"
|
|
|
|
#include "td/telegram/net/NetQueryCounter.h"
|
2020-07-30 16:28:56 +02:00
|
|
|
#include "td/telegram/net/NetQueryStats.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/SignalSlot.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/ObjectPool.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2020-03-16 20:00:44 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2022-01-28 17:12:20 +01:00
|
|
|
#include "td/utils/Span.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
2020-03-15 23:42:24 +01:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2019-08-16 13:57:41 +02:00
|
|
|
#include "td/utils/TsList.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
extern int VERBOSITY_NAME(net_query);
|
|
|
|
|
2022-02-05 21:28:43 +01:00
|
|
|
class ChainId;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class NetQuery;
|
|
|
|
using NetQueryPtr = ObjectPool<NetQuery>::OwnerPtr;
|
|
|
|
using NetQueryRef = ObjectPool<NetQuery>::WeakPtr;
|
|
|
|
|
|
|
|
class NetQueryCallback : public Actor {
|
|
|
|
public:
|
|
|
|
virtual void on_result(NetQueryPtr query);
|
|
|
|
virtual void on_result_resendable(NetQueryPtr query, Promise<NetQueryPtr> promise);
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class NetQuery final : public TsListNode<NetQueryDebug> {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
NetQuery() = default;
|
|
|
|
|
|
|
|
enum class State : int8 { Empty, Query, OK, Error };
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class Type : int8 { Common, Upload, Download, DownloadSmall };
|
2018-12-31 20:04:05 +01:00
|
|
|
enum class AuthFlag : int8 { Off, On };
|
|
|
|
enum class GzipFlag : int8 { Off, On };
|
2021-05-31 20:51:48 +02:00
|
|
|
enum Error : int32 { Resend = 202, Canceled = 203, ResendInvokeAfter = 204 };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
uint64 id() const {
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
DcId dc_id() const {
|
|
|
|
return dc_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipFlag gzip_flag() const {
|
|
|
|
return gzip_flag_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthFlag auth_flag() const {
|
|
|
|
return auth_flag_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 tl_constructor() const {
|
|
|
|
return tl_constructor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resend(DcId new_dc_id) {
|
|
|
|
VLOG(net_query) << "Resend" << *this;
|
2019-08-16 13:57:41 +02:00
|
|
|
{
|
|
|
|
auto guard = lock();
|
2020-06-10 01:03:09 +02:00
|
|
|
get_data_unsafe().resend_count_++;
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
dc_id_ = new_dc_id;
|
|
|
|
status_ = Status::OK();
|
|
|
|
state_ = State::Query;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resend() {
|
|
|
|
resend(dc_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice &query() {
|
|
|
|
return query_;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice &ok() {
|
|
|
|
CHECK(state_ == State::OK);
|
|
|
|
return answer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BufferSlice &ok() const {
|
|
|
|
CHECK(state_ == State::OK);
|
|
|
|
return answer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status &error() {
|
|
|
|
CHECK(state_ == State::Error);
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Status &error() const {
|
|
|
|
CHECK(state_ == State::Error);
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice move_as_ok() {
|
|
|
|
auto ok = std::move(answer_);
|
|
|
|
clear();
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
Status move_as_error() TD_WARN_UNUSED_RESULT {
|
|
|
|
auto status = std::move(status_);
|
|
|
|
clear();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_ok(BufferSlice slice) {
|
|
|
|
VLOG(net_query) << "Got answer " << *this;
|
|
|
|
CHECK(state_ == State::Query);
|
|
|
|
answer_ = std::move(slice);
|
|
|
|
state_ = State::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_net_write(size_t size);
|
|
|
|
void on_net_read(size_t size);
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
void set_error(Status status, string source = string());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_error_resend() {
|
|
|
|
set_error_impl(Status::Error<Error::Resend>());
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:51:48 +02:00
|
|
|
void set_error_canceled() {
|
|
|
|
set_error_impl(Status::Error<Error::Canceled>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_resend_invoke_after() {
|
|
|
|
set_error_impl(Status::Error<Error::ResendInvokeAfter>());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool update_is_ready() {
|
|
|
|
if (state_ == State::Query) {
|
|
|
|
if (cancellation_token_.load(std::memory_order_relaxed) == 0 || cancel_slot_.was_signal()) {
|
2021-05-31 20:51:48 +02:00
|
|
|
set_error_canceled();
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_ready() const {
|
|
|
|
return state_ != State::Query;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const {
|
|
|
|
return state_ == State::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_ok() const {
|
|
|
|
return state_ == State::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 ok_tl_constructor() const {
|
|
|
|
return tl_magic(answer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ignore() const {
|
|
|
|
status_.ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 session_id() const {
|
|
|
|
return session_id_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
void set_session_id(uint64 session_id) {
|
|
|
|
session_id_.store(session_id, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 message_id() const {
|
|
|
|
return message_id_;
|
|
|
|
}
|
|
|
|
void set_message_id(uint64 message_id) {
|
|
|
|
message_id_ = message_id;
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:06:54 +01:00
|
|
|
Span<NetQueryRef> invoke_after() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return invoke_after_;
|
|
|
|
}
|
2022-01-28 15:06:54 +01:00
|
|
|
void set_invoke_after(std::vector<NetQueryRef> refs) {
|
|
|
|
invoke_after_ = std::move(refs);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
uint32 session_rand() const {
|
2022-02-03 20:32:47 +01:00
|
|
|
if (in_sequence_dispacher_ && !chain_ids_.empty()) {
|
|
|
|
return static_cast<uint32>(chain_ids_[0] >> 10);
|
2022-02-01 16:51:20 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cancel(int32 cancellation_token) {
|
|
|
|
cancellation_token_.compare_exchange_strong(cancellation_token, 0, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
void set_cancellation_token(int32 cancellation_token) {
|
|
|
|
cancellation_token_.store(cancellation_token, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2019-08-16 13:57:41 +02:00
|
|
|
if (!is_ready()) {
|
|
|
|
auto guard = lock();
|
2020-06-10 01:03:09 +02:00
|
|
|
LOG(ERROR) << "Destroy not ready query " << *this << " " << tag("state", get_data_unsafe().state_);
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: CHECK if net_query is lost here
|
|
|
|
cancel_slot_.close();
|
|
|
|
*this = NetQuery();
|
|
|
|
}
|
|
|
|
bool empty() const {
|
2020-07-30 13:23:53 +02:00
|
|
|
return state_ == State::Empty || !nq_counter_ || may_be_lost_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop_track() {
|
|
|
|
nq_counter_ = NetQueryCounter();
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug_send_failed() {
|
2019-08-16 13:57:41 +02:00
|
|
|
auto guard = lock();
|
2020-06-10 01:03:09 +02:00
|
|
|
get_data_unsafe().send_failed_count_++;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-04-17 22:21:56 +02:00
|
|
|
void debug(string state, bool may_be_lost = false);
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void set_callback(ActorShared<NetQueryCallback> callback) {
|
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<NetQueryCallback> move_callback() {
|
|
|
|
return std::move(callback_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_migrate(int32 sched_id) {
|
|
|
|
using ::td::start_migrate;
|
|
|
|
start_migrate(cancel_slot_, sched_id);
|
|
|
|
}
|
|
|
|
void finish_migrate() {
|
|
|
|
using ::td::finish_migrate;
|
|
|
|
finish_migrate(cancel_slot_);
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:13:18 +02:00
|
|
|
int8 priority() const {
|
|
|
|
return priority_;
|
|
|
|
}
|
|
|
|
void set_priority(int8 priority) {
|
|
|
|
priority_ = priority;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:32:47 +01:00
|
|
|
Span<uint64> get_chain_ids() const {
|
|
|
|
return chain_ids_;
|
2022-02-01 16:51:20 +01:00
|
|
|
}
|
2022-02-03 20:32:47 +01:00
|
|
|
|
|
|
|
void set_in_sequence_dispatcher(bool in_sequence_dispacher) {
|
|
|
|
in_sequence_dispacher_ = in_sequence_dispacher;
|
2022-02-01 16:51:20 +01:00
|
|
|
}
|
|
|
|
bool in_sequence_dispatcher() const {
|
|
|
|
return in_sequence_dispacher_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
State state_ = State::Empty;
|
2018-10-26 16:11:20 +02:00
|
|
|
Type type_ = Type::Common;
|
|
|
|
AuthFlag auth_flag_ = AuthFlag::Off;
|
|
|
|
GzipFlag gzip_flag_ = GzipFlag::Off;
|
2018-12-31 20:04:05 +01:00
|
|
|
DcId dc_id_;
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
NetQueryCounter nq_counter_;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status_;
|
2018-10-26 16:11:20 +02:00
|
|
|
uint64 id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice query_;
|
|
|
|
BufferSlice answer_;
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 tl_constructor_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-03 20:32:47 +01:00
|
|
|
vector<NetQueryRef> invoke_after_;
|
|
|
|
vector<uint64> chain_ids_;
|
2020-06-10 01:44:54 +02:00
|
|
|
|
2022-02-01 16:51:20 +01:00
|
|
|
bool in_sequence_dispacher_ = false;
|
2020-06-10 01:44:54 +02:00
|
|
|
bool may_be_lost_ = false;
|
2020-08-17 15:13:18 +02:00
|
|
|
int8 priority_{0};
|
2020-06-10 01:44:54 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
2021-07-04 04:58:54 +02:00
|
|
|
struct movable_atomic final : public std::atomic<T> {
|
2018-12-31 20:04:05 +01:00
|
|
|
movable_atomic() = default;
|
|
|
|
movable_atomic(T &&x) : std::atomic<T>(std::forward<T>(x)) {
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
movable_atomic(movable_atomic &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
this->store(other.load(std::memory_order_relaxed), std::memory_order_relaxed);
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
movable_atomic &operator=(movable_atomic &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
this->store(other.load(std::memory_order_relaxed), std::memory_order_relaxed);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
movable_atomic(const movable_atomic &other) = delete;
|
|
|
|
movable_atomic &operator=(const movable_atomic &other) = delete;
|
|
|
|
~movable_atomic() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
movable_atomic<uint64> session_id_{0};
|
2019-11-26 21:47:13 +01:00
|
|
|
uint64 message_id_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
movable_atomic<int32> cancellation_token_{-1}; // == 0 if query is canceled
|
|
|
|
ActorShared<NetQueryCallback> callback_;
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
void set_error_impl(Status status, string source = string()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
VLOG(net_query) << "Got error " << *this << " " << status;
|
|
|
|
status_ = std::move(status);
|
|
|
|
state_ = State::Error;
|
|
|
|
source_ = std::move(source);
|
|
|
|
}
|
|
|
|
|
2020-08-04 01:24:12 +02:00
|
|
|
static int32 tl_magic(const BufferSlice &buffer_slice);
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
public:
|
2022-02-04 15:05:35 +01:00
|
|
|
int32 next_timeout_ = 1; // for NetQueryDelayer
|
|
|
|
int32 total_timeout_ = 0; // for NetQueryDelayer/SequenceDispatcher
|
|
|
|
int32 total_timeout_limit_ = 60; // for NetQueryDelayer/SequenceDispatcher and to be set by caller
|
|
|
|
int32 last_timeout_ = 0; // for NetQueryDelayer/SequenceDispatcher
|
|
|
|
string source_; // for NetQueryDelayer/SequenceDispatcher
|
|
|
|
int32 dispatch_ttl_ = -1; // for NetQueryDispatcher and to be set by caller
|
|
|
|
int32 file_type_ = -1; // to be set by caller
|
|
|
|
Slot cancel_slot_; // for Session and to be set by caller
|
|
|
|
Promise<> quick_ack_promise_; // for Session and to be set by caller
|
|
|
|
bool need_resend_on_503_ = true; // for NetQueryDispatcher and to be set by caller
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
NetQuery(State state, uint64 id, BufferSlice &&query, BufferSlice &&answer, DcId dc_id, Type type, AuthFlag auth_flag,
|
2022-04-17 22:21:56 +02:00
|
|
|
GzipFlag gzip_flag, int32 tl_constructor, int32 total_timeout_limit, NetQueryStats *stats,
|
|
|
|
vector<ChainId> chain_ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &stream, const NetQuery &net_query) {
|
|
|
|
stream << "[Query:";
|
|
|
|
stream << tag("id", net_query.id());
|
|
|
|
stream << tag("tl", format::as_hex(net_query.tl_constructor()));
|
|
|
|
if (!net_query.is_ready()) {
|
|
|
|
stream << tag("state", "Query");
|
|
|
|
} else if (net_query.is_error()) {
|
|
|
|
stream << tag("state", "Error");
|
|
|
|
stream << net_query.error();
|
|
|
|
} else if (net_query.is_ok()) {
|
|
|
|
stream << tag("state", "Result");
|
|
|
|
stream << tag("tl", format::as_hex(net_query.ok_tl_constructor()));
|
|
|
|
}
|
|
|
|
stream << "]";
|
|
|
|
return stream;
|
|
|
|
}
|
2020-03-15 23:01:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &stream, const NetQueryPtr &net_query_ptr) {
|
2022-01-28 13:50:59 +01:00
|
|
|
if (net_query_ptr.empty()) {
|
|
|
|
return stream << "[Query: null]";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return stream << *net_query_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cancel_query(NetQueryRef &ref) {
|
|
|
|
if (ref.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref->cancel(ref.generation());
|
|
|
|
}
|
|
|
|
|
2020-03-15 23:42:24 +01:00
|
|
|
template <class T>
|
|
|
|
Result<typename T::ReturnType> fetch_result(const BufferSlice &message) {
|
|
|
|
TlBufferParser parser(&message);
|
|
|
|
auto result = T::fetch_result(parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
|
|
|
|
const char *error = parser.get_error();
|
|
|
|
if (error != nullptr) {
|
|
|
|
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message.as_slice());
|
|
|
|
return Status::Error(500, Slice(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
Result<typename T::ReturnType> fetch_result(NetQueryPtr query) {
|
|
|
|
CHECK(!query.empty());
|
|
|
|
if (query->is_error()) {
|
|
|
|
return query->move_as_error();
|
|
|
|
}
|
|
|
|
auto buffer = query->move_as_ok();
|
|
|
|
return fetch_result<T>(buffer);
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:04:21 +02:00
|
|
|
template <class T>
|
|
|
|
Result<typename T::ReturnType> fetch_result(Result<NetQueryPtr> r_query) {
|
|
|
|
TRY_RESULT(query, std::move(r_query));
|
|
|
|
return fetch_result<T>(std::move(query));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline void NetQueryCallback::on_result(NetQueryPtr query) {
|
|
|
|
on_result_resendable(std::move(query), Auto());
|
|
|
|
}
|
2020-03-15 23:01:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline void NetQueryCallback::on_result_resendable(NetQueryPtr query, Promise<NetQueryPtr> promise) {
|
|
|
|
on_result(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void start_migrate(NetQueryPtr &net_query, int32 sched_id) {
|
|
|
|
net_query->start_migrate(sched_id);
|
|
|
|
}
|
2020-03-15 23:01:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline void finish_migrate(NetQueryPtr &net_query) {
|
|
|
|
net_query->finish_migrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|