diff --git a/tonlib/tonlib/ExtClientOutbound.cpp b/tonlib/tonlib/ExtClientOutbound.cpp new file mode 100644 index 0000000..a20a8c5 --- /dev/null +++ b/tonlib/tonlib/ExtClientOutbound.cpp @@ -0,0 +1,66 @@ + +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "ExtClientOutbound.h" +#include +namespace tonlib { + +class ExtClientOutboundImp : public ExtClientOutbound { + public: + ExtClientOutboundImp(td::unique_ptr callback) : callback_(std::move(callback)) { + } + + void check_ready(td::Promise promise) override { + promise.set_error(td::Status::Error("Not supported")); + } + + void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout, + td::Promise promise) override { + auto query_id = next_query_id_++; + queries_[query_id] = std::move(promise); + callback_->request(query_id, data.as_slice().str()); + } + + void on_query_result(td::int64 id, td::Result r_data, td::Promise promise) override { + auto it = queries_.find(id); + if (it == queries_.end()) { + promise.set_error(td::Status::Error(400, "Unknown query id")); + } + it->second.set_result(std::move(r_data)); + queries_.erase(it); + promise.set_value(td::Unit()); + } + + private: + td::unique_ptr callback_; + td::int64 next_query_id_{1}; + std::map> queries_; + + void tear_down() override { + for (auto &it : queries_) { + it.second.set_error(td::Status::Error(400, "Query cancelled")); + } + queries_.clear(); + } +}; + +td::actor::ActorOwn ExtClientOutbound::create(td::unique_ptr callback) { + return td::actor::create_actor("ExtClientOutbound", std::move(callback)); +} +} // namespace tonlib diff --git a/tonlib/tonlib/ExtClientOutbound.h b/tonlib/tonlib/ExtClientOutbound.h new file mode 100644 index 0000000..3635fd1 --- /dev/null +++ b/tonlib/tonlib/ExtClientOutbound.h @@ -0,0 +1,37 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/actor/actor.h" + +#include "adnl/adnl-ext-client.h" + +namespace tonlib { +class ExtClientOutbound : public ton::adnl::AdnlExtClient { + public: + class Callback { + public: + virtual ~Callback() { + } + virtual void request(td::int64 id, std::string data) = 0; + }; + virtual void on_query_result(td::int64 id, td::Result r_data, td::Promise promise) = 0; + static td::actor::ActorOwn create(td::unique_ptr callback); +}; + +} // namespace tonlib