src/corosio/src/local_datagram_socket.cpp

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