From 65191605ca05e44408917540c24f15f73a4d0ec2 Mon Sep 17 00:00:00 2001 From: whyXVI <139840508+whyXVI@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:33:15 +0800 Subject: [PATCH 1/2] Fix response handlers for 'me', 'c', and related commands Resolve issues where handlers were not invoked after sending queries, enabling proper response processing --- example/cpp/td_example.cpp | 70 ++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/example/cpp/td_example.cpp b/example/cpp/td_example.cpp index 3f806d7b9049..270ec265e2c9 100644 --- a/example/cpp/td_example.cpp +++ b/example/cpp/td_example.cpp @@ -55,6 +55,15 @@ class TdExample { td::ClientManager::execute(td_api::make_object(1)); client_manager_ = std::make_unique(); client_id_ = client_manager_->create_client_id(); + + // auto proxy = td_api::make_object(); + // auto proxy_type = td_api::make_object(); + // proxy->server_ = "localhost"; + // proxy->port_ = 1234; + // proxy->enable_ = true; + // proxy->type_ = std::move(proxy_type); + // send_query(std::move(proxy), {}); + send_query(td_api::make_object("version"), {}); } @@ -63,7 +72,7 @@ class TdExample { if (need_restart_) { restart(); } else if (!are_authorized_) { - process_response(client_manager_->receive(10)); + fetch_query_response_handler(false,[](int i){std::cout<<"Not authorized"< " "] send message [me] show self [l] logout: " @@ -80,23 +89,17 @@ class TdExample { } if (action == "u") { std::cout << "Checking for updates..." << std::endl; - while (true) { - auto response = client_manager_->receive(0); - if (response.object) { - process_response(std::move(response)); - } else { - break; - } - } + fetch_query_response_handler(false); } else if (action == "close") { std::cout << "Closing..." << std::endl; send_query(td_api::make_object(), {}); } else if (action == "me") { send_query(td_api::make_object(), [this](Object object) { std::cout << to_string(object) << std::endl; }); + fetch_query_response_handler(); } else if (action == "l") { std::cout << "Logging out..." << std::endl; - send_query(td_api::make_object(), {}); + send_query(td_api::make_object(),{}); } else if (action == "m") { std::int64_t chat_id; ss >> chat_id; @@ -121,9 +124,15 @@ class TdExample { } auto chats = td::move_tl_object_as(object); for (auto chat_id : chats->chat_ids_) { - std::cout << "[chat_id:" << chat_id << "] [title:" << chat_title_[chat_id] << "]" << std::endl; + auto chat_info = td_api::make_object(chat_id); + send_query(std::move(chat_info), [this](Object object) { + auto chat = td::move_tl_object_as(object); + std::cout << "Chat title: " << chat->title_ << "chat id:" <id_ << std::endl; + }); + fetch_query_response_handler(); } }); + fetch_query_response_handler(); } } } @@ -160,13 +169,11 @@ class TdExample { } void process_response(td::ClientManager::Response response) { - if (!response.object) { - return; - } //std::cout << response.request_id << " " << to_string(response.object) << std::endl; if (response.request_id == 0) { return process_update(std::move(response.object)); } + auto it = handlers_.find(response.request_id); if (it != handlers_.end()) { it->second(std::move(response.object)); @@ -174,6 +181,41 @@ class TdExample { } } + + void fetch_query_response_handler(bool only_current_query = true, std::function iter_callback = [](int limit) { + std::cout << "Iter limit reached: " << limit << std::endl; + }, int iter_limit = 500000, float msg_fetch_timeout = 0.0f) { + int i_ = 0; + while (true) { + if (i_ > iter_limit) { + iter_callback(iter_limit); + return; + } + auto response = client_manager_->receive(msg_fetch_timeout); + if (!response.object) { + i_++; + continue; + } + if (only_current_query) { + if (response.request_id == current_query_id_) { + if (response.object->get_id() == td_api::error::ID) { + auto error = td::move_tl_object_as(response.object); + if (error) { + std::cout << "Error: " << to_string(error) << std::endl; + } + return; + } + process_response(std::move(response)); + return; + } + } else { + process_response(std::move(response)); + return; + } + i_++; + } + } + std::string get_user_name(std::int64_t user_id) const { auto it = users_.find(user_id); if (it == users_.end()) { From a79c911cee5d82f410b0f5d286c670928a4655c7 Mon Sep 17 00:00:00 2001 From: whyXVI <139840508+whyXVI@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:44:00 +0800 Subject: [PATCH 2/2] Fixed message updates flooding --- example/cpp/td_example.cpp | 81 +++++++++++--------------------------- 1 file changed, 24 insertions(+), 57 deletions(-) diff --git a/example/cpp/td_example.cpp b/example/cpp/td_example.cpp index 270ec265e2c9..5d8755e9fb62 100644 --- a/example/cpp/td_example.cpp +++ b/example/cpp/td_example.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 // // 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) @@ -55,15 +55,6 @@ class TdExample { td::ClientManager::execute(td_api::make_object(1)); client_manager_ = std::make_unique(); client_id_ = client_manager_->create_client_id(); - - // auto proxy = td_api::make_object(); - // auto proxy_type = td_api::make_object(); - // proxy->server_ = "localhost"; - // proxy->port_ = 1234; - // proxy->enable_ = true; - // proxy->type_ = std::move(proxy_type); - // send_query(std::move(proxy), {}); - send_query(td_api::make_object("version"), {}); } @@ -72,7 +63,7 @@ class TdExample { if (need_restart_) { restart(); } else if (!are_authorized_) { - fetch_query_response_handler(false,[](int i){std::cout<<"Not authorized"<receive(10)); } else { std::cout << "Enter action [q] quit [u] check for updates and request results [c] show chats [m " "] send message [me] show self [l] logout: " @@ -89,17 +80,29 @@ class TdExample { } if (action == "u") { std::cout << "Checking for updates..." << std::endl; - fetch_query_response_handler(false); + while (true) { + auto response = client_manager_->receive(0); + if (response.object) { + process_response(std::move(response)); + if (response.request_id != 0 && response.request_id == last_user_query_) { + std::cout << "Response to last user query_id: " << response.request_id << " received!" << std::endl; + break; + } + } else { + break; + } + } } else if (action == "close") { std::cout << "Closing..." << std::endl; send_query(td_api::make_object(), {}); } else if (action == "me") { send_query(td_api::make_object(), [this](Object object) { std::cout << to_string(object) << std::endl; }); - fetch_query_response_handler(); + last_user_query_ = current_query_id_; + std::cout << "user query_id sent: " << last_user_query_ << std::endl; } else if (action == "l") { std::cout << "Logging out..." << std::endl; - send_query(td_api::make_object(),{}); + send_query(td_api::make_object(), {}); } else if (action == "m") { std::int64_t chat_id; ss >> chat_id; @@ -124,15 +127,11 @@ class TdExample { } auto chats = td::move_tl_object_as(object); for (auto chat_id : chats->chat_ids_) { - auto chat_info = td_api::make_object(chat_id); - send_query(std::move(chat_info), [this](Object object) { - auto chat = td::move_tl_object_as(object); - std::cout << "Chat title: " << chat->title_ << "chat id:" <id_ << std::endl; - }); - fetch_query_response_handler(); + std::cout << "[chat_id:" << chat_id << "] [title:" << chat_title_[chat_id] << "]" << std::endl; } }); - fetch_query_response_handler(); + last_user_query_ = current_query_id_; + std::cout << "user query_id sent: " << last_user_query_ << std::endl; } } } @@ -148,6 +147,7 @@ class TdExample { bool need_restart_{false}; std::uint64_t current_query_id_{0}; std::uint64_t authentication_query_id_{0}; + std::uint64_t last_user_query_{0}; std::map> handlers_; @@ -169,11 +169,13 @@ class TdExample { } void process_response(td::ClientManager::Response response) { + if (!response.object) { + return; + } //std::cout << response.request_id << " " << to_string(response.object) << std::endl; if (response.request_id == 0) { return process_update(std::move(response.object)); } - auto it = handlers_.find(response.request_id); if (it != handlers_.end()) { it->second(std::move(response.object)); @@ -181,41 +183,6 @@ class TdExample { } } - - void fetch_query_response_handler(bool only_current_query = true, std::function iter_callback = [](int limit) { - std::cout << "Iter limit reached: " << limit << std::endl; - }, int iter_limit = 500000, float msg_fetch_timeout = 0.0f) { - int i_ = 0; - while (true) { - if (i_ > iter_limit) { - iter_callback(iter_limit); - return; - } - auto response = client_manager_->receive(msg_fetch_timeout); - if (!response.object) { - i_++; - continue; - } - if (only_current_query) { - if (response.request_id == current_query_id_) { - if (response.object->get_id() == td_api::error::ID) { - auto error = td::move_tl_object_as(response.object); - if (error) { - std::cout << "Error: " << to_string(error) << std::endl; - } - return; - } - process_response(std::move(response)); - return; - } - } else { - process_response(std::move(response)); - return; - } - i_++; - } - } - std::string get_user_name(std::int64_t user_id) const { auto it = users_.find(user_id); if (it == users_.end()) {