src/corosio/src/local_datagram_socket.cpp

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