Flow-IPC 3.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
22#include <boost/move/make_unique.hpp>
23#include <ostream>
24#include <string>
25#include <utility>
26
27namespace ipc::transport
28{
29
30// Initializers.
31
32const Shared_name& Native_socket_stream::S_RESOURCE_TYPE_ID = Sync_io_obj::S_RESOURCE_TYPE_ID;
33
34// Implementations.
35
36Native_socket_stream::Native_socket_stream(Native_socket_stream&&) = default; // Nice and performant: ptr exchange.
37Native_socket_stream& Native_socket_stream::operator=(Native_socket_stream&&) = default; // Ditto.
38
40{
41 return m_impl ? m_impl
42 : (m_impl = boost::movelib::make_unique<Impl>(nullptr, ""));
43}
44
46 Native_socket_stream(nullptr, "")
47{
48 // Yay.
49}
50
51// The rest is forwarding to impl() (essentially m_impl) a-la pImpl.
52
53Native_socket_stream::Native_socket_stream(flow::log::Logger* logger_ptr, util::String_view nickname_str) :
54 m_impl(boost::movelib::make_unique<Impl>(logger_ptr, nickname_str))
55{
56 // Yay.
57}
58
59Native_socket_stream::Native_socket_stream(flow::log::Logger* logger_ptr, util::String_view nickname_str,
60 Native_handle&& native_peer_socket_moved) :
61 m_impl(boost::movelib::make_unique<Impl>(logger_ptr, nickname_str, std::move(native_peer_socket_moved)))
62{
63 // Yay.
64}
65
66Native_socket_stream::Native_socket_stream(Sync_io_obj&& sync_io_core_in_peer_state_moved) :
67 m_impl(boost::movelib::make_unique<Impl>(std::move(sync_io_core_in_peer_state_moved)))
68{
69 // Yay.
70}
71
72Native_socket_stream::~Native_socket_stream() = default; // It's only explicitly defined to formally document it.
73
74const std::string& Native_socket_stream::nickname() const
75{
76 return impl()->nickname();
77}
78
79bool Native_socket_stream::sync_connect(const Shared_name& absolute_name, Error_code* err_code)
80{
81 return impl()->sync_connect(absolute_name, err_code);
82}
83
85{
86 return impl()->remote_peer_process_credentials(err_code);
87}
88
90{
91 return impl()->remote_peer_process_credentials(creds);
92}
93
95{
96 return impl()->send_meta_blob_max_size();
97}
98
100{
101 return impl()->send_blob_max_size();
102}
103
105 Error_code* err_code)
106{
107 return impl()->send_native_handle(hndl_or_null, meta_blob, err_code);
108}
109
111{
112 return impl()->send_blob(blob, err_code);
113}
114
116{
117 return impl()->end_sending();
118}
119
121{
122 return impl()->auto_ping(period);
123}
124
126{
127 return impl()->blob_send_stats();
128}
129
131{
132 impl()->blob_send_stats_reset();
133}
134
136{
137 return impl()->native_handle_send_stats();
138}
139
141{
142 impl()->native_handle_send_stats_reset();
143}
144
146{
147 return impl()->receive_meta_blob_max_size();
148}
149
151{
152 return impl()->receive_blob_max_size();
153}
154
156{
157 return impl()->idle_timer_run(timeout);
158}
159
161{
162 return impl()->blob_receive_stats();
163}
164
166{
167 impl()->blob_receive_stats_reset();
168}
169
171{
172 return impl()->native_handle_receive_stats();
173}
174
176{
177 impl()->native_handle_receive_stats_reset();
178}
179
180// `friend`ship needed for this "non-method method":
181
182std::ostream& operator<<(std::ostream& os, const Native_socket_stream& val)
183{
184 return os << *(val.impl());
185}
186
187} // namespace ipc::transport
Internal, non-movable pImpl-lite implementation of Native_socket_stream class.
Implements both Native_handle_sender and Native_handle_receiver concepts by using a Unix domain socke...
Blob_rcv_stats native_handle_receive_stats() const
Returns the same stats as blob_receive_stats(): since this transport uses a single Unix-domain-socket...
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.
~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 sync_connect(const Shared_name &absolute_name, Error_code *err_code=nullptr)
To be invoked in NULL state only, it synchronously and non-blockingly attempts to connect to an oppos...
Blob_snd_stats native_handle_send_stats() const
Returns the same stats as blob_send_stats(): since this transport uses a single Unix-domain-socket pi...
static const Shared_name & S_RESOURCE_TYPE_ID
Implements concept API.
void native_handle_receive_stats_reset()
Equivalent to blob_receive_stats_reset(); see blob_receive_stats() rationale.
bool idle_timer_run(util::Fine_duration timeout=boost::chrono::seconds{5})
Implements Native_handle_receiver, Blob_receiver API per contract.
bool send_native_handle(Native_handle hndl_or_null, const util::Blob_const &meta_blob, Error_code *err_code=nullptr)
Implements Native_handle_sender API per contract.
void blob_receive_stats_reset()
Implements Blob_receiver API per contract.
size_t send_meta_blob_max_size() const
Implements Native_handle_sender API per contract.
void blob_send_stats_reset()
Implements Blob_sender API per contract.
bool auto_ping(util::Fine_duration period=boost::chrono::seconds{2})
Implements Native_handle_sender, Blob_sender API per contract.
void native_handle_send_stats_reset()
Equivalent to blob_send_stats_reset(); see blob_send_stats() rationale.
const util::Process_credentials & remote_peer_process_credentials(Error_code *err_code=nullptr) const
OS-reported process credential (PID, etc.) info about the other connected peer's process,...
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-lite 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.
Blob_snd_stats blob_send_stats() const
Implements Blob_sender API per contract.
size_t receive_blob_max_size() const
Implements Blob_receiver API per contract.
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...
bool send_blob(const util::Blob_const &blob, Error_code *err_code=nullptr)
Implements Blob_sender API per contract.
Blob_rcv_stats blob_receive_stats() const
Implements Blob_receiver API per contract.
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...
std::ostream & operator<<(std::ostream &os, const Generic_msg_batch_in< Msg_resource_t, NO_HNDLS > &val)
Prints string representation of the given Generic_msg_batch_in to the given ostream.
Definition: batch.hpp:431
util::Shared_name Shared_name
Convenience alias for the commonly used type util::Shared_name.
flow::Fine_duration Fine_duration
Short-hand for Flow's Fine_duration.
Definition: util_fwd.hpp:114
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:131
flow::util::String_view String_view
Short-hand for Flow's String_view.
Definition: util_fwd.hpp:112
flow::Error_code Error_code
Short-hand for flow::Error_code which is very common.
Definition: common.hpp:313
Cumulative stats from the incoming-direction (receive) pipe of a Blob_receiver or Native_handle_recei...
Cumulative stats from the outgoing-direction (send) pipe of a Blob_sender or Native_handle_sender con...
A monolayer-thin wrapper around a native handle, a/k/a descriptor a/k/a FD.