Flow-IPC 1.0.1
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.
23#include <boost/move/make_unique.hpp>
24
25namespace ipc::transport
26{
27
28// Initializers.
29
30const Shared_name& Native_socket_stream::S_RESOURCE_TYPE_ID = Sync_io_obj::S_RESOURCE_TYPE_ID;
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(Sync_io_obj&& sync_io_core_in_peer_state_moved) :
77 m_impl(boost::movelib::make_unique<Impl>(std::move(sync_io_core_in_peer_state_moved)))
78{
79 // Yay.
80}
81
82Native_socket_stream::~Native_socket_stream() = default; // It's only explicitly defined to formally document it.
83
84const std::string& Native_socket_stream::nickname() const
85{
86 return impl()->nickname();
87}
88
89bool Native_socket_stream::sync_connect(const Shared_name& absolute_name, Error_code* err_code)
90{
91 return impl()->sync_connect(absolute_name, err_code);
92}
93
95{
96 return impl()->remote_peer_process_credentials(err_code);
97}
98
100{
101 return impl()->send_meta_blob_max_size();
102}
103
105{
106 return impl()->send_blob_max_size();
107}
108
110 Error_code* err_code)
111{
112 return impl()->send_native_handle(hndl_or_null, meta_blob, err_code);
113}
114
116{
117 return impl()->send_blob(blob, err_code);
118}
119
121{
122 return impl()->end_sending();
123}
124
126{
127 return impl()->auto_ping(period);
128}
129
131{
132 return impl()->receive_meta_blob_max_size();
133}
134
136{
137 return impl()->receive_blob_max_size();
138}
139
141{
142 return impl()->idle_timer_run(timeout);
143}
144
145// `friend`ship needed for this "non-method method":
146
147std::ostream& operator<<(std::ostream& os, const Native_socket_stream& val)
148{
149 return os << *(val.impl());
150}
151
152// Though, some of them (the templates) had to be written as _fwd() non-templated helpers that take various Function<>s.
153
154bool Native_socket_stream::async_end_sending_fwd(flow::async::Task_asio_err&& on_done_func)
155{
156 return impl()->async_end_sending(std::move(on_done_func));
157}
158
160 const util::Blob_mutable& target_meta_blob,
161 flow::async::Task_asio_err_sz&& on_done_func)
162{
163 return impl()->async_receive_native_handle(target_hndl, target_meta_blob, std::move(on_done_func));
164}
165
167 flow::async::Task_asio_err_sz&& on_done_func)
168{
169 return impl()->async_receive_blob(target_blob, std::move(on_done_func));
170}
171
172} // namespace ipc::transport
Internal, non-movable pImpl implementation of Native_socket_stream class.
Implements both Native_handle_sender and Native_handle_receiver concepts by using a stream-oriented U...
Impl_ptr & impl() const
Helper that simply returns m_impl while guaranteeing that m_impl is non-null upon return.
size_t send_blob_max_size() const
Implements Blob_sender API per contract.
bool idle_timer_run(util::Fine_duration timeout=boost::chrono::seconds(5))
Implements Native_handle_receiver, Blob_receiver API per contract.
~Native_socket_stream()
Implements Native_handle_sender and Native_handle_receiver APIs at the same time, per their concept c...
const std::string & nickname() const
Returns nickname, a brief string suitable for logging.
bool send_blob(const util::Blob_const &blob, Error_code *err_code=0)
Implements Blob_sender API per contract.
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.
bool async_receive_blob_fwd(const util::Blob_mutable &target_blob, flow::async::Task_asio_err_sz &&on_done_func)
Template-free version of async_receive_blob() as required by pImpl idiom.
static const Shared_name & S_RESOURCE_TYPE_ID
Implements concept API.
size_t send_meta_blob_max_size() const
Implements Native_handle_sender API per contract.
bool sync_connect(const Shared_name &absolute_name, Error_code *err_code=0)
To be invoked in NULL state only, it synchronously and non-blockingly attempts to connect to an oppos...
bool async_end_sending_fwd(flow::async::Task_asio_err &&on_done_func)
Template-free version of async_end_sending() as required by pImpl idiom.
bool auto_ping(util::Fine_duration period=boost::chrono::seconds(2))
Implements Native_handle_sender, Blob_sender API per contract.
Native_socket_stream()
Default ctor (object is in NULL state).
bool async_receive_native_handle_fwd(Native_handle *target_hndl, const util::Blob_mutable &target_meta_blob, flow::async::Task_asio_err_sz &&on_done_func)
Template-free version of async_receive_native_handle() as required by pImpl idiom.
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.
Impl_ptr m_impl
The true implementation of this class.
size_t receive_meta_blob_max_size() const
Implements Native_handle_receiver API per contract.
bool end_sending()
Implements Native_handle_sender, Blob_sender API per contract.
size_t receive_blob_max_size() const
Implements Blob_receiver API per contract.
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,...
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...
Implements both sync_io::Native_handle_sender and sync_io::Native_handle_receiver concepts by using a...
A process's credentials (PID, UID, GID as of this writing).
String-wrapping abstraction representing a name uniquely distinguishing a kernel-persistent entity fr...
Flow-IPC module providing transmission of structured messages and/or low-level blobs (and more) betwe...
util::Shared_name Shared_name
Convenience alias for the commonly used type util::Shared_name.
std::ostream & operator<<(std::ostream &os, const Bipc_mq_handle &val)
Prints string representation of the given Bipc_mq_handle to the given ostream.
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
A monolayer-thin wrapper around a native handle, a/k/a descriptor a/k/a FD.