Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
classic_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
21#include "ipc/session/schema/common.capnp.h"
23
24/**
25 * Support for SHM-backed ipc::session sessions and session-servers with the SHM-classic
26 * (ipc::shm::classic::Pool_arena) provider. See the doc header for the general ipc::session::shm namespace.
27 */
28namespace ipc::session::shm::classic // See also ipc::session::shm::classic::rpc {} lower down.
29{
30
31// Types.
32
33// Find doc headers near the bodies of these compound types.
34
35template<typename Session_t>
36class Session_mv;
37
38template<session::schema::MqType MQ_TYPE_OR_NONE, bool TRANSMIT_NATIVE_HANDLES, typename Mdt_payload = ::capnp::Void>
39class Server_session;
40
41template<session::schema::MqType MQ_TYPE_OR_NONE, bool TRANSMIT_NATIVE_HANDLES, typename Mdt_payload = ::capnp::Void>
42class Session_server;
43
44/**
45 * This is to session::Client_session what shm::classic::Server_session is to session::Server_session. In
46 * terms of additional (to that of #Client_session) public behavior, from the public API user's point of
47 * view, this class template alias is simply ~exactly identical/symmetrical to shm::classic::Server_session.
48 * See its doc header. That is during PEER state.
49 *
50 * @warning See same-named section of session::Client_session_mv ctor doc header. In short: the `Client_app`
51 * and `Server_app` passed to the ctor must outlive `*this`.
52 *
53 * The only difference is shm::classic::Server_session::app_shm() and `shm::classic::Client_session::app_shm()`
54 * (actually Session_mv::app_shm() = both) differ in terms of how one views the lifetime of the
55 * underlying SHM areas. This is reflected in the latter's doc header.
56 * Essentially, though, the `Arena*` returned by the former can be used beyond the returning `*this` being destroyed,
57 * as long as its parent shm::classic::Session_server -- or another shm::classic::Server_session from it, with the
58 * same Client_app -- is alive (which is typical). In contrast that is not true of that returned by
59 * `shm::classic::Client_session::app_shm()`; but since the destruction of a #Client_session implies either the
60 * process is itself going down, or the opposing server process (and thus shm::classic::Session_server) is going
61 * down/has gone down, this should not be relevant, as those objects should never be accessed again anyway.
62 *
63 * @internal
64 * ### Implementation ###
65 * First see the Implementation section of shm::classic::Server_session. It gives a detailed description of the
66 * overall design -- both its own side and this (client) side too. So read it; then come back here.
67 *
68 * That said, shm::classic::Client_session is a much simpler situation. It is not inter-operating with
69 * any server object; and (as explained in the referenced doc header) all it needs to do is open the pools
70 * guaranteed to exist by the time the vanilla #Client_session enters PEER state (just past log-in response receipt).
71 *
72 * So all we have to do is provide a modified `async_connect()` which:
73 * - executes vanilla session::Client_session_impl::async_connect(); once that triggers the on-done handler:
74 * - open the 2 SHM pools (a synchronous op);
75 * - invoke the user's original on-done handler.
76 *
77 * In `*this`, mechanically: the true implementation of the needed setup and accessors (explained above) is in
78 * shm::classic::Client_session_impl, and the vanilla core of that is in its super-class session::Client_session_impl.
79 * That's the key; then Client_session_mv adds movability around that guy; and lastly this type sub-classes *that*
80 * and completes the puzzle by pImpl-forwarding to the added (SHM-focused) API.
81 *
82 * The last and most boring piece of the puzzle are the pImpl-lite wrappers around the SHM-specific API
83 * that shm::classic::Client_session adds to super-class Client_session_mv: `session_shm()` and so on.
84 * Just see same spot in shm::classic::Server_session doc header; it explains both client and server sides.
85 * @endinternal
86 *
87 * @tparam MQ_TYPE_OR_NONE
88 * Identical to session::Client_session.
89 * @tparam TRANSMIT_NATIVE_HANDLES
90 * Identical to session::Client_session.
91 * @tparam Mdt_payload
92 * Identical to session::Client_session.
93 */
94template<session::schema::MqType MQ_TYPE_OR_NONE, bool TRANSMIT_NATIVE_HANDLES, typename Mdt_payload = ::capnp::Void>
99
100// Free functions.
101
102/**
103 * Prints string representation of the given `Session_mv` to the given `ostream`.
104 *
105 * @relatesalso Session_mv
106 *
107 * @param os
108 * Stream to which to write.
109 * @param val
110 * Object to serialize.
111 * @return `os`.
112 */
113template<typename Session_t>
114std::ostream& operator<<(std::ostream& os, const Session_mv<Session_t>& val);
115
116/**
117 * Prints string representation of the given `Server_session` to the given `ostream`.
118 *
119 * @relatesalso Server_session
120 *
121 * @param os
122 * Stream to which to write.
123 * @param val
124 * Object to serialize.
125 * @return `os`.
126 */
127template<session::schema::MqType MQ_TYPE_OR_NONE, bool TRANSMIT_NATIVE_HANDLES,
128 typename Mdt_payload>
129std::ostream& operator<<(std::ostream& os,
130 const Server_session
131 <MQ_TYPE_OR_NONE, TRANSMIT_NATIVE_HANDLES, Mdt_payload>& val);
132
133/**
134 * Prints string representation of the given `Session_server` to the given `ostream`.
135 *
136 * @relatesalso Session_server
137 *
138 * @param os
139 * Stream to which to write.
140 * @param val
141 * Object to serialize.
142 * @return `os`.
143 */
144template<session::schema::MqType MQ_TYPE_OR_NONE, bool TRANSMIT_NATIVE_HANDLES,
145 typename Mdt_payload>
146std::ostream& operator<<(std::ostream& os,
147 const Session_server
148 <MQ_TYPE_OR_NONE, TRANSMIT_NATIVE_HANDLES, Mdt_payload>& val);
149
150} // namespace ipc::session::shm::classic
151
152/**
153 * Sub-namespace containing utilities useful when working with capnp-RPC-with-zero-copy module of Flow-IPC
154 * (transport::struc::shm::rpc) -- assuming one has chosen SHM-classic as the SHM-provider for such work.
155 */
157{
158
159// Types.
160
161/**
162 * Having decided to use SHM-classic SHM-provider: Suitable template argument for
163 * transport::struc::shm::rpc::Client_context, or more generally a suitable session::Client_session for
164 * transport::struc::shm::rpc::Session_vat_network.
165 *
166 * @tparam S_MQ_TYPE_OR_NONE
167 * Whether you want the session to include a transport::Persistent_mq_handle (MQ) pipe; and if so
168 * what kind. The default `NONE` is best, unless you specifically intend to create side channel(s)
169 * in addition to the one used for the central capnp-RPC session; in the latter case choose the one
170 * that fits your performance needs best.
171 *
172 * @internal
173 * ### Rationale ###
174 * Why this value for the alias and the ones nearby?
175 * - By definition it must be a session::Client_session variant that is SHM-enabled.
176 * - transport::struc::shm::rpc::Client_context (et al), by its internal protocol, sends a native handle --
177 * the stream-socket underlying the capnp-RPC conversation -- over the first init-channel; so specifying
178 * `true` there is required, guaranteeing the session will produce Native_handle-bearing channel(s).
179 * - An additional MQ-based pipe *might* be useful for user's side channels (if any), so `NONE` is default, but
180 * they can choose an actual MQ type instead.
181 */
182template<session::schema::MqType S_MQ_TYPE_OR_NONE = session::schema::MqType::NONE>
184
185/**
186 * Having decided to use SHM-classic SHM-provider: Suitable template argument for
187 * transport::struc::shm::rpc::Context_server, or more generally a suitable session::Session_server for
188 * generating `Session`s in use with transport::struc::shm::rpc::Session_vat_network.
189 *
190 * @tparam S_MQ_TYPE_OR_NONE
191 * See #Client_session.
192 */
193template<session::schema::MqType S_MQ_TYPE_OR_NONE = session::schema::MqType::NONE>
195
196/**
197 * Having decided to use SHM-classic SHM-provider: Generally a suitable session::Server_session for
198 * transport::struc::shm::rpc::Session_vat_network.
199 *
200 * @tparam S_MQ_TYPE_OR_NONE
201 * See #Client_session.
202 */
203template<session::schema::MqType S_MQ_TYPE_OR_NONE = session::schema::MqType::NONE>
205
206} // namespace ipc::session::shm::classic::rpc
Implements Session concept on the Client_app end: a Session_mv that first achieves PEER state by conn...
Core internally-used implementation of shm::classic::Client_session: it is to the latter what its pub...
Identical to session::Server_session in every way, except that it makes available two SHM arenas,...
Implements the SHM-related API common to shm::classic::Server_session and shm::classic::Client_sessio...
Definition: session.hpp:47
This is to vanilla Session_server what shm::classic::Server_session is to vanilla Server_session: it ...
Sub-namespace containing utilities useful when working with capnp-RPC-with-zero-copy module of Flow-I...
classic::Session_server< S_MQ_TYPE_OR_NONE, true > Session_server
Having decided to use SHM-classic SHM-provider: Suitable template argument for transport::struc::shm:...
Support for SHM-backed ipc::session sessions and session-servers with the SHM-classic (ipc::shm::clas...
Definition: classic_fwd.hpp:26
std::ostream & operator<<(std::ostream &os, const Session_impl< Session_impl_t > &val)
Prints string representation of the given Session_impl to the given ostream.
Server_session_mv< Server_session_impl< MQ_TYPE_OR_NONE, TRANSMIT_NATIVE_HANDLES, Mdt_payload > > Server_session
A vanilla Server_session with no optional capabilities.