Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
rpc_fwd.hpp
Go to the documentation of this file.
1/* Flow-IPC: Shared Memory
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
23
24/**
25 * Provides Flow-IPC's integration with Cap'n Proto's Remote Procedure Call (RPC) layer, wherein one can
26 * perform all the cool, promise-pipelined, interface-and-callback-happy RPC provided by capnp-RPC -- while
27 * enjoying the zero-copy performance provided by Flow-IPC's SHM features. Naturally this works for IPC-RPC
28 * local to one machine (as SHM is local to one machine) -- not networked -- but one should be able to
29 * slot-it-in painlessly whenever no network is involved; and revert to normal networked capnp-RPC otherwise;
30 * with only a handful lines of code different between the two.
31 *
32 * Generally speaking, here is how this module relates to a few other key modules:
33 * - struc::shm::rpc *implements* capnp-RPC interfaces and concepts, so that one can use capnp-RPC as normal --
34 * but zero-copy-enabled (plus some added niceties, namely optional ipc::session support in peer-process discovery
35 * and session establishment/termination).
36 * - struc::shm::rpc layer *sits on top of* Flow-IPC's SHM-enabled structured-transport layer,
37 * ipc::transport::struc::shm. Notably, to zero-copyify vanilla capnp-RPC, internally it makes use
38 * of public APIs shm::Capnp_message_builder and shm::Capnp_message_reader (which themselves implement key
39 * interfaces `capnp::MessageBuilder` and `capnp::MessageReader`, respectively, in capnp's lower serialization
40 * layer).
41 * - struc::shm::rpc is a cousin of, and an *alternative to*, struc::Channel (+ struc::Msg_out, struc::Msg_in):
42 * both carry capnp-encoded data between processes with zero-copy performance; they differ in programming model.
43 * See just below.
44 *
45 * ### capnp-RPC versus struc::Channel ###
46 * struc::Channel transmits a stream of the user's own capnp-encoded messages (and, optionally, native handles),
47 * adding message-type demultiplexing and request/response. It is simple to set up, simple to reason about (including
48 * in terms of perf), and can be slotted into nearly any event loop.
49 *
50 * capnp-RPC is semantically far more powerful: function-call-like semantics on remote objects (capabilities),
51 * callbacks, and *promise pipelining* ("time travel"). It is also an established, mature API in its own right;
52 * Flow-IPC "merely" provides the zero-copy plumbing underneath (plus optional ipc::session integration). The price is
53 * that one uses, or integrates with, the KJ promise-based single-threaded event loop.
54 *
55 * Both are viable. (The struc::Channel doc header contains a similar comparison from its point of view.)
56 *
57 * ### Map of this namespace ###
58 * From highest-level (easiest to use) to lowest-level:
59 * - Ez_rpc_client, Ez_rpc_server: analogous to capnp's `EzRpcClient`, `EzRpcServer`. They set up everything,
60 * including the KJ event loop, in a few lines.
61 * - Client_context, Context_server (which yields `Server_context`s): establish the Flow-IPC session (including SHM)
62 * with the opposing process and supply the `VatNetwork`; you create and drive the `capnp::RpcSystem` yourself.
63 * - Session_vat_network: the core: Flow-IPC's implementation of `capnp::VatNetwork`; essentially
64 * `capnp::TwoPartyVatNetwork` but with zero-copy. The above classes use it internally.
65 *
66 * ### Learning capnp-RPC; examples ###
67 * How to use capnp-RPC itself is outside our scope here. See https://capnproto.org/rpc.html and
68 * https://capnproto.org/cxxrpc.html; and capnp's Calculator sample (`c++/samples/calculator-*`). Flow-IPC's
69 * `test/suite/rpc_demo` program replicates that sample using the classes above.
70 *
71 * @see The guided Manual chapter @ref chan_rpc covers these topics.
72 */
74{
75
76// Types.
77
78// Find doc headers near the bodies of these compound types.
79
80template<typename Shm_lender_borrower_t, typename Shm_arena_t>
81class Session_vat_network;
82
83template<typename Client_session_t>
84class Client_context;
85template<typename Client_session_t>
86class Server_context;
87template<typename Session_server_t>
88class Context_server;
89
90template<typename Client_session_t>
91class Ez_rpc_client;
92template<typename Session_server_t>
93class Ez_rpc_server;
94
95/* Various Flow-styled aliases of capnp types (Rpc_conn, Rpc_msg_out, Rpc_msg_in, Vat_id, Rpc_system) live in
96 * session_vat_network.hpp -- not here, though normally aliases would belong in the present _fwd.hpp file. Why?
97 * Answer: capnp ships no _fwd.hpp counterparts of its own; so it was a choice between hand-forward-declaring capnp
98 * types here (fragile against upstream changes; and impossible for Rpc_conn, which aliases a *nested* class)
99 * and placing the aliases where the capnp-RPC headers are included anyway. Hence: there. Bonus: the present
100 * file thus stays light-weight (capnp-RPC-include-free) for non-RPC-using code. */
101
102// Free functions.
103
104/**
105 * Prints string representation of the given Session_vat_network to the given `ostream`.
106 *
107 * @relatesalso Session_vat_network
108 *
109 * @param os
110 * Stream to which to write.
111 * @param val
112 * Object to serialize.
113 * @return `os`.
114 */
115template<typename Shm_lender_borrower_t, typename Shm_arena_t>
116std::ostream& operator<<(std::ostream& os, const Session_vat_network<Shm_lender_borrower_t, Shm_arena_t>& val);
117
118/**
119 * Prints string representation of the given Client_context to the given `ostream`.
120 *
121 * @relatesalso Client_context
122 *
123 * @param os
124 * Stream to which to write.
125 * @param val
126 * Object to serialize.
127 * @return `os`.
128 */
129template<typename Client_session_t>
130std::ostream& operator<<(std::ostream& os, const Client_context<Client_session_t>& val);
131
132/**
133 * Prints string representation of the given Server_context to the given `ostream`.
134 *
135 * @relatesalso Server_context
136 *
137 * @param os
138 * Stream to which to write.
139 * @param val
140 * Object to serialize.
141 * @return `os`.
142 */
143template<typename Server_session_t>
144std::ostream& operator<<(std::ostream& os, const Server_context<Server_session_t>& val);
145
146/**
147 * Prints string representation of the given Context_server to the given `ostream`.
148 *
149 * @relatesalso Context_server
150 *
151 * @param os
152 * Stream to which to write.
153 * @param val
154 * Object to serialize.
155 * @return `os`.
156 */
157template<typename Session_server_t>
158std::ostream& operator<<(std::ostream& os, const Context_server<Session_server_t>& val);
159
160/**
161 * Prints string representation of the given Ez_rpc_server to the given `ostream`.
162 *
163 * @relatesalso Ez_rpc_server
164 *
165 * @param os
166 * Stream to which to write.
167 * @param val
168 * Object to serialize.
169 * @return `os`.
170 */
171template<typename Session_server_t>
172std::ostream& operator<<(std::ostream& os, const Ez_rpc_server<Session_server_t>& val);
173
174/**
175 * Prints string representation of the given Ez_rpc_client to the given `ostream`.
176 *
177 * @relatesalso Ez_rpc_client
178 *
179 * @param os
180 * Stream to which to write.
181 * @param val
182 * Object to serialize.
183 * @return `os`.
184 */
185template<typename Client_session_t>
186std::ostream& operator<<(std::ostream& os, const Ez_rpc_client<Client_session_t>& val);
187
188} // namespace ipc::transport::struc::shm::rpc
Provides Flow-IPC's integration with Cap'n Proto's Remote Procedure Call (RPC) layer,...
std::ostream & operator<<(std::ostream &os, const Client_context< Client_session_t > &val)
Prints string representation of the given Client_context to the given ostream.