2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
|
2022-02-05 21:28:43 +01:00
|
|
|
#include "td/telegram/ChainId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2019-02-19 19:15:08 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-05 21:28:43 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/utils/misc.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2022-06-02 15:19:16 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
|
2022-02-08 15:30:06 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
int VERBOSITY_NAME(net_query) = VERBOSITY_NAME(INFO);
|
|
|
|
|
2022-04-17 22:21:56 +02:00
|
|
|
void NetQuery::debug(string state, bool may_be_lost) {
|
|
|
|
may_be_lost_ = may_be_lost;
|
|
|
|
VLOG(net_query) << *this << " " << tag("state", state);
|
|
|
|
{
|
|
|
|
auto guard = lock();
|
|
|
|
auto &data = get_data_unsafe();
|
|
|
|
data.state_ = std::move(state);
|
|
|
|
data.state_timestamp_ = Time::now();
|
|
|
|
data.state_change_count_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 17:13:35 +02:00
|
|
|
NetQuery::NetQuery(uint64 id, BufferSlice &&query, DcId dc_id, Type type, AuthFlag auth_flag, GzipFlag gzip_flag,
|
|
|
|
int32 tl_constructor, int32 total_timeout_limit, NetQueryStats *stats, vector<ChainId> chain_ids)
|
|
|
|
: state_(State::Query)
|
2022-04-17 22:21:56 +02:00
|
|
|
, type_(type)
|
|
|
|
, auth_flag_(auth_flag)
|
|
|
|
, gzip_flag_(gzip_flag)
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, status_()
|
|
|
|
, id_(id)
|
|
|
|
, query_(std::move(query))
|
|
|
|
, tl_constructor_(tl_constructor)
|
|
|
|
, total_timeout_limit_(total_timeout_limit) {
|
|
|
|
CHECK(id_ != 0);
|
2022-02-05 22:16:16 +01:00
|
|
|
chain_ids_ = transform(chain_ids, [](ChainId chain_id) { return chain_id.get() == 0 ? 1 : chain_id.get(); });
|
2022-02-05 21:28:43 +01:00
|
|
|
td::unique(chain_ids_);
|
2022-04-17 22:21:56 +02:00
|
|
|
|
|
|
|
auto &data = get_data_unsafe();
|
2022-08-18 19:21:40 +02:00
|
|
|
data.my_id_ = G()->get_option_integer("my_id");
|
2022-04-17 22:21:56 +02:00
|
|
|
data.start_timestamp_ = data.state_timestamp_ = Time::now();
|
|
|
|
LOG(INFO) << *this;
|
|
|
|
if (stats) {
|
|
|
|
nq_counter_ = stats->register_query(this);
|
|
|
|
}
|
2022-02-05 21:28:43 +01:00
|
|
|
}
|
|
|
|
|
2024-05-15 14:05:36 +02:00
|
|
|
void NetQuery::clear() {
|
|
|
|
if (!is_ready()) {
|
|
|
|
auto guard = lock();
|
|
|
|
LOG(ERROR) << "Destroy not ready query " << *this << " " << tag("state", get_data_unsafe().state_);
|
|
|
|
}
|
|
|
|
// TODO: CHECK if net_query is lost here
|
|
|
|
cancel_slot_.close();
|
|
|
|
*this = NetQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQuery::resend(DcId new_dc_id) {
|
|
|
|
VLOG(net_query) << "Resend " << *this;
|
|
|
|
{
|
|
|
|
auto guard = lock();
|
|
|
|
get_data_unsafe().resend_count_++;
|
|
|
|
}
|
|
|
|
dc_id_ = new_dc_id;
|
|
|
|
status_ = Status::OK();
|
|
|
|
state_ = State::Query;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetQuery::update_is_ready() {
|
|
|
|
if (state_ == State::Query) {
|
|
|
|
if (cancellation_token_.load(std::memory_order_relaxed) == 0 || cancel_slot_.was_signal()) {
|
|
|
|
set_error_canceled();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQuery::set_ok(BufferSlice slice) {
|
|
|
|
VLOG(net_query) << "Receive answer " << *this;
|
|
|
|
CHECK(state_ == State::Query);
|
|
|
|
answer_ = std::move(slice);
|
|
|
|
state_ = State::OK;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void NetQuery::on_net_write(size_t size) {
|
2023-03-13 15:26:23 +01:00
|
|
|
const auto &callbacks = G()->get_net_stats_file_callbacks();
|
|
|
|
if (static_cast<size_t>(file_type_) < callbacks.size()) {
|
|
|
|
callbacks[file_type_]->on_write(size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void NetQuery::on_net_read(size_t size) {
|
2023-03-13 15:26:23 +01:00
|
|
|
const auto &callbacks = G()->get_net_stats_file_callbacks();
|
|
|
|
if (static_cast<size_t>(file_type_) < callbacks.size()) {
|
|
|
|
callbacks[file_type_]->on_read(size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 NetQuery::tl_magic(const BufferSlice &buffer_slice) {
|
|
|
|
auto slice = buffer_slice.as_slice();
|
|
|
|
if (slice.size() < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return as<int32>(slice.begin());
|
|
|
|
}
|
|
|
|
|
2018-06-08 20:42:04 +02:00
|
|
|
void NetQuery::set_error(Status status, string source) {
|
2021-05-31 20:51:48 +02:00
|
|
|
if (status.code() == Error::Resend || status.code() == Error::Canceled || status.code() == Error::ResendInvokeAfter) {
|
2018-06-08 20:42:04 +02:00
|
|
|
return set_error_impl(Status::Error(200, PSLICE() << status), std::move(source));
|
|
|
|
}
|
|
|
|
|
2018-06-24 23:52:17 +02:00
|
|
|
if (begins_with(status.message(), "INPUT_METHOD_INVALID")) {
|
2023-02-21 09:21:07 +01:00
|
|
|
LOG(ERROR) << "Receive INPUT_METHOD_INVALID for query " << format::as_hex_dump<4>(query_.as_slice());
|
2018-06-08 20:42:04 +02:00
|
|
|
}
|
|
|
|
if (status.message() == "BOT_METHOD_INVALID") {
|
2019-02-19 19:15:08 +01:00
|
|
|
auto id = tl_constructor();
|
2020-04-28 15:25:56 +02:00
|
|
|
if (id != telegram_api::help_getNearestDc::ID && id != telegram_api::help_getAppConfig::ID) {
|
2019-02-19 19:15:08 +01:00
|
|
|
LOG(ERROR) << "Receive BOT_METHOD_INVALID for query " << format::as_hex(id);
|
|
|
|
}
|
2018-06-08 20:42:04 +02:00
|
|
|
}
|
2018-06-21 21:32:56 +02:00
|
|
|
if (status.message() == "MSG_WAIT_FAILED" && status.code() != 400) {
|
|
|
|
status = Status::Error(400, "MSG_WAIT_FAILED");
|
|
|
|
}
|
2018-06-08 20:42:04 +02:00
|
|
|
set_error_impl(std::move(status), std::move(source));
|
|
|
|
}
|
|
|
|
|
2024-05-15 14:05:36 +02:00
|
|
|
void NetQuery::set_error_impl(Status status, string source) {
|
|
|
|
VLOG(net_query) << "Receive error " << *this << " " << status;
|
|
|
|
status_ = std::move(status);
|
|
|
|
state_ = State::Error;
|
|
|
|
source_ = std::move(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
auto message_id = net_query.message_id();
|
|
|
|
if (message_id != 0) {
|
|
|
|
stream << tag("msg_id", format::as_hex(message_id));
|
|
|
|
}
|
|
|
|
if (net_query.is_error()) {
|
|
|
|
stream << net_query.error();
|
|
|
|
} else if (net_query.is_ok()) {
|
|
|
|
stream << tag("result_tl", format::as_hex(net_query.ok_tl_constructor()));
|
|
|
|
}
|
|
|
|
stream << ']';
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &stream, const NetQueryPtr &net_query_ptr) {
|
|
|
|
if (net_query_ptr.empty()) {
|
|
|
|
return stream << "[Query: null]";
|
|
|
|
}
|
|
|
|
return stream << *net_query_ptr;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|