Use ClientManager in C++ example.
This commit is contained in:
parent
012353aef5
commit
00abe2f401
@ -53,8 +53,10 @@ namespace td_api = td::td_api;
|
|||||||
class TdExample {
|
class TdExample {
|
||||||
public:
|
public:
|
||||||
TdExample() {
|
TdExample() {
|
||||||
td::Client::execute({0, td_api::make_object<td_api::setLogVerbosityLevel>(1)});
|
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
|
||||||
client_ = std::make_unique<td::Client>();
|
client_manager_ = std::make_unique<td::ClientManager>();
|
||||||
|
client_id_ = client_manager_->create_client();
|
||||||
|
send_query(td_api::make_object<td_api::getOption>("version"), {});
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -62,7 +64,7 @@ class TdExample {
|
|||||||
if (need_restart_) {
|
if (need_restart_) {
|
||||||
restart();
|
restart();
|
||||||
} else if (!are_authorized_) {
|
} else if (!are_authorized_) {
|
||||||
process_response(client_->receive(10));
|
process_response(client_manager_->receive(10));
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Enter action [q] quit [u] check for updates and request results [c] show chats [m <id> <text>] "
|
std::cout << "Enter action [q] quit [u] check for updates and request results [c] show chats [m <id> <text>] "
|
||||||
"send message [me] show self [l] logout: "
|
"send message [me] show self [l] logout: "
|
||||||
@ -80,7 +82,7 @@ class TdExample {
|
|||||||
if (action == "u") {
|
if (action == "u") {
|
||||||
std::cout << "Checking for updates..." << std::endl;
|
std::cout << "Checking for updates..." << std::endl;
|
||||||
while (true) {
|
while (true) {
|
||||||
auto response = client_->receive(0);
|
auto response = client_manager_->receive(0);
|
||||||
if (response.object) {
|
if (response.object) {
|
||||||
process_response(std::move(response));
|
process_response(std::move(response));
|
||||||
} else {
|
} else {
|
||||||
@ -131,7 +133,8 @@ class TdExample {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
using Object = td_api::object_ptr<td_api::Object>;
|
using Object = td_api::object_ptr<td_api::Object>;
|
||||||
std::unique_ptr<td::Client> client_;
|
std::unique_ptr<td::ClientManager> client_manager_;
|
||||||
|
std::int32_t client_id_{0};
|
||||||
|
|
||||||
td_api::object_ptr<td_api::AuthorizationState> authorization_state_;
|
td_api::object_ptr<td_api::AuthorizationState> authorization_state_;
|
||||||
bool are_authorized_{false};
|
bool are_authorized_{false};
|
||||||
@ -146,7 +149,7 @@ class TdExample {
|
|||||||
std::map<std::int64_t, std::string> chat_title_;
|
std::map<std::int64_t, std::string> chat_title_;
|
||||||
|
|
||||||
void restart() {
|
void restart() {
|
||||||
client_.reset();
|
client_manager_.reset();
|
||||||
*this = TdExample();
|
*this = TdExample();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,18 +158,18 @@ class TdExample {
|
|||||||
if (handler) {
|
if (handler) {
|
||||||
handlers_.emplace(query_id, std::move(handler));
|
handlers_.emplace(query_id, std::move(handler));
|
||||||
}
|
}
|
||||||
client_->send({query_id, std::move(f)});
|
client_manager_->send(client_id_, query_id, std::move(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_response(td::Client::Response response) {
|
void process_response(td::ClientManager::Response response) {
|
||||||
if (!response.object) {
|
if (!response.object) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//std::cout << response.id << " " << to_string(response.object) << std::endl;
|
//std::cout << response.request_id << " " << to_string(response.object) << std::endl;
|
||||||
if (response.id == 0) {
|
if (response.request_id == 0) {
|
||||||
return process_update(std::move(response.object));
|
return process_update(std::move(response.object));
|
||||||
}
|
}
|
||||||
auto it = handlers_.find(response.id);
|
auto it = handlers_.find(response.request_id);
|
||||||
if (it != handlers_.end()) {
|
if (it != handlers_.end()) {
|
||||||
it->second(std::move(response.object));
|
it->second(std::move(response.object));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user