diff options
author | Andrej Rode <andrej.rode@ettus.com> | 2017-04-03 18:49:58 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2017-12-22 15:03:45 -0800 |
commit | 53795c74aead4e6021bc788b788f8ed5b4a0166d (patch) | |
tree | 45e4075f3d8ffdee7dff7c72dd665f5c5b0c746c /host/lib/deps/rpclib/include/rpc/client.inl | |
parent | 6a12add1560545438e1bebc05efbafd05aace4f9 (diff) | |
download | uhd-53795c74aead4e6021bc788b788f8ed5b4a0166d.tar.gz uhd-53795c74aead4e6021bc788b788f8ed5b4a0166d.tar.bz2 uhd-53795c74aead4e6021bc788b788f8ed5b4a0166d.zip |
uhd: add cut-down rpclib source tree and import tool
Diffstat (limited to 'host/lib/deps/rpclib/include/rpc/client.inl')
-rw-r--r-- | host/lib/deps/rpclib/include/rpc/client.inl | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/host/lib/deps/rpclib/include/rpc/client.inl b/host/lib/deps/rpclib/include/rpc/client.inl new file mode 100644 index 000000000..4ff33294d --- /dev/null +++ b/host/lib/deps/rpclib/include/rpc/client.inl @@ -0,0 +1,65 @@ +namespace rpc { + +template <typename... Args> +RPCLIB_MSGPACK::object_handle client::call(std::string const &func_name, + Args... args) { + RPCLIB_CREATE_LOG_CHANNEL(client) + auto future = async_call(func_name, std::forward<Args>(args)...); + auto wait_result = future.wait_for(std::chrono::milliseconds(get_timeout())); + + if (wait_result == std::future_status::timeout) { + throw_timeout(func_name); + } + + return future.get(); +} + +template <typename... Args> +std::future<RPCLIB_MSGPACK::object_handle> +client::async_call(std::string const &func_name, Args... args) { + RPCLIB_CREATE_LOG_CHANNEL(client) + wait_conn(); + using RPCLIB_MSGPACK::object; + LOG_DEBUG("Calling {}", func_name); + + auto args_obj = std::make_tuple(args...); + const int idx = get_next_call_idx(); + auto call_obj = + std::make_tuple(static_cast<uint8_t>(client::request_type::call), idx, + func_name, args_obj); + + auto buffer = std::make_shared<RPCLIB_MSGPACK::sbuffer>(); + RPCLIB_MSGPACK::pack(*buffer, call_obj); + + // TODO: Change to move semantics when asio starts supporting move-only + // handlers in post(). [sztomi, 2016-02-14] + auto p = std::make_shared<std::promise<RPCLIB_MSGPACK::object_handle>>(); + auto ft = p->get_future(); + + post(buffer, idx, func_name, p); + + return ft; +} + +//! \brief Sends a notification with the given name and arguments (if any). +//! \param func_name The name of the notification to call. +//! \param args The arguments to pass to the function. +//! \note This function returns when the notification is written to the +//! socket. +//! \tparam Args THe types of the arguments. +template <typename... Args> +void client::send(std::string const &func_name, Args... args) { + RPCLIB_CREATE_LOG_CHANNEL(client) + LOG_DEBUG("Sending notification {}", func_name); + + auto args_obj = std::make_tuple(args...); + auto call_obj = std::make_tuple( + static_cast<uint8_t>(client::request_type::notification), func_name, + args_obj); + + auto buffer = new RPCLIB_MSGPACK::sbuffer; + RPCLIB_MSGPACK::pack(*buffer, call_obj); + + post(buffer); +} +} |