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/error.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 341x local_datagram_socket::~local_datagram_socket()
27 {
28 341x close();
29 341x }
30
31 309x local_datagram_socket::local_datagram_socket(capy::execution_context& ctx)
32 309x : io_object(handle(
33 ctx,
34 detail::use_backend_service<
35 detail::local_datagram_service_of,
36 309x detail::local_datagram_service>(ctx)))
37 {
38 309x }
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 359x local_datagram_socket::close() noexcept
61 {
62 359x if (!is_open())
63 134x return;
64 225x 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 150x local_datagram_socket::assign(native_handle_type fd) noexcept
95 {
96 150x if (is_open())
97 6x return make_error_code(error::already_open);
98 144x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
99 144x std::error_code ec = svc.assign_socket(
100 144x static_cast<local_datagram_socket::implementation&>(*h_.get()), fd);
101 144x return ec;
102 }
103
104 native_handle_type
105 23x local_datagram_socket::native_handle() const noexcept
106 {
107 23x if (!is_open())
108 2x return -1;
109 21x return get().native_handle();
110 }
111
112 capy::results<native_handle_type>
113 6x local_datagram_socket::release() noexcept
114 {
115 6x if (!is_open())
116 return {
117 4x make_error_code(std::errc::bad_file_descriptor),
118 2x detail::invalid_native_handle};
119 4x return {std::error_code{}, get().release_socket()};
120 }
121
122 capy::results<std::size_t>
123 9x local_datagram_socket::available() const noexcept
124 {
125 9x if (!is_open())
126 2x return {make_error_code(std::errc::bad_file_descriptor), 0};
127 7x int value = 0;
128 7x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
129 5x return {detail::make_err(errno), 0};
130 2x return {std::error_code{}, static_cast<std::size_t>(value)};
131 }
132
133 local_endpoint
134 4x local_datagram_socket::local_endpoint() const noexcept
135 {
136 4x if (!is_open())
137 2x return corosio::local_endpoint{};
138 2x return get().local_endpoint();
139 }
140
141 local_endpoint
142 4x local_datagram_socket::remote_endpoint() const noexcept
143 {
144 4x if (!is_open())
145 2x return corosio::local_endpoint{};
146 2x return get().remote_endpoint();
147 }
148
149 } // namespace boost::corosio
150
151 #endif // BOOST_COROSIO_POSIX
152