Flow-IPC 1.0.2
Flow-IPC project: Full implementation reference.
channel_base.hpp
Go to the documentation of this file.
1/* Flow-IPC: Structured Transport
2 * Copyright (c) 2023 Akamai Technologies, Inc.; and other contributors.
3 * Each commit is copyright by its respective author or author's employer.
4 *
5 * Licensed under the MIT License:
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE. */
24
25/// @file
26#pragma once
27
29
31{
32
33// Types.
34
35/// `Channel` base that contains non-parameterized `public` items such as tag types and constants.
37{
38public:
39 // Types.
40
41 /**
42 * Clarifying short-hand for outgoing-message IDs. This is public for logging/reporting use only
43 * (see set_remote_unexpected_response_handler()) as of this writing.
44 */
46
47 /**
48 * Tag type for ctor selection: Backing memory for serialization comes from fixed-size segment allocation(s) from
49 * regular heap (`malloc()`); and similarly for deserialization. Therefore the serialization is copied
50 * into the Channel::Owned_channel transport in the Channel::send() invoker process;
51 * then copied out of there by the opposing (receiving) process. I.e., this mode is *not* zero-copy, in
52 * both directions.
53 *
54 * More specifically: Heap_fixed_builder and Heap_reader are the serialization engines used.
55 *
56 * For concision, and to compile successfully, use the struc::Channel alias class template:
57 * #Channel_via_heap.
58 *
59 * ### Relative benefits/limitations (versus SHM-based `Serializa_via_*_shm`s) ###
60 * These are discussed in some detail in Heap_fixed_builder doc header. Short version:
61 * Pros:
62 * - It's simple internally and externally, and cleanup is 100% guaranteed (no kernel-persistent resources
63 * are involved in serialization of structured messages).
64 * - In terms of simplicity specifically: it is not required to operate within ipc::session at all;
65 * whereas `Serializa_via_*_shm` are so required.
66 * - Subtlety: One *can* operate the SHM-based builders without ipc::session. To do so you'll have to
67 * use the struc::Channel ctor form wherein you provide your own builder and reader engines
68 * which you'll need to first create manually. One cannot do so with Serialize_via_heap ctor form,
69 * however, which is intended for easiest possible struc::Channel setup.
70 *
71 * Cons:
72 * - It's not zero-copy. A linear-time copy is involved on each side of a transmitted message's processing.
73 * - If a leaf is too large to fit into the underlying `Owned_channel` pipe, the transmission will fail,
74 * and the struc::Channel will be hosed. I.e., it places limits on what can be transmitted, and the
75 * exact nature of these limits is not 100% exact/predictable.
76 */
78
79 /**
80 * Tag type for ctor selection: Backing RAM for serialization comes from a given session::Session's SHM arena
81 * (of per-session scope) via exponential-growing-size segment allocation(s). Subsequent deserialization in the
82 * opposing process occurs from directly within the same backing RAM area(s). Therefore the serialization
83 * is *never* copied, period; only a short handle is internally copied into and out of the `Owned_channel` transport.
84 * Conversely, the opposing process does the same as the sender, and we do the converse as the receiver.
85 * I.e., this mode *is* zero-copy, in both directions. Serialize_via_session_shm is usable only within the
86 * ipc::session framework; if you are operating outside it then use the struc::Channel ctor form wherein the
87 * builder/reader engines are supplied as args.
88 *
89 * More specifically, as of this writing: If the `Session` type in tag-form struc::Channel ctor is
90 * session::shm::classic::Server_session or session::shm::classic::Client_session,
91 * then shm::classic::Builder and shm::classic::Reader are the [de]serialization engines used. If the
92 * `Session` type in tag-form struc::Channel ctor is session::shm::arena_lend::jemalloc::Server_session
93 * or session::shm::arena_lend::jemalloc::Client_session, then shm::arena_lend::jemalloc::Builder and
94 * shm::arena_lend::jemalloc::Reader are the [de]serialization engines used. The source arena for our serialization
95 * (message building) comes from the `Session` impl supplied nearby in the ctor call; specifically its
96 * `.session_shm()` arena.
97 *
98 * For concision, and to compile successfully, use the struc::Channel alias class template:
99 * shm::classic::Channel or shm::arena_lend::jemalloc::Channel.
100 *
101 * Relative benefits/limitations
102 * -----------------------------
103 * ### As compared to Serialize_via_heap ###
104 * Essentially they are the inverse of those of Serialize_via_heap above.
105 *
106 * ### shm::classic::Channel versus shm::arena_lend::jemalloc::Channel ###
107 * The relative merits of the two SHM providers (SHM-classic versus SHM-jemalloc) in ipc::shm are discussed within
108 * that namespace's docs. Short version:
109 * - SHM-jemalloc is intended for safety (due to its asymmetrical internally-IPC-using nature) and efficiency of
110 * allocation (jemalloc is an industry-strength `malloc()` impl, which we've adapted to SHM use; whereas
111 * SHM-classic uses boost.ipc's relatively rudimentary default allocation algorithm (e.g., it does not do thread
112 * caching)).
113 */
115
116 /**
117 * Similar to Serialize_via_session_shm but assumes per-app-scope SHM-arena (as opposed to
118 * per-session-scope) at compile time. More specifically: The source arena for our serialization comes from the
119 * `Session` impl supplied nearby in the ctor call; specifically its `.app_shm()` arena. As for deserialization
120 * on our end: we will be able to handle any objects sent from the other side, period.
121 *
122 * `Session` must be specifically *not* of the `"ipc::session::shm::arena_lend::*::Server_session"` variety;
123 * `"ipc::session::shm::arena_lend::*::Client_session"` cannot be used (any attempt
124 * will not compile). This is because the latter lacks `.app_shm()` due to the nature of arena-lending
125 * SHM providers, where each process allocates from an arena it maintains for itself (and from which
126 * the other process can only borrow objects). Some more notes on this are below in this doc header.
127 *
128 * For concision, and to compile successfully, use the struc::Channel alias class template:
129 * shm::classic::Channel or shm::arena_lend::jemalloc::Channel.
130 *
131 * Relative benefits/limitations
132 * -----------------------------
133 * ### As compared to Serialize_via_heap ###
134 * Essentially they are the inverse of those of Serialize_via_heap above.
135 *
136 * ### shm::classic::Channel versus shm::arena_lend::jemalloc::Channel ###
137 * See Serialize_via_session_shm.
138 *
139 * In addition there is a *possibly* crucial difference in capabilities, depending on one's app needs
140 * (it is mentioned above in this doc header):
141 * - With SHM-jemalloc (via shm::arena_lend::jemalloc::Channel), it is *not* possible
142 * to use this per-app-scope mode on the client side; only per-session scope is available (this is
143 * a consequence of its asymmetrical, safety-oriented design as of this writing).
144 * On the `Server_session` end it is possible to create out-messages in either scope however.
145 * - An attempt to use this per-app-scope mode with a `session::shm::arena_lend::jemalloc::Client_session`
146 * will be caught at compile time (it... won't compile).
147 * - Specifically, then, with SHM-jemalloc, in session-server use either Serialize_via_app_shm or
148 * Serialize_via_session_shm; in the opposing session-client use the latter only. Deserialization will work
149 * on both sides; but serialization (building/sending) will work only for session-scope resources
150 * (again, attempt to do otherwise will not compile).
151 */
153
154 // Constants.
155
156 /// The sole value of the tag type Serialize_via_heap.
158
159 /// The sole value of the tag type Serialize_via_session_shm.
161
162 /// The sole value of the tag type Serialize_via_app_shm.
164}; // class Channel_base
165
166} // namespace ipc::transport::struc
Channel base that contains non-parameterized public items such as tag types and constants.
static constexpr Serialize_via_session_shm S_SERIALIZE_VIA_SESSION_SHM
The sole value of the tag type Serialize_via_session_shm.
static constexpr Serialize_via_heap S_SERIALIZE_VIA_HEAP
The sole value of the tag type Serialize_via_heap.
msg_id_t msg_id_out_t
Clarifying short-hand for outgoing-message IDs.
static constexpr Serialize_via_app_shm S_SERIALIZE_VIA_APP_SHM
The sole value of the tag type Serialize_via_app_shm.
Sub-module of Flow-IPC module ipc::transport providing transmission of structured messages specifical...
Definition: channel.cpp:31
uint64_t msg_id_t
Message ID uniquely identifying outgoing message (Msg_out, among all other Msg_outs),...
Definition: struc_fwd.hpp:145
Similar to Serialize_via_session_shm but assumes per-app-scope SHM-arena (as opposed to per-session-s...
Tag type for ctor selection: Backing memory for serialization comes from fixed-size segment allocatio...
Tag type for ctor selection: Backing RAM for serialization comes from a given session::Session's SHM ...