Skip to content

Commit

Permalink
Parse updates in another thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Jul 27, 2023
1 parent 56adf14 commit 32043df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 4 additions & 8 deletions td/telegram/Td.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3070,20 +3070,16 @@ std::shared_ptr<Td::ResultHandler> Td::extract_handler(uint64 id) {
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) {
return;
}

TlBufferParser parser(&update);
auto ptr = 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_manager_->schedule_get_difference("failed to fetch update");
if (updates == nullptr) {
updates_manager_->schedule_get_difference("failed to fetch updates");
} else {
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()) {
alarm_timeout_.set_timeout_in(PING_SERVER_ALARM_ID,
PING_SERVER_TIMEOUT + Random::fast(0, PING_SERVER_TIMEOUT / 5));
Expand Down
2 changes: 1 addition & 1 deletion td/telegram/Td.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Td final : public Actor {

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);

Expand Down
12 changes: 11 additions & 1 deletion td/telegram/net/SessionProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
#include "td/telegram/net/Session.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UniqueId.h"

#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Time.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_parsers.h"

namespace td {

Expand Down Expand Up @@ -63,7 +66,14 @@ class SessionCallback final : public Session::Callback {
}

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 {
Expand Down

0 comments on commit 32043df

Please sign in to comment.