src/corosio/src/local_datagram_socket.cpp

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