Bitcoin Core 29.99.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
20namespace mp {
21class Connection;
22class EventLoop;
25template <typename Interface> struct ProxyClient; // IWYU pragma: export
28template <typename Interface> struct ProxyServer; // IWYU pragma: export
30template <typename Params> struct ProxyMethod; // IWYU pragma: export
32template <typename Struct> struct ProxyStruct; // IWYU pragma: export
34template <typename Type> struct ProxyType; // IWYU pragma: export
35
36using CleanupList = std::list<std::function<void()>>;
37using CleanupIt = typename CleanupList::iterator;
38
39inline 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{
52public:
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;
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
79template <typename Interface_, typename Impl_>
80class ProxyClientBase : public Impl_
81{
82public:
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
138template <typename Interface, typename Impl>
139class ProxyClientCustom : public ProxyClientBase<Interface, Impl>
140{
142};
143
146template <typename Interface_, typename Impl_>
147struct ProxyServerBase : public virtual Interface_::Server
148{
149public:
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
170 std::shared_ptr<Impl> m_impl;
172};
173
194template <typename Interface, typename Impl>
195struct ProxyServerCustom : public ProxyServerBase<Interface, Impl>
196{
198};
199
215template <class Fn>
217
221template <class _Class, class _Result, class... _Params>
222struct FunctionTraits<_Result (_Class::*const)(_Params...)>
223{
224 using Params = TypeList<_Params...>;
225 using Result = _Result;
226 template <size_t N>
227 using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
228 using Fields =
229 std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
230
238 template <size_t N>
239 static decltype(auto) Fwd(Param<N>& arg) { return static_cast<Param<N>&&>(arg); }
240};
241
256template <typename MethodParams, typename Enable = void>
258{
260 using Result = void;
261 using Fields = Params;
262
263 template <typename ServerContext>
264 static void invoke(ServerContext&)
265 {
266 }
267};
268
281template <typename MethodParams>
282struct ProxyMethodTraits<MethodParams, Require<decltype(ProxyMethod<MethodParams>::impl)>>
283 : public FunctionTraits<decltype(ProxyMethod<MethodParams>::impl)>
284{
285 template <typename ServerContext, typename... Args>
286 static decltype(auto) invoke(ServerContext& server_context, Args&&... args)
287 {
288 return (server_context.proxy_server.m_impl.get()->*ProxyMethod<MethodParams>::impl)(std::forward<Args>(args)...);
289 }
290};
291
294template <typename MethodParams>
295struct ProxyClientMethodTraits : public ProxyMethodTraits<MethodParams>
296{
297};
298
301template <typename MethodParams>
302struct ProxyServerMethodTraits : public ProxyMethodTraits<MethodParams>
303{
304};
305
306static constexpr int FIELD_IN = 1;
307static constexpr int FIELD_OUT = 2;
308static constexpr int FIELD_OPTIONAL = 4;
309static constexpr int FIELD_REQUESTED = 8;
310static constexpr int FIELD_BOXED = 16;
311
313template <typename Field, int flags>
314struct Accessor : public Field
315{
316 static const bool in = flags & FIELD_IN;
317 static const bool out = flags & FIELD_OUT;
318 static const bool optional = flags & FIELD_OPTIONAL;
319 static const bool requested = flags & FIELD_REQUESTED;
320 static const bool boxed = flags & FIELD_BOXED;
321};
322
324template <typename Fn>
326
328template <typename Result, typename... Args>
329class ProxyCallback<std::function<Result(Args...)>>
330{
331public:
332 virtual Result call(Args&&... args) = 0;
333};
334
335} // namespace mp
336
337#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:321
Event loop implementation.
Definition: proxy-io.h:161
Event loop smart pointer automatically managing m_num_clients.
Definition: proxy.h:51
EventLoopRef & operator=(const EventLoopRef &)=delete
EventLoopRef(EventLoopRef &&other) noexcept
Definition: proxy.h:54
EventLoopRef & operator=(EventLoopRef &&)=delete
EventLoopRef(EventLoop &loop, Lock *lock=nullptr)
Definition: proxy.cpp:48
void reset(bool relock=false)
Definition: proxy.cpp:58
EventLoop * m_loop
Definition: proxy.h:63
EventLoopRef(const EventLoopRef &)=delete
EventLoop * operator->() const
Definition: proxy.h:60
Lock * m_lock
Definition: proxy.h:64
EventLoop & operator*() const
Definition: proxy.h:59
Definition: util.h:170
Wrapper around std::function for passing std::function objects between client and servers.
Definition: proxy.h:325
Base class for generated ProxyClient classes that implement a C++ interface and forward calls to a ca...
Definition: proxy.h:81
Interface::Client m_client
Definition: proxy.h:132
ProxyContext m_context
Definition: proxy.h:133
Interface_ Interface
Definition: proxy.h:83
~ProxyClientBase() noexcept
Definition: proxy-io.h:458
ProxyClientBase(typename Interface::Client client, Connection *connection, bool destroy_connection)
Construct libmultiprocess client object wrapping Cap'n Proto client object with a reference to the as...
Definition: proxy-io.h:398
static void construct(Super &)
Definition: proxy.h:129
static void destroy(Super &)
Definition: proxy.h:130
Customizable (through template specialization) base class used in generated ProxyClient implementatio...
Definition: proxy.h:140
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
std::list< std::function< void()> > CleanupList
Definition: proxy.h:36
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:96
static constexpr int FIELD_OUT
Definition: proxy.h:307
ServerInvokeContext< ProxyServer< Interface >, ::capnp::CallContext< Params, Results > > ServerContext
Definition: proxy-io.h:59
static constexpr int FIELD_BOXED
Definition: proxy.h:310
static constexpr int FIELD_REQUESTED
Definition: proxy.h:309
static constexpr int FIELD_IN
Definition: proxy.h:306
typename CleanupList::iterator CleanupIt
Definition: proxy.h:37
void CleanupRun(CleanupList &fns)
Definition: proxy.h:39
static constexpr int FIELD_OPTIONAL
Definition: proxy.h:308
Accessor type holding flags that determine how to access a message field.
Definition: proxy.h:315
static const bool boxed
Definition: proxy.h:320
static const bool optional
Definition: proxy.h:318
static const bool out
Definition: proxy.h:317
static const bool requested
Definition: proxy.h:319
static const bool in
Definition: proxy.h:316
std::conditional_t< std::is_same_v< void, Result >, Params, TypeList< _Params..., _Result > > Fields
Definition: proxy.h:229
static decltype(auto) Fwd(Param< N > &arg)
Enable perfect forwarding for clientInvoke calls.
Definition: proxy.h:239
typename std::tuple_element< N, std::tuple< _Params... > >::type Param
Definition: proxy.h:227
Function traits class used to get method parameter and result types, used in generated ProxyClient an...
Definition: proxy.h:216
Mapping from capnp interface type to proxy client implementation (specializations are generated by pr...
Definition: proxy.h:25
Customizable (through template specialization) traits class used in generated ProxyClient implementat...
Definition: proxy.h:296
Context data associated with proxy client and server classes.
Definition: proxy.h:69
EventLoopRef loop
Definition: proxy.h:71
Connection * connection
Definition: proxy.h:70
CleanupList cleanup_fns
Definition: proxy.h:72
ProxyContext(Connection *connection)
Definition: proxy.cpp:80
Mapping from capnp method params type to method traits (specializations are generated by proxy-codege...
Definition: proxy.h:30
static decltype(auto) invoke(ServerContext &server_context, Args &&... args)
Definition: proxy.h:286
Traits class for a proxy method, providing the same Params/Result/Param/Fields described in the Funct...
Definition: proxy.h:258
TypeList<> Params
Definition: proxy.h:259
static void invoke(ServerContext &)
Definition: proxy.h:264
Base class for generated ProxyServer classes that implement capnp server methods and forward calls to...
Definition: proxy.h:148
Interface_ Interface
Definition: proxy.h:150
ProxyServerBase(std::shared_ptr< Impl > impl, Connection &connection)
Definition: proxy-io.h:464
ProxyContext m_context
Definition: proxy.h:171
void invokeDestroy()
If the capnp interface defined a special "destroy" method, as described the ProxyClientBase class,...
Definition: proxy-io.h:531
virtual ~ProxyServerBase()
ProxyServer destructor, called from the EventLoop thread by Cap'n Proto garbage collection code after...
Definition: proxy-io.h:483
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:170
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
Customizable (through template specialization) traits class used in generated ProxyServer implementat...
Definition: proxy.h:303
Mapping from capnp struct type to struct traits (specializations are generated by proxy-codegen....
Definition: proxy.h:32
Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen....
Definition: proxy.h:34
ProxyServer & proxy_server
Definition: proxy-io.h:48
Generic utility functions used by capnp code.
Definition: util.h:32
assert(!tx.IsCoinBase())