src/corosio/src/local_datagram_socket.cpp

100.0% Lines (64 / 64) 100.0% Functions (14 / 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 #include <sys/socket.h>
21
22 namespace boost::corosio {
23
24 271x local_datagram_socket::~local_datagram_socket()
25 {
26 271x close();
27 271x }
28
29 239x local_datagram_socket::local_datagram_socket(capy::execution_context& ctx)
30 239x : io_object(create_handle<detail::local_datagram_service>(ctx))
31 {
32 239x }
33
34 std::error_code
35 96x local_datagram_socket::open() noexcept
36 {
37 96x if (is_open())
38 5x return {};
39 91x return open_for_family(AF_UNIX, SOCK_DGRAM, 0);
40 }
41
42 std::error_code
43 91x local_datagram_socket::open_for_family(
44 int family, int type, int protocol) noexcept
45 {
46 91x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
47 91x std::error_code ec = svc.open_socket(
48 91x static_cast<local_datagram_socket::implementation&>(*h_.get()), family,
49 type, protocol);
50 91x return ec;
51 }
52
53 void
54 285x local_datagram_socket::close() noexcept
55 {
56 285x if (!is_open())
57 64x return;
58 221x h_.service().close(h_);
59 }
60
61 std::error_code
62 70x local_datagram_socket::bind(corosio::local_endpoint ep) noexcept
63 {
64 70x if (!is_open())
65 2x return make_error_code(std::errc::bad_file_descriptor);
66 68x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
67 68x return svc.bind_socket(
68 136x static_cast<local_datagram_socket::implementation&>(*h_.get()), ep);
69 }
70
71 void
72 6x local_datagram_socket::cancel() noexcept
73 {
74 6x if (!is_open())
75 2x return;
76 4x get().cancel();
77 }
78
79 std::error_code
80 10x local_datagram_socket::shutdown(shutdown_type what) noexcept
81 {
82 10x if (!is_open())
83 2x return make_error_code(std::errc::bad_file_descriptor);
84 8x return get().shutdown(what);
85 }
86
87 std::error_code
88 144x local_datagram_socket::assign(native_handle_type fd) noexcept
89 {
90 144x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
91 144x std::error_code ec = svc.assign_socket(
92 144x static_cast<local_datagram_socket::implementation&>(*h_.get()), fd);
93 144x return ec;
94 }
95
96 native_handle_type
97 19x local_datagram_socket::native_handle() const noexcept
98 {
99 19x if (!is_open())
100 2x return -1;
101 17x return get().native_handle();
102 }
103
104 native_handle_type
105 6x local_datagram_socket::release()
106 {
107 6x if (!is_open())
108 2x detail::throw_system_error(
109 2x make_error_code(std::errc::bad_file_descriptor),
110 "local_datagram_socket::release");
111 4x return get().release_socket();
112 }
113
114 std::size_t
115 9x local_datagram_socket::available() const
116 {
117 9x if (!is_open())
118 2x detail::throw_system_error(
119 4x make_error_code(std::errc::bad_file_descriptor),
120 "local_datagram_socket::available");
121 7x int value = 0;
122 7x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
123 5x detail::throw_system_error(
124 10x detail::make_err(errno), "local_datagram_socket::available");
125 2x return static_cast<std::size_t>(value);
126 }
127
128 local_endpoint
129 4x local_datagram_socket::local_endpoint() const noexcept
130 {
131 4x if (!is_open())
132 2x return corosio::local_endpoint{};
133 2x return get().local_endpoint();
134 }
135
136 local_endpoint
137 4x local_datagram_socket::remote_endpoint() const noexcept
138 {
139 4x if (!is_open())
140 2x return corosio::local_endpoint{};
141 2x return get().remote_endpoint();
142 }
143
144 } // namespace boost::corosio
145
146 #endif // BOOST_COROSIO_POSIX
147