Bitcoin Core  31.0.0
P2P Digital Currency
proxy.h
Go to the documentation of this file.
1 // Copyright (c) The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef MP_PROXY_H
6 #define MP_PROXY_H
7 
8 #include <mp/util.h>
9 
10 #include <cassert>
11 #include <functional>
12 #include <list>
13 #include <memory>
14 #include <stddef.h>
15 #include <tuple>
16 #include <type_traits>
17 #include <utility>
18 #include <variant> // IWYU pragma: keep
19 
20 namespace mp {
21 class Connection;
22 class EventLoop;
25 template <typename Interface> struct ProxyClient; // IWYU pragma: export
28 template <typename Interface> struct ProxyServer; // IWYU pragma: export
30 template <typename Params> struct ProxyMethod; // IWYU pragma: export
32 template <typename Struct> struct ProxyStruct; // IWYU pragma: export
34 template <typename Type> struct ProxyType; // IWYU pragma: export
35 
36 using CleanupList = std::list<std::function<void()>>;
37 using CleanupIt = typename CleanupList::iterator;
38 
39 inline void CleanupRun(CleanupList& fns) {
40  while (!fns.empty()) {
41  auto fn = std::move(fns.front());
42  fns.pop_front();
43  fn();
44  }
45 }
46 
51 {
52 public:
53  explicit EventLoopRef(EventLoop& loop, Lock* lock = nullptr);
54  EventLoopRef(EventLoopRef&& other) noexcept : m_loop(other.m_loop) { other.m_loop = nullptr; }
55  EventLoopRef(const EventLoopRef&) = delete;
56  EventLoopRef& operator=(const EventLoopRef&) = delete;
57  EventLoopRef& operator=(EventLoopRef&&) = delete;
59  EventLoop& operator*() const { assert(m_loop); return *m_loop; }
60  EventLoop* operator->() const { assert(m_loop); return m_loop; }
61  void reset(bool relock=false);
62 
63  EventLoop* m_loop{nullptr};
64  Lock* m_lock{nullptr};
65 };
66 
69 {
73 
75 };
76 
79 template <typename Interface_, typename Impl_>
80 class ProxyClientBase : public Impl_
81 {
82 public:
83  using Interface = Interface_;
84  using Impl = Impl_;
87 
97  ProxyClientBase(typename Interface::Client client, Connection* connection, bool destroy_connection);
98  ~ProxyClientBase() noexcept;
99 
100  // construct/destroy methods called during client construction/destruction
101  // that can optionally be defined in capnp interfaces to invoke code on the
102  // server when proxy client objects are created and destroyed.
103  //
104  // The construct() method is not generally very useful, but can be used to
105  // run custom code on the server automatically when a ProxyClient client is
106  // constructed. The only current use is adding a construct method to Init
107  // interfaces that is called automatically on construction, so client and
108  // server exchange ThreadMap references and set Connection::m_thread_map
109  // values as soon as the Init client is created.
110  //
111  // construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap: Proxy.ThreadMap);
112  //
113  // But construct() is not necessary for this, thread maps could be passed
114  // through a normal method that is just called explicitly rather than
115  // implicitly.
116  //
117  // The destroy() method is more generally useful than construct(), because
118  // it ensures that the server object will be destroyed synchronously before
119  // the client destructor returns, instead of asynchronously at some
120  // unpredictable time after the client object is already destroyed and
121  // client code has moved on. If the destroy method accepts a Context
122  // parameter like:
123  //
124  // destroy @0 (context: Proxy.Context) -> ();
125  //
126  // then it will also ensure that the destructor runs on the same thread the
127  // client used to make other RPC calls, instead of running on the server
128  // EventLoop thread and possibly blocking it.
129  static void construct(Super&) {}
130  static void destroy(Super&) {}
131 
132  typename Interface::Client m_client;
134 };
135 
138 template <typename Interface, typename Impl>
139 class ProxyClientCustom : public ProxyClientBase<Interface, Impl>
140 {
142 };
143 
146 template <typename Interface_, typename Impl_>
147 struct ProxyServerBase : public virtual Interface_::Server
148 {
149 public:
150  using Interface = Interface_;
151  using Impl = Impl_;
152 
153  ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection);
154  virtual ~ProxyServerBase();
155  void invokeDestroy();
156  using Interface_::Server::thisCap;
157 
171  std::shared_ptr<Impl> m_impl;
173 };
174 
195 template <typename Interface, typename Impl>
196 struct ProxyServerCustom : public ProxyServerBase<Interface, Impl>
197 {
199 };
200 
216 template <class Fn>
218 
222 template <class _Class, class _Result, class... _Params>
223 struct FunctionTraits<_Result (_Class::*const)(_Params...)>
224 {
225  using Params = TypeList<_Params...>;
226  using Result = _Result;
227  template <size_t N>
228  using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
229  using Fields =
230  std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
231 
239  template <size_t N>
240  static decltype(auto) Fwd(Param<N>& arg) { return static_cast<Param<N>&&>(arg); }
241 };
242 
257 template <typename MethodParams, typename Enable = void>
259 {
261  using Result = void;
262  using Fields = Params;
263 
264  template <typename ServerContext>
265  static void invoke(ServerContext&)
266  {
267  }
268 };
269 
282 template <typename MethodParams>
283 struct ProxyMethodTraits<MethodParams, Require<decltype(ProxyMethod<MethodParams>::impl)>>
284  : public FunctionTraits<decltype(ProxyMethod<MethodParams>::impl)>
285 {
286  template <typename ServerContext, typename... Args>
287  static decltype(auto) invoke(ServerContext& server_context, Args&&... args)
288  {
289  return (server_context.proxy_server.m_impl.get()->*ProxyMethod<MethodParams>::impl)(std::forward<Args>(args)...);
290  }
291 };
292 
295 template <typename MethodParams>
296 struct ProxyClientMethodTraits : public ProxyMethodTraits<MethodParams>
297 {
298 };
299 
302 template <typename MethodParams>
303 struct ProxyServerMethodTraits : public ProxyMethodTraits<MethodParams>
304 {
305 };
306 
307 static constexpr int FIELD_IN = 1;
308 static constexpr int FIELD_OUT = 2;
309 static constexpr int FIELD_OPTIONAL = 4;
310 static constexpr int FIELD_REQUESTED = 8;
311 static constexpr int FIELD_BOXED = 16;
312 
314 template <typename Field, int flags>
315 struct Accessor : public Field
316 {
317  static const bool in = flags & FIELD_IN;
318  static const bool out = flags & FIELD_OUT;
319  static const bool optional = flags & FIELD_OPTIONAL;
320  static const bool requested = flags & FIELD_REQUESTED;
321  static const bool boxed = flags & FIELD_BOXED;
322 };
323 
325 template <typename Fn>
327 
329 template <typename Result, typename... Args>
330 class ProxyCallback<std::function<Result(Args...)>>
331 {
332 public:
333  virtual Result call(Args&&... args) = 0;
334 };
335 
336 } // namespace mp
337 
338 #endif // MP_PROXY_H
ProxyContext m_context
Definition: proxy.h:172
ProxyServerBase(std::shared_ptr< Impl > impl, Connection &connection)
Definition: proxy-io.h:574
CleanupList cleanup_fns
Definition: proxy.h:72
assert(!tx.IsCoinBase())
EventLoop * operator->() const
Definition: proxy.h:60
Wrapper around std::function for passing std::function objects between client and servers...
Definition: proxy.h:326
Base class for generated ProxyClient classes that implement a C++ interface and forward calls to a ca...
Definition: proxy.h:80
Mapping from capnp method params type to method traits (specializations are generated by proxy-codege...
Definition: proxy.h:30
Object holding network & rpc state associated with either an incoming server connection, or an outgoing client connection.
Definition: proxy-io.h:422
std::conditional_t< std::is_same_v< void, Result >, Params, TypeList< _Params..., _Result > > Fields
Definition: proxy.h:230
Accessor type holding flags that determine how to access a message field.
Definition: proxy.h:315
Definition: common.h:29
std::shared_ptr< Impl > m_impl
Implementation pointer that may or may not be owned and deleted when this capnp server goes out of sc...
Definition: proxy.h:171
static constexpr int FIELD_REQUESTED
Definition: proxy.h:310
static void construct(Super &)
Definition: proxy.h:129
Generic utility functions used by capnp code.
Definition: util.h:32
Customizable (through template specialization) traits class used in generated ProxyServer implementat...
Definition: proxy.h:303
ProxyContext m_context
Definition: proxy.h:133
Lock * m_lock
Definition: proxy.h:64
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
Event loop implementation.
Definition: proxy-io.h:238
Context data associated with proxy client and server classes.
Definition: proxy.h:68
Customizable (through template specialization) base class which ProxyServer classes produced by gener...
Definition: proxy.h:196
Mapping from capnp interface type to proxy server implementation (specializations are generated by pr...
Definition: proxy.h:28
ArgsManager & args
Definition: bitcoind.cpp:277
static const bool out
Definition: proxy.h:318
EventLoopRef loop
Definition: proxy.h:71
static constexpr int FIELD_BOXED
Definition: proxy.h:311
typename _Require< SfinaeExpr, Result >::Result Require
SFINAE helper, basically the same as to C++17&#39;s void_t, but allowing types other than void to be retu...
Definition: util.h:97
ProxyContext(Connection *connection)
Definition: proxy.cpp:81
static constexpr int FIELD_IN
Definition: proxy.h:307
Customizable (through template specialization) traits class used in generated ProxyClient implementat...
Definition: proxy.h:296
EventLoopRef(EventLoop &loop, Lock *lock=nullptr)
Definition: proxy.cpp:49
TypeList<> Params
Definition: proxy.h:260
ServerInvokeContext< ProxyServer< Interface >, ::capnp::CallContext< Params, Results > > ServerContext
Definition: proxy-io.h:72
ProxyClientBase(typename Interface::Client client, Connection *connection, bool destroy_connection)
Construct libmultiprocess client object wrapping Cap&#39;n Proto client object with a reference to the as...
Definition: proxy-io.h:508
Mapping from capnp struct type to struct traits (specializations are generated by proxy-codegen...
Definition: proxy.h:32
EventLoopRef(EventLoopRef &&other) noexcept
Definition: proxy.h:54
std::list< std::function< void()> > CleanupList
Definition: proxy.h:36
Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen...
Definition: proxy.h:34
static void invoke(ServerContext &)
Definition: proxy.h:265
static const bool requested
Definition: proxy.h:320
EventLoopRef & operator=(const EventLoopRef &)=delete
int flags
Definition: bitcoin-tx.cpp:529
Customizable (through template specialization) base class used in generated ProxyClient implementatio...
Definition: proxy.h:139
static const bool optional
Definition: proxy.h:319
const CChainParams & Params()
Return the currently selected parameters.
typename std::tuple_element< N, std::tuple< _Params... > >::type Param
Definition: proxy.h:228
static constexpr int FIELD_OUT
Definition: proxy.h:308
EventLoop * m_loop
Definition: proxy.h:63
static void destroy(Super &)
Definition: proxy.h:130
virtual ~ProxyServerBase()
ProxyServer destructor, called from the EventLoop thread by Cap&#39;n Proto garbage collection code after...
Definition: proxy-io.h:593
Event loop smart pointer automatically managing m_num_clients.
Definition: proxy.h:50
Function traits class used to get method parameter and result types, used in generated ProxyClient an...
Definition: proxy.h:217
static constexpr int FIELD_OPTIONAL
Definition: proxy.h:309
static const bool boxed
Definition: proxy.h:321
Definition: util.h:171
static const bool in
Definition: proxy.h:317
Traits class for a proxy method, providing the same Params/Result/Param/Fields described in the Funct...
Definition: proxy.h:258
void invokeDestroy()
If the capnp interface defined a special "destroy" method, as described the ProxyClientBase class...
Definition: proxy-io.h:641
Interface::Client m_client
Definition: proxy.h:132
Base class for generated ProxyServer classes that implement capnp server methods and forward calls to...
Definition: proxy.h:147
typename CleanupList::iterator CleanupIt
Definition: proxy.h:37
void CleanupRun(CleanupList &fns)
Definition: proxy.h:39
EventLoop & operator*() const
Definition: proxy.h:59
Connection * connection
Definition: proxy.h:70
Mapping from capnp interface type to proxy client implementation (specializations are generated by pr...
Definition: proxy.h:25
void reset(bool relock=false)
Definition: proxy.cpp:59
~ProxyClientBase() noexcept
Definition: proxy-io.h:568