Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
asio_waitable_native_hndl.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
20#include "ipc/common.hpp"
21#include <cassert>
22#include <cstdlib>
23
24namespace ipc::util::sync_io
25{
26
27// Implementations.
28
30 Base(ex, hndl.m_native_handle)
31{
32 /* This might be too late; I do know that ->assign() throws "Bad descriptor" in Linux on .null() (-1) handle.
33 * It (assign()), as of Boost-1.81 anyway, immediately does an epoll_ctl(ADD) on it which yields bad-descriptor
34 * error. */
35 assert(!hndl.null());
36}
37
39 Base(ex)
40{
41 // Yeah.
42}
43
45
47
49{
50 /* boost.asio documentation is slightly ambiguous -- well, terse as usual -- as to whether `Base::~Base()`
51 * (posix::basic_descriptor dtor), which is `protected` by the way, will act as-if .close() was called,
52 * in the way that certainly (e.g.) boost::asio::ip::tcp::socket::~socket() does. Of course we could test it; but
53 * various signs and common sense indicate that it would in fact Native_handle::close() the FD. Even if not,
54 * the following would do no harm though. To be clear: We do *not* want to close it. We exist for watchability
55 * only. */
56 release();
57
58 // Probably any async_wait() would get operation_aborted because of that. They should be ready for that anyway.
59
60 // Now Base::~Base() will do its usual thing, but on a (perfectly valid) as-if-default-cted *this.
61}
62
64{
65 return Base::is_open() ? Native_handle{Base::native_handle()} : Native_handle{};
66}
67
69{
70 assert(!hndl.null()); // See comment in 2+-arg ctor above.
71
72 if (is_open())
73 {
74 release();
75 }
76
77 Error_code err_code_ok;
78 Base::assign(hndl.m_native_handle, err_code_ok);
79
80 if (err_code_ok)
81 {
82 assert(false && "This can fail at least with bad-descriptor error in Linux; but if you followed "
83 "contract (passed valid descriptor), then it will not. Bug? Blowing up.");
84 std::abort();
85 }
86}
87
88} // namespace ipc::util::sync_io
Useful if using the sync_io pattern within a user event loop built on boost.asio (optionally with flo...
~Asio_waitable_native_handle()
Destructor that does not OS-close (in the Native_handle::close() sense) the stored native handle (if ...
Asio_waitable_native_handle(const Base::executor_type &ex, Native_handle hndl)
Construct boost.asio descriptor object such that one can async_wait() events on the given non-....
boost::asio::posix::descriptor Base
Short-hand for our base type; can be used for, e.g., Base::wait_write and Base::wait_read.
void assign(Native_handle hndl)
Loads value to be returned by native_handle().
Native_handle native_handle()
Returns the same Native_handle as passed to original handle-loading ctor or assign(),...
Asio_waitable_native_handle & operator=(Asio_waitable_native_handle &&src)
Move-assign.
Contains common code, as well as important explanatory documentation in the following text,...
Definition: util_fwd.hpp:217
flow::Error_code Error_code
Short-hand for flow::Error_code which is very common.
Definition: common.hpp:313
A monolayer-thin wrapper around a native handle, a/k/a descriptor a/k/a FD.
constexpr bool null() const noexcept
Returns true if and only if m_native_handle equals S_NULL_HANDLE.
handle_t m_native_handle
The native handle (possibly equal to S_NULL_HANDLE), the exact payload of this Native_handle.