2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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"
|
|
|
|
|
|
|
|
#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
|
|
|
|
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"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 NetQuery::get_my_id() {
|
|
|
|
return G()->get_my_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQuery::on_net_write(size_t size) {
|
|
|
|
if (file_type_ == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
G()->get_net_stats_file_callbacks().at(file_type_)->on_write(size);
|
|
|
|
}
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void NetQuery::on_net_read(size_t size) {
|
|
|
|
if (file_type_ == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
G()->get_net_stats_file_callbacks().at(file_type_)->on_read(size);
|
|
|
|
}
|
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) {
|
|
|
|
if (status.code() == Error::Resend || status.code() == Error::Cancelled ||
|
|
|
|
status.code() == Error::ResendInvokeAfter) {
|
|
|
|
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")) {
|
2018-06-08 20:42:04 +02:00
|
|
|
LOG(ERROR) << "Receive INPUT_METHOD_INVALID for query " << format::as_hex_dump<4>(Slice(query_.as_slice()));
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-06-10 00:47:17 +02:00
|
|
|
TsList<NetQueryDebug> &NetQuery::get_net_query_list() {
|
|
|
|
static TsList<NetQueryDebug> net_query_list;
|
|
|
|
return net_query_list;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void dump_pending_network_queries() {
|
|
|
|
auto n = NetQueryCounter::get_count();
|
|
|
|
LOG(WARNING) << tag("pending net queries", n);
|
|
|
|
|
|
|
|
decltype(n) i = 0;
|
|
|
|
bool was_gap = false;
|
2020-06-10 00:47:17 +02:00
|
|
|
auto &net_query_list = NetQuery::get_net_query_list();
|
|
|
|
auto guard = net_query_list.lock();
|
|
|
|
for (auto end = net_query_list.end(), cur = net_query_list.begin(); cur != end; cur = cur->get_next(), i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (i < 20 || i + 20 > n || i % (n / 20 + 1) == 0) {
|
|
|
|
if (was_gap) {
|
|
|
|
LOG(WARNING) << "...";
|
|
|
|
was_gap = false;
|
|
|
|
}
|
2020-06-10 00:38:00 +02:00
|
|
|
const NetQueryDebug &debug = cur->get_data_unsafe();
|
2020-06-10 01:03:09 +02:00
|
|
|
const NetQuery &nq = *static_cast<const NetQuery *>(cur);
|
2020-06-10 02:42:00 +02:00
|
|
|
LOG(WARNING) << tag("user", debug.my_id_) << nq << tag("total flood", format::as_time(nq.total_timeout_))
|
2020-06-10 00:38:00 +02:00
|
|
|
<< tag("since start", format::as_time(Time::now_cached() - debug.start_timestamp_))
|
2020-06-10 01:03:09 +02:00
|
|
|
<< tag("state", debug.state_)
|
|
|
|
<< tag("in this state", format::as_time(Time::now_cached() - debug.state_timestamp_))
|
|
|
|
<< tag("state changed", debug.state_change_count_) << tag("resend count", debug.resend_count_)
|
|
|
|
<< tag("fail count", debug.send_failed_count_) << tag("ack state", debug.ack_state_)
|
|
|
|
<< tag("unknown", debug.unknown_state_);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
was_gap = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|