Bitcoin Core 29.99.0
P2P Digital Currency
proxy.h
Go to the documentation of this file.
1// Copyright (c) 2019 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 <array>
11#include <functional>
12#include <list>
13#include <stddef.h>
14#include <tuple>
15#include <type_traits>
16#include <utility>
17
18namespace mp {
19class Connection;
20class EventLoop;
23template <typename Interface>
27template <typename Interface>
30template <typename Params>
33template <typename Struct>
36template <typename Type>
37struct ProxyType;
38
39using CleanupList = std::list<std::function<void()>>;
40using CleanupIt = typename CleanupList::iterator;
41
42inline void CleanupRun(CleanupList& fns) {
43 while (!fns.empty()) {
44 auto fn = std::move(fns.front());
45 fns.pop_front();
46 fn();
47 }
48}
49
52{
55
57};
58
61template <typename Interface_, typename Impl_>
62class ProxyClientBase : public Impl_
63{
64public:
65 using Interface = Interface_;
66 using Impl = Impl_;
69
70 ProxyClientBase(typename Interface::Client client, Connection* connection, bool destroy_connection);
71 ~ProxyClientBase() noexcept;
72
73 // construct/destroy methods called during client construction/destruction
74 // that can optionally be defined in capnp interfaces to invoke code on the
75 // server when proxy client objects are created and destroyed.
76 //
77 // The construct() method is not generally very useful, but can be used to
78 // run custom code on the server automatically when a ProxyClient client is
79 // constructed. The only current use is adding a construct method to Init
80 // interfaces that is called automatically on construction, so client and
81 // server exchange ThreadMap references and set Connection::m_thread_map
82 // values as soon as the Init client is created.
83 //
84 // construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap: Proxy.ThreadMap);
85 //
86 // But construct() is not necessary for this, thread maps could be passed
87 // through a normal method that is just called explicitly rather than
88 // implicitly.
89 //
90 // The destroy() method is more generally useful than construct(), because
91 // it ensures that the server object will be destroyed synchronously before
92 // the client destructor returns, instead of asynchronously at some
93 // unpredictable time after the client object is already destroyed and
94 // client code has moved on. If the destroy method accepts a Context
95 // parameter like:
96 //
97 // destroy @0 (context: Proxy.Context) -> ();
98 //
99 // then it will also ensure that the destructor runs on the same thread the
100 // client used to make other RPC calls, instead of running on the server
101 // EventLoop thread and possibly blocking it.
102 static void construct(Super&) {}
103 static void destroy(Super&) {}
104
105 typename Interface::Client m_client;
107};
108
111template <typename Interface, typename Impl>
112class ProxyClientCustom : public ProxyClientBase<Interface, Impl>
113{
115};
116
119template <typename Interface_, typename Impl_>
120struct ProxyServerBase : public virtual Interface_::Server
121{
122public:
123 using Interface = Interface_;
124 using Impl = Impl_;
125
126 ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection);
127 virtual ~ProxyServerBase();
128 void invokeDestroy();
129
143 std::shared_ptr<Impl> m_impl;
145};
146
167template <typename Interface, typename Impl>
168struct ProxyServerCustom : public ProxyServerBase<Interface, Impl>
169{
171};
172
188template <class Fn>
190
194template <class _Class, class _Result, class... _Params>
195struct FunctionTraits<_Result (_Class::*const)(_Params...)>
196{
197 using Params = TypeList<_Params...>;
198 using Result = _Result;
199 template <size_t N>
200 using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
201 using Fields =
202 std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
203
211 template <size_t N>
212 static decltype(auto) Fwd(Param<N>& arg) { return static_cast<Param<N>&&>(arg); }
213};
214
229template <typename MethodParams, typename Enable = void>
231{
233 using Result = void;
234 using Fields = Params;
235
236 template <typename ServerContext>
237 static void invoke(ServerContext&)
238 {
239 }
240};
241
254template <typename MethodParams>
255struct ProxyMethodTraits<MethodParams, Require<decltype(ProxyMethod<MethodParams>::impl)>>
256 : public FunctionTraits<decltype(ProxyMethod<MethodParams>::impl)>
257{
258 template <typename ServerContext, typename... Args>
259 static decltype(auto) invoke(ServerContext& server_context, Args&&... args)
260 {
261 return (server_context.proxy_server.m_impl.get()->*ProxyMethod<MethodParams>::impl)(std::forward<Args>(args)...);
262 }
263};
264
267template <typename MethodParams>
268struct ProxyClientMethodTraits : public ProxyMethodTraits<MethodParams>
269{
270};
271
274template <typename MethodParams>
275struct ProxyServerMethodTraits : public ProxyMethodTraits<MethodParams>
276{
277};
278
279static constexpr int FIELD_IN = 1;
280static constexpr int FIELD_OUT = 2;
281static constexpr int FIELD_OPTIONAL = 4;
282static constexpr int FIELD_REQUESTED = 8;
283static constexpr int FIELD_BOXED = 16;
284
286template <typename Field, int flags>
287struct Accessor : public Field
288{
289 static const bool in = flags & FIELD_IN;
290 static const bool out = flags & FIELD_OUT;
291 static const bool optional = flags & FIELD_OPTIONAL;
292 static const bool requested = flags & FIELD_REQUESTED;
293 static const bool boxed = flags & FIELD_BOXED;
294};
295
297template <typename Fn>
299
301template <typename Result, typename... Args>
302class ProxyCallback<std::function<Result(Args...)>>
303{
304public:
305 virtual Result call(Args&&... args) = 0;
306};
307
308} // namespace mp
309
310#endif // MP_PROXY_H
int flags
Definition: bitcoin-tx.cpp:529
ArgsManager & args
Definition: bitcoind.cpp:277
const CChainParams & Params()
Return the currently selected parameters.
Object holding network & rpc state associated with either an incoming server connection,...
Definition: proxy-io.h:288
Wrapper around std::function for passing std::function objects between client and servers.
Definition: proxy.h:298
Base class for generated ProxyClient classes that implement a C++ interface and forward calls to a ca...
Definition: proxy.h:63
Interface::Client m_client
Definition: proxy.h:105
ProxyContext m_context
Definition: proxy.h:106
Interface_ Interface
Definition: proxy.h:65
~ProxyClientBase() noexcept
Definition: proxy-io.h:447
ProxyClientBase(typename Interface::Client client, Connection *connection, bool destroy_connection)
Definition: proxy-io.h:378
static void construct(Super &)
Definition: proxy.h:102
static void destroy(Super &)
Definition: proxy.h:103
Customizable (through template specialization) base class used in generated ProxyClient implementatio...
Definition: proxy.h:113
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
std::list< std::function< void()> > CleanupList
Definition: proxy.h:39
typename _Require< SfinaeExpr, Result >::Result Require
SFINAE helper, basically the same as to C++17's void_t, but allowing types other than void to be retu...
Definition: util.h:97
static constexpr int FIELD_OUT
Definition: proxy.h:280
ServerInvokeContext< ProxyServer< Interface >, ::capnp::CallContext< Params, Results > > ServerContext
Definition: proxy-io.h:56
static constexpr int FIELD_BOXED
Definition: proxy.h:283
static constexpr int FIELD_REQUESTED
Definition: proxy.h:282
static constexpr int FIELD_IN
Definition: proxy.h:279
typename CleanupList::iterator CleanupIt
Definition: proxy.h:40
void CleanupRun(CleanupList &fns)
Definition: proxy.h:42
static constexpr int FIELD_OPTIONAL
Definition: proxy.h:281
Accessor type holding flags that determine how to access a message field.
Definition: proxy.h:288
static const bool boxed
Definition: proxy.h:293
static const bool optional
Definition: proxy.h:291
static const bool out
Definition: proxy.h:290
static const bool requested
Definition: proxy.h:292
static const bool in
Definition: proxy.h:289
std::conditional_t< std::is_same_v< void, Result >, Params, TypeList< _Params..., _Result > > Fields
Definition: proxy.h:202
static decltype(auto) Fwd(Param< N > &arg)
Enable perfect forwarding for clientInvoke calls.
Definition: proxy.h:212
typename std::tuple_element< N, std::tuple< _Params... > >::type Param
Definition: proxy.h:200
Function traits class used to get method parameter and result types, used in generated ProxyClient an...
Definition: proxy.h:189
Mapping from capnp interface type to proxy client implementation (specializations are generated by pr...
Definition: proxy.h:24
Customizable (through template specialization) traits class used in generated ProxyClient implementat...
Definition: proxy.h:269
Context data associated with proxy client and server classes.
Definition: proxy.h:52
Connection * connection
Definition: proxy.h:53
CleanupList cleanup_fns
Definition: proxy.h:54
ProxyContext(Connection *connection)
Definition: proxy.h:56
Mapping from capnp method params type to method traits (specializations are generated by proxy-codege...
Definition: proxy.h:31
static decltype(auto) invoke(ServerContext &server_context, Args &&... args)
Definition: proxy.h:259
Traits class for a proxy method, providing the same Params/Result/Param/Fields described in the Funct...
Definition: proxy.h:231
TypeList<> Params
Definition: proxy.h:232
static void invoke(ServerContext &)
Definition: proxy.h:237
Base class for generated ProxyServer classes that implement capnp server methods and forward calls to...
Definition: proxy.h:121
Interface_ Interface
Definition: proxy.h:123
ProxyServerBase(std::shared_ptr< Impl > impl, Connection &connection)
Definition: proxy-io.h:453
ProxyContext m_context
Definition: proxy.h:144
void invokeDestroy()
If the capnp interface defined a special "destroy" method, as described the ProxyClientBase class,...
Definition: proxy-io.h:514
virtual ~ProxyServerBase()
ProxyServer destructor, called from the EventLoop thread by Cap'n Proto garbage collection code after...
Definition: proxy-io.h:464
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:143
Customizable (through template specialization) base class which ProxyServer classes produced by gener...
Definition: proxy.h:169
Mapping from capnp interface type to proxy server implementation (specializations are generated by pr...
Definition: proxy.h:28
Customizable (through template specialization) traits class used in generated ProxyServer implementat...
Definition: proxy.h:276
Mapping from capnp struct type to struct traits (specializations are generated by proxy-codegen....
Definition: proxy.h:34
Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen....
Definition: proxy.h:37
ProxyServer & proxy_server
Definition: proxy-io.h:45
Generic utility functions used by capnp code.
Definition: util.h:33