src/corosio/src/local_datagram_socket.cpp

100.0% Lines (65/0/65) 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 249x local_datagram_socket::~local_datagram_socket()
24 {
25 249x close();
26 249x }
27
28 221x local_datagram_socket::local_datagram_socket(capy::execution_context& ctx)
29 221x : io_object(create_handle<detail::local_datagram_service>(ctx))
30 {
31 221x }
32
33 std::error_code
34 82x local_datagram_socket::open(local_datagram proto) noexcept
35 {
36 82x if (is_open())
37 5x return {};
38 77x return open_for_family(proto.family(), proto.type(), proto.protocol());
39 }
40
41 std::error_code
42 77x local_datagram_socket::open_for_family(int family, int type, int protocol) noexcept
43 {
44 77x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
45 77x std::error_code ec = svc.open_socket(
46 77x static_cast<local_datagram_socket::implementation&>(*h_.get()),
47 family, type, protocol);
48 77x return ec;
49 }
50
51 void
52 263x local_datagram_socket::close() noexcept
53 {
54 263x if (!is_open())
55 62x return;
56 201x h_.service().close(h_);
57 }
58
59 std::error_code
60 64x local_datagram_socket::bind(corosio::local_endpoint ep) noexcept
61 {
62 64x if (!is_open())
63 2x return make_error_code(std::errc::bad_file_descriptor);
64 62x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
65 62x return svc.bind_socket(
66 62x static_cast<local_datagram_socket::implementation&>(*h_.get()),
67 62x 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 134x local_datagram_socket::assign(native_handle_type fd) noexcept
88 {
89 134x auto& svc = static_cast<detail::local_datagram_service&>(h_.service());
90 134x std::error_code ec = svc.assign_socket(
91 134x static_cast<local_datagram_socket::implementation&>(*h_.get()), fd);
92 134x return ec;
93 }
94
95 native_handle_type
96 17x local_datagram_socket::native_handle() const noexcept
97 {
98 17x if (!is_open())
99 2x return -1;
100 15x return get().native_handle();
101 }
102
103 native_handle_type
104 4x local_datagram_socket::release()
105 {
106 4x 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 2x 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),
124 "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