src/corosio/src/udp_socket.cpp

100.0% Lines (58/58) 100.0% List of functions (13/13) 100.0% Branches (22/22)
udp_socket.cpp
f(x) Functions (13)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #include <boost/corosio/udp_socket.hpp>
12 #include <boost/corosio/error.hpp>
13 #include <boost/corosio/native/detail/endpoint_convert.hpp>
14 #include <boost/corosio/detail/except.hpp>
15 #include <boost/corosio/detail/platform.hpp>
16
17 #include <boost/corosio/detail/udp_service.hpp>
18
19 #include "src/detail/use_backend_service.hpp"
20
21 namespace boost::corosio {
22
23 182x udp_socket::~udp_socket()
24 {
25 182x close();
26 182x }
27
28 159x udp_socket::udp_socket(capy::execution_context& ctx)
29 159x : io_object(handle(
30 ctx,
31 detail::use_backend_service<
32 detail::udp_service_of,
33
2/2
✓ Branch 2 → 3 taken 159 times.
✓ Branch 3 → 4 taken 159 times.
159x detail::udp_service>(ctx)))
34 {
35 159x }
36
37 std::error_code
38 147x udp_socket::open(family f) noexcept
39 {
40
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 146 times.
147x if (is_open())
41 1x return {};
42 146x return open_for_family(detail::native_family(f), SOCK_DGRAM, IPPROTO_UDP);
43 }
44
45 std::error_code
46 146x udp_socket::open_for_family(int family, int type, int protocol) noexcept
47 {
48 146x auto& svc = static_cast<detail::udp_service&>(h_.service());
49 146x std::error_code ec = svc.open_datagram_socket(
50 146x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
51 protocol);
52 146x return ec;
53 }
54
55 std::error_code
56 31x udp_socket::assign(native_handle_type fd) noexcept
57 {
58
2/2
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 5 taken 25 times.
31x if (is_open())
59 6x return make_error_code(error::already_open);
60 25x auto& svc = static_cast<detail::udp_service&>(h_.service());
61 25x std::error_code ec = svc.assign_socket(
62 25x static_cast<udp_socket::implementation&>(*h_.get()), fd);
63 25x return ec;
64 }
65
66 native_handle_type
67 4x udp_socket::release()
68 {
69
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 3 times.
4x if (!is_open())
70 1x detail::throw_system_error(
71 1x make_error_code(std::errc::bad_file_descriptor),
72 "udp_socket::release");
73 3x return get().release_socket();
74 }
75
76 void
77 227x udp_socket::close() noexcept
78 {
79
2/2
✓ Branch 3 → 4 taken 91 times.
✓ Branch 3 → 5 taken 136 times.
227x if (!is_open())
80 91x return;
81 136x h_.service().close(h_);
82 }
83
84 std::error_code
85 80x udp_socket::bind(endpoint ep) noexcept
86 {
87
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 79 times.
80x if (!is_open())
88 1x return make_error_code(std::errc::bad_file_descriptor);
89 79x auto& svc = static_cast<detail::udp_service&>(h_.service());
90 79x return svc.bind_datagram(
91 158x static_cast<udp_socket::implementation&>(*h_.get()), ep);
92 }
93
94 std::error_code
95 5x udp_socket::shutdown(shutdown_type what) noexcept
96 {
97
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 4 times.
5x if (!is_open())
98 1x return make_error_code(std::errc::bad_file_descriptor);
99 4x return get().shutdown(what);
100 }
101
102 void
103 7x udp_socket::cancel() noexcept
104 {
105
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 6 times.
7x if (!is_open())
106 1x return;
107 6x get().cancel();
108 }
109
110 native_handle_type
111 12x udp_socket::native_handle() const noexcept
112 {
113
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 11 times.
12x if (!is_open())
114 {
115 #if BOOST_COROSIO_HAS_IOCP
116 1x return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
117 #else
118 return -1;
119 #endif
120 }
121 11x return get().native_handle();
122 }
123
124 endpoint
125 55x udp_socket::local_endpoint() const noexcept
126 {
127
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 54 times.
55x if (!is_open())
128 1x return endpoint{};
129 54x return get().local_endpoint();
130 }
131
132 endpoint
133 4x udp_socket::remote_endpoint() const noexcept
134 {
135
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 3 times.
4x if (!is_open())
136 1x return endpoint{};
137 3x return get().remote_endpoint();
138 }
139
140 } // namespace boost::corosio
141