Parse updates in another thread.

This commit is contained in:
levlam 2023-07-27 12:52:29 +03:00
parent 56adf14116
commit 32043df840
3 changed files with 16 additions and 10 deletions

View File

@ -3070,20 +3070,16 @@ std::shared_ptr<Td::ResultHandler> Td::extract_handler(uint64 id) {
return result; return result;
} }
void Td::on_update(BufferSlice &&update, uint64 auth_key_id) { void Td::on_update(telegram_api::object_ptr<telegram_api::Updates> updates, uint64 auth_key_id) {
if (close_flag_ > 1) { if (close_flag_ > 1) {
return; return;
} }
TlBufferParser parser(&update); if (updates == nullptr) {
auto ptr = telegram_api::Updates::fetch(parser); updates_manager_->schedule_get_difference("failed to fetch updates");
parser.fetch_end();
if (parser.get_error()) {
LOG(ERROR) << "Failed to fetch update: " << parser.get_error() << format::as_hex_dump<4>(update.as_slice());
updates_manager_->schedule_get_difference("failed to fetch update");
} else { } else {
updates_manager_->on_update_from_auth_key_id(auth_key_id); updates_manager_->on_update_from_auth_key_id(auth_key_id);
updates_manager_->on_get_updates(std::move(ptr), Promise<Unit>()); updates_manager_->on_get_updates(std::move(updates), Promise<Unit>());
if (auth_manager_->is_bot() && auth_manager_->is_authorized()) { if (auth_manager_->is_bot() && auth_manager_->is_authorized()) {
alarm_timeout_.set_timeout_in(PING_SERVER_ALARM_ID, alarm_timeout_.set_timeout_in(PING_SERVER_ALARM_ID,
PING_SERVER_TIMEOUT + Random::fast(0, PING_SERVER_TIMEOUT / 5)); PING_SERVER_TIMEOUT + Random::fast(0, PING_SERVER_TIMEOUT / 5));

View File

@ -121,7 +121,7 @@ class Td final : public Actor {
void reload_promo_data(); void reload_promo_data();
void on_update(BufferSlice &&update, uint64 auth_key_id); void on_update(telegram_api::object_ptr<telegram_api::Updates> updates, uint64 auth_key_id);
void on_result(NetQueryPtr query); void on_result(NetQueryPtr query);

View File

@ -14,10 +14,12 @@
#include "td/telegram/net/Session.h" #include "td/telegram/net/Session.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/TdDb.h" #include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UniqueId.h" #include "td/telegram/UniqueId.h"
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/HashTableUtils.h" #include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/Promise.h" #include "td/utils/Promise.h"
@ -25,6 +27,7 @@
#include "td/utils/SliceBuilder.h" #include "td/utils/SliceBuilder.h"
#include "td/utils/Time.h" #include "td/utils/Time.h"
#include "td/utils/tl_helpers.h" #include "td/utils/tl_helpers.h"
#include "td/utils/tl_parsers.h"
namespace td { namespace td {
@ -63,7 +66,14 @@ class SessionCallback final : public Session::Callback {
} }
void on_update(BufferSlice &&update, uint64 auth_key_id) final { void on_update(BufferSlice &&update, uint64 auth_key_id) final {
send_closure_later(G()->td(), &Td::on_update, std::move(update), auth_key_id); TlBufferParser parser(&update);
auto updates = telegram_api::Updates::fetch(parser);
parser.fetch_end();
if (parser.get_error()) {
LOG(ERROR) << "Failed to fetch update: " << parser.get_error() << format::as_hex_dump<4>(update.as_slice());
updates = nullptr;
}
send_closure_later(G()->td(), &Td::on_update, std::move(updates), auth_key_id);
} }
void on_result(NetQueryPtr query) final { void on_result(NetQueryPtr query) final {