Flow-IPC 1.0.0
Flow-IPC project: Full implementation reference.
native_socket_stream.cpp
Go to the documentation of this file.
1/* Flow-IPC: Core
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
20// Include Native_socket_stream::Impl class body to complete that type and enable pImpl forwarding.
22#include <boost/move/make_unique.hpp>
23
25{
26
27// Initializers.
28
30const size_t& Native_socket_stream::S_MAX_META_BLOB_LENGTH = Impl::S_MAX_META_BLOB_LENGTH;
31
32// Implementations (strict pImpl-idiom style).
33
34// The performant move semantics we get delightfully free with pImpl; they'll just move-to/from the unique_ptr m_impl.
35
36Native_socket_stream::Native_socket_stream(Native_socket_stream&&) = default;
37Native_socket_stream& Native_socket_stream::operator=(Native_socket_stream&&) = default;
38
39// Oh and this helper is needed; just see its doc header for rationale.
40
42{
43 using boost::movelib::make_unique;
44
45 if (!m_impl)
46 {
47 m_impl = make_unique<Impl>(nullptr, "");
48 }
49
50 return m_impl;
51}
52
53// Provide specific default ctor for documentation elegance:
54
56 Native_socket_stream(nullptr, "")
57{
58 // Yay.
59}
60
61// The rest is strict forwarding to impl() (essentially m_impl).
62
63Native_socket_stream::Native_socket_stream(flow::log::Logger* logger_ptr, util::String_view nickname_str) :
64 m_impl(boost::movelib::make_unique<Impl>(logger_ptr, nickname_str))
65{
66 // Yay.
67}
68
69Native_socket_stream::Native_socket_stream(flow::log::Logger* logger_ptr, util::String_view nickname_str,
70 Native_handle&& native_peer_socket_moved) :
71 m_impl(boost::movelib::make_unique<Impl>(logger_ptr, nickname_str, std::move(native_peer_socket_moved)))
72{
73 // Yay.
74}
75
76Native_socket_stream::~Native_socket_stream() = default; // It's only explicitly defined to formally document it.
77
78const std::string& Native_socket_stream::nickname() const
79{
80 return impl()->nickname();
81}
82
83bool Native_socket_stream::sync_connect(const Shared_name& absolute_name, Error_code* err_code)
84{
85 return impl()->sync_connect(absolute_name, err_code);
86}
87
88#if 0 // See the declaration in class { body }; explains why `if 0` yet still here.
89Native_socket_stream Native_socket_stream::release()
90{
91 auto sock = std::move(*this);
92 sock.impl()->reset_sync_io_setup();
93 return sock;
94}
95#endif
96
98{
99 return impl()->remote_peer_process_credentials(err_code);
100}
101
103{
104 return impl()->send_meta_blob_max_size();
105}
106
108{
109 return impl()->send_blob_max_size();
110}
111
113 Error_code* err_code)
114{
115 return impl()->send_native_handle(hndl_or_null, meta_blob, err_code);
116}
117
119{
120 return impl()->send_blob(blob, err_code);
121}
122
124{
125 return impl()->end_sending();
126}
127
129{
130 return impl()->auto_ping(period);
131}
132
134{
135 return impl()->receive_meta_blob_max_size();
136}
137
139{
140 return impl()->receive_blob_max_size();
141}
142
144{
145 return impl()->idle_timer_run(timeout);
146}
147
148flow::log::Logger* Native_socket_stream::get_logger() const
149{
150 return impl()->get_logger();
151}
152
153// `friend`ship needed for this "non-method method":
154
155std::ostream& operator<<(std::ostream& os, const Native_socket_stream& val)
156{
157 return os << *(val.impl());
158}
159
160// Though, some of them (the templates) had to be written as _fwd() non-templated helpers that take various Function<>s.
161
163 (const Function<util::sync_io::Asio_waitable_native_handle ()>& create_ev_wait_hndl_func)
164{
165 return impl()->replace_event_wait_handles(create_ev_wait_hndl_func);
166}
167
169{
170 return impl()->start_send_native_handle_ops(std::move(ev_wait_func));
171}
172
174{
175 return impl()->start_send_blob_ops(std::move(ev_wait_func));
176}
177
178bool Native_socket_stream::async_end_sending_fwd(Error_code* sync_err_code, flow::async::Task_asio_err&& on_done_func)
179{
180 return impl()->async_end_sending(sync_err_code, std::move(on_done_func));
181}
182
184{
185 return impl()->start_receive_native_handle_ops(std::move(ev_wait_func));
186}
187
189{
190 return impl()->start_receive_blob_ops(std::move(ev_wait_func));
191}
192
194 const util::Blob_mutable& target_meta_blob,
195 Error_code* sync_err_code, size_t* sync_sz,
196 flow::async::Task_asio_err_sz&& on_done_func)
197{
198 return impl()->async_receive_native_handle(target_hndl, target_meta_blob, sync_err_code, sync_sz,
199 std::move(on_done_func));
200}
201
203 Error_code* sync_err_code, size_t* sync_sz,
204 flow::async::Task_asio_err_sz&& on_done_func)
205{
206 return impl()->async_receive_blob(target_blob, sync_err_code, sync_sz, std::move(on_done_func));
207}
208
209} // namespace ipc::transport::sync_io
Internal, non-movable pImpl implementation of sync_io::Native_socket_stream class.
Implements both sync_io::Native_handle_sender and sync_io::Native_handle_receiver concepts by using a...
static const size_t & S_MAX_META_BLOB_LENGTH
The maximum length of a blob that can be sent by this protocol.
util::Process_credentials remote_peer_process_credentials(Error_code *err_code=0) const
OS-reported process credential (PID, etc.) info about the other connected peer's process,...
bool async_end_sending_fwd(Error_code *sync_err_code, flow::async::Task_asio_err &&on_done_func)
Template-free version of async_end_sending() as required by pImpl idiom.
static const Shared_name S_RESOURCE_TYPE_ID
Implements concept API.
bool send_blob(const util::Blob_const &blob, Error_code *err_code=0)
Implements Blob_sender API per contract.
bool sync_connect(const Shared_name &absolute_name, Error_code *err_code=0)
Identical to Async_io_obj counterpart.
Native_socket_stream()
Default ctor (object is in NULL state).
std::experimental::propagate_const< boost::movelib::unique_ptr< Impl > > Impl_ptr
Short-hand for const-respecting wrapper around Native_socket_stream::Impl for the pImpl idiom.
bool end_sending()
Implements Native_handle_sender, Blob_sender API per contract.
bool replace_event_wait_handles_fwd(const Function< util::sync_io::Asio_waitable_native_handle()> &create_ev_wait_hndl_func)
Template-free version of replace_event_wait_handles() as required by pImpl idiom.
const std::string & nickname() const
Returns nickname, a brief string suitable for logging.
bool async_receive_blob_fwd(const util::Blob_mutable &target_blob, Error_code *sync_err_code, size_t *sync_sz, flow::async::Task_asio_err_sz &&on_done_func)
Template-free version of async_receive_blob() as required by pImpl idiom.
Impl_ptr & impl() const
Helper that simply returns m_impl while guaranteeing that m_impl is non-null upon return.
Native_socket_stream & operator=(Native_socket_stream &&src)
Move-assigns from src; *this acts as if destructed; src becomes as-if default-cted (therefore in NULL...
size_t send_meta_blob_max_size() const
Implements Native_handle_sender API per contract.
bool start_send_blob_ops_fwd(util::sync_io::Event_wait_func &&ev_wait_func)
Template-free version of start_send_blob_ops() as required by pImpl idiom.
bool send_native_handle(Native_handle hndl_or_null, const util::Blob_const &meta_blob, Error_code *err_code=0)
Implements Native_handle_sender API per contract.
Impl_ptr m_impl
The true implementation of this class.
bool start_send_native_handle_ops_fwd(util::sync_io::Event_wait_func &&ev_wait_func)
Template-free version of start_send_native_handle_ops() as required by pImpl idiom.
size_t receive_blob_max_size() const
Implements Blob_receiver API per contract.
size_t send_blob_max_size() const
Implements Blob_sender API per contract.
flow::log::Logger * get_logger() const
Returns logger (possibly null).
bool auto_ping(util::Fine_duration period=boost::chrono::seconds(2))
Implements Native_handle_sender, Blob_sender API per contract.
bool async_receive_native_handle_fwd(Native_handle *target_hndl, const util::Blob_mutable &target_meta_blob, Error_code *sync_err_code, size_t *sync_sz, flow::async::Task_asio_err_sz &&on_done_func)
Template-free version of async_receive_native_handle() as required by pImpl idiom.
bool idle_timer_run(util::Fine_duration timeout=boost::chrono::seconds(5))
Implements Native_handle_receiver, Blob_receiver API per contract.
size_t receive_meta_blob_max_size() const
Implements Native_handle_receiver API per contract.
bool start_receive_native_handle_ops_fwd(util::sync_io::Event_wait_func &&ev_wait_func)
Template-free version of start_receive_native_handle_ops() as required by pImpl idiom.
~Native_socket_stream()
Implements Native_handle_sender and Native_handle_receiver APIs at the same time, per their concept c...
bool start_receive_blob_ops_fwd(util::sync_io::Event_wait_func &&ev_wait_func)
Template-free version of start_receive_blob_ops() as required by pImpl idiom.
A process's credentials (PID, UID, GID as of this writing).
String-wrapping abstraction representing a name uniquely distinguishing a kernel-persistent entity fr...
static Shared_name ct(const Source &src)
Copy-constructs from a char-sequence container (including string, util::String_view,...
Useful if using the sync_io pattern within a user event loop built on boost.asio (optionally with flo...
sync_io-pattern counterparts to async-I/O-pattern object types in parent namespace ipc::transport.
std::ostream & operator<<(std::ostream &os, const Blob_stream_mq_receiver_impl< Persistent_mq_handle > &val)
Prints string representation of the given Blob_stream_mq_receiver_impl to the given ostream.
util::Shared_name Shared_name
Convenience alias for the commonly used type util::Shared_name.
Function< void(Asio_waitable_native_handle *hndl_of_interest, bool ev_of_interest_snd_else_rcv, Task_ptr &&on_active_ev_func)> Event_wait_func
In sync_io pattern, concrete type storing user-supplied function invoked by pattern-implementing ipc:...
boost::asio::mutable_buffer Blob_mutable
Short-hand for an mutable blob somewhere in memory, stored as exactly a void* and a size_t.
Definition: util_fwd.hpp:134
flow::Fine_duration Fine_duration
Short-hand for Flow's Fine_duration.
Definition: util_fwd.hpp:111
boost::asio::const_buffer Blob_const
Short-hand for an immutable blob somewhere in memory, stored as exactly a void const * and a size_t.
Definition: util_fwd.hpp:128
flow::util::String_view String_view
Short-hand for Flow's String_view.
Definition: util_fwd.hpp:109
flow::Error_code Error_code
Short-hand for flow::Error_code which is very common.
Definition: common.hpp:297
flow::Function< Signature > Function
Short-hand for polymorphic functor holder which is very common. This is essentially std::function.
Definition: common.hpp:301
A monolayer-thin wrapper around a native handle, a/k/a descriptor a/k/a FD.