src/corosio/src/local_datagram_socket.cpp

100.0% Lines (64/0/64) 100.0% List of functions (14/0/14)
local_datagram_socket.cpp
f(x) Functions (14)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/detail/platform.hpp>
11
12 #if BOOST_COROSIO_POSIX
13
14 #include <boost/corosio/local_datagram_socket.hpp>
15 #include <boost/corosio/detail/except.hpp>
16 #include <boost/corosio/detail/local_datagram_service.hpp>
17 #include <boost/corosio/native/detail/make_err.hpp>
18
19 #include <sys/ioctl.h>
20
21 namespace boost::corosio {
22
23 271x local_datagram_socket::~local_datagram_socket()
24 {
25 271x close();
26 271x }
27
28 239x local_datagram_socket::local_datagram_socket(capy::execution_context& ctx)
29 239x : io_object(create_handle<detail::local_datagram_service>(ctx))
30 {
31 239x }
32
33 std::error_code
34 96x local_datagram_socket::open(local_datagram proto) noexcept
35 {
36 96x if (is_open())
37 5x return {};
38 91x return open_for_family(proto.family(), proto.type(), proto.protocol());
39 }
40
41 std::error_code
42 91x local_datagram_socket::open_for_family(
43 int family, int type, int protocol) noexcept
44 {
45 91x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
46 91x std::error_code ec = svc.open_socket(
47 91x static_cast<local_datagram_socket::implementation&>(*h_.get()), family,
48 type, protocol);
49 91x return ec;
50 }
51
52 void
53 285x local_datagram_socket::close() noexcept
54 {
55 285x if (!is_open())
56 64x return;
57 221x h_.service().close(h_);
58 }
59
60 std::error_code
61 70x local_datagram_socket::bind(corosio::local_endpoint ep) noexcept
62 {
63 70x if (!is_open())
64 2x return make_error_code(std::errc::bad_file_descriptor);
65 68x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
66 68x return svc.bind_socket(
67 136x static_cast<local_datagram_socket::implementation&>(*h_.get()), ep);
68 }
69
70 void
71 6x local_datagram_socket::cancel() noexcept
72 {
73 6x if (!is_open())
74 2x return;
75 4x get().cancel();
76 }
77
78 std::error_code
79 10x local_datagram_socket::shutdown(shutdown_type what) noexcept
80 {
81 10x if (!is_open())
82 2x return make_error_code(std::errc::bad_file_descriptor);
83 8x return get().shutdown(what);
84 }
85
86 std::error_code
87 144x local_datagram_socket::assign(native_handle_type fd) noexcept
88 {
89 144x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
90 144x std::error_code ec = svc.assign_socket(
91 144x static_cast<local_datagram_socket::implementation&>(*h_.get()), fd);
92 144x return ec;
93 }
94
95 native_handle_type
96 19x local_datagram_socket::native_handle() const noexcept
97 {
98 19x if (!is_open())
99 2x return -1;
100 17x return get().native_handle();
101 }
102
103 native_handle_type
104 6x local_datagram_socket::release()
105 {
106 6x if (!is_open())
107 2x detail::throw_system_error(
108 2x make_error_code(std::errc::bad_file_descriptor),
109 "local_datagram_socket::release");
110 4x return get().release_socket();
111 }
112
113 std::size_t
114 9x local_datagram_socket::available() const
115 {
116 9x if (!is_open())
117 2x detail::throw_system_error(
118 4x make_error_code(std::errc::bad_file_descriptor),
119 "local_datagram_socket::available");
120 7x int value = 0;
121 7x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
122 5x detail::throw_system_error(
123 10x detail::make_err(errno), "local_datagram_socket::available");
124 2x return static_cast<std::size_t>(value);
125 }
126
127 local_endpoint
128 4x local_datagram_socket::local_endpoint() const noexcept
129 {
130 4x if (!is_open())
131 2x return corosio::local_endpoint{};
132 2x return get().local_endpoint();
133 }
134
135 local_endpoint
136 4x local_datagram_socket::remote_endpoint() const noexcept
137 {
138 4x if (!is_open())
139 2x return corosio::local_endpoint{};
140 2x return get().remote_endpoint();
141 }
142
143 } // namespace boost::corosio
144
145 #endif // BOOST_COROSIO_POSIX
146