From bb5434cebe05e084bbd636484aae1f912f6b0480 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 3 Jun 2019 19:06:17 +0800 Subject: [PATCH] fix for future --- include/rpc_client.hpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/include/rpc_client.hpp b/include/rpc_client.hpp index 5296a11..81d712c 100644 --- a/include/rpc_client.hpp +++ b/include/rpc_client.hpp @@ -13,7 +13,7 @@ namespace rest_rpc { class req_result { public: req_result() = default; - req_result(string_view data) : data_(data) {} + req_result(string_view data) : data_(data.data(), data.length()) {} bool success() const { return !has_error(data_); } @@ -32,8 +32,9 @@ namespace rest_rpc { throw std::logic_error("rpc error"); } } + private: - string_view data_; + std::string data_; }; enum class CallModel { @@ -235,8 +236,17 @@ namespace rest_rpc { template std::future async_call(const std::string& rpc_name, Args&&... args) { + auto p = std::make_shared>(); + std::future future = p->get_future(); + uint64_t fu_id = 0; - auto future = get_future(fu_id); + { + std::unique_lock lock(cb_mtx_); + fu_id_++; + fu_id = fu_id_; + future_map_.emplace(fu_id, std::move(p)); + } + msgpack_codec codec; auto ret = codec.pack_args(rpc_name, std::forward(args)...); write(fu_id, std::move(ret)); @@ -246,7 +256,6 @@ namespace rest_rpc { template void async_call(const std::string& rpc_name, std::function cb, Args&& ... args) { uint64_t cb_id = 0; - { std::unique_lock lock(cb_mtx_); callback_id_++; @@ -394,18 +403,6 @@ namespace rest_rpc { }); } - std::future get_future(uint64_t& fu_id) { - auto p = std::make_shared>(); - std::future future = p->get_future(); - - std::unique_lock lock(cb_mtx_); - fu_id_++; - fu_id = fu_id_; - future_map_.emplace(fu_id_, std::move(p)); - - return future; - } - void call_back(uint64_t req_id, const boost::system::error_code& ec, string_view data) { auto cb_flag = req_id >> 63; if (cb_flag) {