client.h
Go to the documentation of this file.
1 //-------------------------------------------------------------------
2 // Nano RPC
3 // https://github.com/tdv/nanorpc
4 // Created: 05.2018
5 // Copyright (C) 2018 tdv
6 //-------------------------------------------------------------------
7 
8 #ifndef __NANO_RPC_CORE_CLIENT_H__
9 #define __NANO_RPC_CORE_CLIENT_H__
10 
11 // STD
12 #include <functional>
13 #include <stdexcept>
14 #include <string>
15 #include <tuple>
16 #include <utility>
17 
18 // C++17 backwards compatibility
19 #include <linb/any.hpp>
20 #include <tl/optional.hpp>
21 
22 // NANORPC
24 #include "nanorpc/core/exception.h"
25 #include "nanorpc/core/type.h"
26 #include "nanorpc/core/hash.h"
27 #include "nanorpc/version/core.h"
28 
29 
30 namespace nanorpc
31 {
32 namespace core
33 {
34 
35 template <typename TPacker>
36 class client final
37 {
38 private:
39  class result;
40 
41 public:
43  : executor_{std::move(executor)}
44  {
45  }
46 
47  template <typename ... TArgs>
48  result call(std::string name, TArgs && ... args)
49  {
50  return call(hash_id(name), std::forward<TArgs>(args) ... );
51  }
52 
53  template <typename ... TArgs>
54  result call(type::id id, TArgs && ... args)
55  {
56  auto data = std::make_tuple(std::forward<TArgs>(args) ... );
57 
58  packer_type packer;
59  auto request = packer
60  .pack(version::core::protocol::value)
62  .pack(id)
63  .pack(data)
64  .to_buffer();
65 
66  auto buffer = executor_(std::move(request));
67  auto response = packer.from_buffer(std::move(buffer));
68 
69  {
70  version::core::protocol::value_type protocol{};
71  response = response.unpack(protocol);
72  if (protocol != version::core::protocol::value)
73  {
74  throw exception::client{"[nanorpc::core::client::call] Unsupported protocol version \"" +
75  std::to_string(protocol) + "\"."};
76  }
77  }
78 
79  {
81  response = response.unpack(type);
83  throw exception::client{"[nanorpc::core::client::call] Bad response type."};
84  }
85 
86  {
88  response = response.unpack(status);
90  {
91  std::string message;
92  response = response.unpack(message);
93  throw exception::logic{message};
94  }
95  }
96 
97  return {std::move(response)};
98  }
99 
100 private:
101  using packer_type = TPacker;
102  using deserializer_type = typename packer_type::deserializer_type;
103 
105 
106  class result final
107  {
108  public:
109  result(result &&) noexcept = default;
110  result& operator = (result &&) noexcept = default;
111  ~result() noexcept = default;
112 
113  template <typename T>
114  T as() const
115  {
116  if (!value_ && !deserializer_)
117  throw exception::client{"[nanorpc::core::client::result::as] No data."};
118 
119  using Type = typename std::decay<T>::type;
120 
121  if (!value_)
122  {
123  if (!deserializer_)
124  throw exception::client{"[nanorpc::core::client::result::as] No data."};
125 
126  Type data{};
127  deserializer_->unpack(data);
128 
129  value_ = std::move(data);
131  }
132 
133  return linb::any_cast<Type>(*value_);
134  }
135 
136  template <typename T>
137  operator T () const
138  {
139  return as<T>();
140  }
141 
142  private:
143  template <typename>
144  friend class client;
145 
148 
149  result(deserializer_type deserializer)
150  : deserializer_{std::move(deserializer)}
151  {
152  }
153 
154  result(result const &) = delete;
155  result& operator = (result const &) = delete;
156  };
157 };
158 
159 } // namespace core
160 } // namespace nanorpc
161 
162 #endif // !__NANO_RPC_CORE_CLIENT_H__
nanorpc
Definition: client.h:30
nanorpc::core::detail::pack::meta::status::good
@ good
nanorpc::core::client::result::result
result(result &&) noexcept=default
nanorpc::core::hash_id
type::id hash_id(const std::string &str)
Definition: hash.h:14
nanorpc::core::client::deserializer_type
typename packer_type::deserializer_type deserializer_type
Definition: client.h:102
nanorpc::core::client::client
client(type::executor executor)
Definition: client.h:42
nanorpc::core::client::call
result call(std::string name, TArgs &&... args)
Definition: client.h:48
optional.hpp
nanorpc::core::client::result::~result
~result() noexcept=default
nanorpc::core::client::result::as
T as() const
Definition: client.h:114
nanorpc::version::core::protocol
std::integral_constant< std::uint32_t, 1 > protocol
Definition: core.h:22
dai::bootloader::Type
Type
Definition: Type.hpp:11
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
type.h
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
nanorpc::core::detail::pack::meta::type::response
@ response
nanorpc::core::client::result::deserializer_
tl::optional< deserializer_type > deserializer_
Definition: client.h:146
nanorpc::core::type::executor
std::function< buffer(buffer)> executor
Definition: type.h:29
nanorpc::core::client::packer_type
TPacker packer_type
Definition: client.h:101
nanorpc::core::type::id
std::uint64_t id
Definition: type.h:27
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
nanorpc::core::detail::pack::meta::type
type
Definition: pack_meta.h:26
nanorpc::core::detail::pack::meta::type::request
@ request
nanorpc::core::client::result::operator=
result & operator=(result &&) noexcept=default
nanorpc::core::detail::pack::meta::status
status
Definition: pack_meta.h:33
tl::optional< deserializer_type >
nanorpc::core::client
Definition: client.h:36
tl::optional::reset
void reset() noexcept
Destroys the stored value if one exists, making the optional empty.
Definition: 3rdparty/tl/optional.hpp:1329
core.h
nanorpc::core::client::result::value_
tl::optional< linb::any > value_
Definition: client.h:147
hash.h
nanorpc::core::client::executor_
type::executor executor_
Definition: client.h:104
exception.h
any.hpp
nanorpc::core::client::result
Definition: client.h:106
nanorpc::core::client::result::result
result(deserializer_type deserializer)
Definition: client.h:149
nanorpc::core::client::call
result call(type::id id, TArgs &&... args)
Definition: client.h:54
pack_meta.h


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:18