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
187template <class Fn>
189
193template <class _Class, class _Result, class... _Params>
194struct FunctionTraits<_Result (_Class::*const)(_Params...)>
195{
196 using Params = TypeList<_Params...>;
197 using Result = _Result;
198 template <size_t N>
199 using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
200 using Fields =
201 std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
202};
203
218template <typename MethodParams, typename Enable = void>
220{
222 using Result = void;
223 using Fields = Params;
224
225 template <typename ServerContext>
226 static void invoke(ServerContext&)
227 {
228 }
229};
230
243template <typename MethodParams>
244struct ProxyMethodTraits<MethodParams, Require<decltype(ProxyMethod<MethodParams>::impl)>>
245 : public FunctionTraits<decltype(ProxyMethod<MethodParams>::impl)>
246{
247 template <typename ServerContext, typename... Args>
248 static decltype(auto) invoke(ServerContext& server_context, Args&&... args)
249 {
250 return (server_context.proxy_server.m_impl.get()->*ProxyMethod<MethodParams>::impl)(std::forward<Args>(args)...);
251 }
252};
253
256template <typename MethodParams>
257struct ProxyClientMethodTraits : public ProxyMethodTraits<MethodParams>
258{
259};
260
263template <typename MethodParams>
264struct ProxyServerMethodTraits : public ProxyMethodTraits<MethodParams>
265{
266};
267
268static constexpr int FIELD_IN = 1;
269static constexpr int FIELD_OUT = 2;
270static constexpr int FIELD_OPTIONAL = 4;
271static constexpr int FIELD_REQUESTED = 8;
272static constexpr int FIELD_BOXED = 16;
273
275template <typename Field, int flags>
276struct Accessor : public Field
277{
278 static const bool in = flags & FIELD_IN;
279 static const bool out = flags & FIELD_OUT;
280 static const bool optional = flags & FIELD_OPTIONAL;
281 static const bool requested = flags & FIELD_REQUESTED;
282 static const bool boxed = flags & FIELD_BOXED;
283};
284
286template <typename Fn>
288
290template <typename Result, typename... Args>
291class ProxyCallback<std::function<Result(Args...)>>
292{
293public:
294 virtual Result call(Args&&... args) = 0;
295};
296
297} // namespace mp
298
299#endif // MP_PROXY_H
int flags
Definition: bitcoin-tx.cpp:536
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:287
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:269
ServerInvokeContext< ProxyServer< Interface >, ::capnp::CallContext< Params, Results > > ServerContext
Definition: proxy-io.h:56
static constexpr int FIELD_BOXED
Definition: proxy.h:272
static constexpr int FIELD_REQUESTED
Definition: proxy.h:271
static constexpr int FIELD_IN
Definition: proxy.h:268
typename CleanupList::iterator CleanupIt
Definition: proxy.h:40
void CleanupRun(CleanupList &fns)
Definition: proxy.h:42
static constexpr int FIELD_OPTIONAL
Definition: proxy.h:270
Accessor type holding flags that determine how to access a message field.
Definition: proxy.h:277
static const bool boxed
Definition: proxy.h:282
static const bool optional
Definition: proxy.h:280
static const bool out
Definition: proxy.h:279
static const bool requested
Definition: proxy.h:281
static const bool in
Definition: proxy.h:278
std::conditional_t< std::is_same_v< void, Result >, Params, TypeList< _Params..., _Result > > Fields
Definition: proxy.h:201
typename std::tuple_element< N, std::tuple< _Params... > >::type Param
Definition: proxy.h:199
Function traits class used to get method parameter and result types, used in generated ProxyClient an...
Definition: proxy.h:188
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:258
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:248
Traits class for a proxy method, providing the same Params/Result/Param/Fields described in the Funct...
Definition: proxy.h:220
TypeList<> Params
Definition: proxy.h:221
static void invoke(ServerContext &)
Definition: proxy.h:226
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:265
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