diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index 990b1504..7ad4f4fd 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -18,6 +18,9 @@ #include #include +#include +#include +#include namespace td { @@ -108,28 +111,28 @@ class Client::Impl final { class MultiTd : public Actor { public: - void create(int td_id, unique_ptr callback) { + void create(int32 td_id, unique_ptr callback) { auto &td = tds_[td_id]; CHECK(td.empty()); string name = "Td"; if (td_id != 0) { - name += PSTRING() << "#" << td_id; + name += PSTRING() << '#' << td_id; } td = create_actor(name, std::move(callback)); } - void send(int td_id, Client::Request request) { + void send(int32 td_id, Client::Request request) { auto &td = tds_[td_id]; CHECK(!td.empty()); send_closure(td, &Td::request, request.id, std::move(request.function)); } - void destroy(int td_id) { + void destroy(int32 td_id) { auto size = tds_.erase(td_id); CHECK(size == 1); } private: - std::unordered_map > tds_; + std::unordered_map > tds_; }; class MultiImpl { @@ -145,6 +148,7 @@ class MultiImpl { } return res; } + MultiImpl() { concurrent_scheduler_ = std::make_shared(); concurrent_scheduler_->init(3); @@ -161,17 +165,25 @@ class MultiImpl { concurrent_scheduler->finish(); }); } + MultiImpl(const MultiImpl &) = delete; + MultiImpl &operator=(const MultiImpl &) = delete; + MultiImpl(MultiImpl &&) = delete; + MultiImpl &operator=(MultiImpl &&) = delete; + int32 create_id() { return id_.fetch_add(1) + 1; } - void create(int32 td_id, td::unique_ptr callback) { + + void create(int32 td_id, unique_ptr callback) { auto guard = concurrent_scheduler_->get_send_guard(); send_closure(multi_td_, &MultiTd::create, td_id, std::move(callback)); } + void send(int32 td_id, Client::Request request) { auto guard = concurrent_scheduler_->get_send_guard(); send_closure(multi_td_, &MultiTd::send, td_id, std::move(request)); } + void destroy(int32 td_id) { auto guard = concurrent_scheduler_->get_send_guard(); send_closure(multi_td_, &MultiTd::destroy, td_id); @@ -188,12 +200,11 @@ class MultiImpl { private: std::shared_ptr concurrent_scheduler_; - td::thread scheduler_thread_; - td::ActorOwn multi_td_; + thread scheduler_thread_; + ActorOwn multi_td_; std::atomic id_{0}; }; -/*** Client::Impl ***/ class Client::Impl final { public: using OutputQueue = MpscPollableQueue; @@ -275,7 +286,7 @@ class Client::Impl final { if (output_queue_ready_cnt_ > 0) { output_queue_ready_cnt_--; auto res = output_queue_->reader_get_unsafe(); - if (!res.object) { + if (res.object == nullptr && res.id == 0) { is_closed_ = true; } return res; @@ -289,7 +300,6 @@ class Client::Impl final { }; #endif -/*** Client ***/ Client::Client() : impl_(std::make_unique()) { // At least it should be enough for everybody who uses TDLib init_openssl_threads();