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/NetQueryCreator.h"
|
|
|
|
|
2020-03-16 02:09:17 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2020-03-15 23:01:14 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-08-06 18:04:21 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Gzip.h"
|
2018-07-17 13:27:24 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-08-10 20:54:17 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2020-03-15 23:01:14 +01:00
|
|
|
#include "td/utils/Storer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2020-03-15 23:01:14 +01:00
|
|
|
NetQueryPtr NetQueryCreator::create(const telegram_api::Function &function, DcId dc_id, NetQuery::Type type) {
|
|
|
|
return create(UniqueId::next(), function, dc_id, type, NetQuery::AuthFlag::On);
|
2020-03-15 22:17:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 23:01:14 +01:00
|
|
|
NetQueryPtr NetQueryCreator::create(uint64 id, const telegram_api::Function &function, DcId dc_id, NetQuery::Type type,
|
2020-03-15 03:22:43 +01:00
|
|
|
NetQuery::AuthFlag auth_flag) {
|
2020-03-15 23:01:14 +01:00
|
|
|
LOG(DEBUG) << "Create query " << to_string(function);
|
|
|
|
auto storer = DefaultStorer<telegram_api::Function>(function);
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice slice(storer.size());
|
2018-07-06 22:33:11 +02:00
|
|
|
auto real_size = storer.store(slice.as_slice().ubegin());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(real_size == slice.size()) << real_size << " " << slice.size() << " "
|
2019-02-12 22:28:47 +01:00
|
|
|
<< format::as_hex_dump<4>(Slice(slice.as_slice()));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int32 tl_constructor = NetQuery::tl_magic(slice);
|
2020-03-15 00:49:10 +01:00
|
|
|
|
|
|
|
size_t MIN_GZIPPED_SIZE = 128;
|
|
|
|
auto gzip_flag = slice.size() < MIN_GZIPPED_SIZE ? NetQuery::GzipFlag::Off : NetQuery::GzipFlag::On;
|
|
|
|
if (slice.size() >= 16384) {
|
|
|
|
// test compression ratio for the middle part
|
|
|
|
// if it is less than 0.9, then try to compress the whole request
|
|
|
|
size_t TESTED_SIZE = 1024;
|
|
|
|
BufferSlice compressed_part = gzencode(slice.as_slice().substr((slice.size() - TESTED_SIZE) / 2, TESTED_SIZE), 0.9);
|
|
|
|
if (compressed_part.empty()) {
|
|
|
|
gzip_flag = NetQuery::GzipFlag::Off;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (gzip_flag == NetQuery::GzipFlag::On) {
|
2020-03-14 23:56:48 +01:00
|
|
|
BufferSlice compressed = gzencode(slice.as_slice(), 0.9);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (compressed.empty()) {
|
|
|
|
gzip_flag = NetQuery::GzipFlag::Off;
|
|
|
|
} else {
|
|
|
|
slice = std::move(compressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 02:09:17 +01:00
|
|
|
double total_timeout_limit = 60;
|
|
|
|
if (!G()->close_flag() && G()->td().get_actor_unsafe()->auth_manager_->is_bot()) {
|
|
|
|
total_timeout_limit = 8;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = object_pool_.create(NetQuery::State::Query, id, std::move(slice), BufferSlice(), dc_id, type, auth_flag,
|
2020-03-16 02:09:17 +01:00
|
|
|
gzip_flag, tl_constructor, total_timeout_limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_cancellation_token(query.generation());
|
|
|
|
return query;
|
|
|
|
}
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|