Update Client.cpp

This commit is contained in:
Andrea Cavalli 2020-09-02 11:15:33 +02:00
parent 5ae1a9dcda
commit 7a99da836d

View File

@ -91,16 +91,20 @@ class TdReceiver {
} }
void on_result(uint64 id, td_api::object_ptr<td_api::Object> result) override { void on_result(uint64 id, td_api::object_ptr<td_api::Object> result) override {
if (id == 0) { if (id == 0) {
impl_->responses_.push({client_id_, id, nullptr});
impl_->updates_.push({client_id_, 0, std::move(result)}); impl_->updates_.push({client_id_, 0, std::move(result)});
} else { } else {
impl_->responses_.push({client_id_, id, std::move(result)}); impl_->responses_.push({client_id_, id, std::move(result)});
impl_->updates_.push({client_id_, id, nullptr});
} }
} }
void on_error(uint64 id, td_api::object_ptr<td_api::error> error) override { void on_error(uint64 id, td_api::object_ptr<td_api::error> error) override {
if (id == 0) { if (id == 0) {
impl_->responses_.push({client_id_, 0, nullptr});
impl_->updates_.push({client_id_, 0, std::move(error)}); impl_->updates_.push({client_id_, 0, std::move(error)});
} else { } else {
impl_->responses_.push({client_id_, id, std::move(error)}); impl_->responses_.push({client_id_, id, std::move(error)});
impl_->updates_.push({client_id_, id, nullptr});
} }
} }
Callback(const Callback &) = delete; Callback(const Callback &) = delete;
@ -108,6 +112,7 @@ class TdReceiver {
Callback(Callback &&) = delete; Callback(Callback &&) = delete;
Callback &operator=(Callback &&) = delete; Callback &operator=(Callback &&) = delete;
~Callback() override { ~Callback() override {
impl_->responses_.push({client_id_, 0, nullptr});
impl_->updates_.push({client_id_, 0, nullptr}); impl_->updates_.push({client_id_, 0, nullptr});
} }
@ -283,16 +288,20 @@ class TdReceiver {
} }
void on_result(uint64 id, td_api::object_ptr<td_api::Object> result) override { void on_result(uint64 id, td_api::object_ptr<td_api::Object> result) override {
if (id == 0) { if (id == 0) {
output_responses_queue_->writer_put({client_id_, id, nullptr});
output_updates_queue_->writer_put({client_id_, 0, std::move(result)}); output_updates_queue_->writer_put({client_id_, 0, std::move(result)});
} else { } else {
output_responses_queue_->writer_put({client_id_, id, std::move(result)}); output_responses_queue_->writer_put({client_id_, id, std::move(result)});
output_updates_queue_->writer_put({client_id_, id, nullptr});
} }
} }
void on_error(uint64 id, td_api::object_ptr<td_api::error> error) override { void on_error(uint64 id, td_api::object_ptr<td_api::error> error) override {
if (id == 0) { if (id == 0) {
output_responses_queue_->writer_put({client_id_, id, nullptr});
output_updates_queue_->writer_put({client_id_, 0, std::move(error)}); output_updates_queue_->writer_put({client_id_, 0, std::move(error)});
} else { } else {
output_responses_queue_->writer_put({client_id_, id, std::move(error)}); output_responses_queue_->writer_put({client_id_, id, std::move(error)});
output_updates_queue_->writer_put({client_id_, id, nullptr});
} }
} }
Callback(const Callback &) = delete; Callback(const Callback &) = delete;
@ -301,6 +310,7 @@ class TdReceiver {
Callback &operator=(Callback &&) = delete; Callback &operator=(Callback &&) = delete;
~Callback() override { ~Callback() override {
output_updates_queue_->writer_put({client_id_, 0, nullptr}); output_updates_queue_->writer_put({client_id_, 0, nullptr});
output_responses_queue_->writer_put({client_id_, 0, nullptr});
} }
private: private:
@ -345,17 +355,10 @@ class TdReceiver {
} else if (!include_responses && include_updates) { } else if (!include_responses && include_updates) {
output_updates_queue_->reader_get_event_fd().wait(static_cast<int>(timeout * 1000)); output_updates_queue_->reader_get_event_fd().wait(static_cast<int>(timeout * 1000));
} else if (include_responses && include_updates) { } else if (include_responses && include_updates) {
auto cnt = 0; output_updates_queue_->reader_get_event_fd().wait(static_cast<int>(timeout * 1000));
//todo: find a better implementation by joining two fd together, instead of quickly cycling them.
while (cnt <= timeout * 1000) {
output_updates_queue_->reader_get_event_fd().wait(static_cast<int>(10));
if (output_updates_queue_->reader_wait_nonblock() > 0) break;
output_responses_queue_->reader_get_event_fd().wait(static_cast<int>(10));
if (output_responses_queue_->reader_wait_nonblock() > 0) break;
cnt += 10;
}
} else { } else {
// do nothing, this configuration shouldn't be used. // This configuration (include_responses = false and include_updates = false) shouldn't be used.
output_updates_queue_->reader_get_event_fd().wait(static_cast<int>(timeout * 1000));
} }
return receive_unlocked(0, include_responses, include_updates); return receive_unlocked(0, include_responses, include_updates);
} }