src/corosio/src/udp_socket.cpp

100.0% Lines (58 / 58) 100.0% Branches (22 / 22) 100.0% Functions (13 / 13)
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/platform.hpp>
15
16 #include <boost/corosio/detail/udp_service.hpp>
17
18 #include "src/detail/use_backend_service.hpp"
19
20 namespace boost::corosio {
21
22 187x udp_socket::~udp_socket()
23 {
24 187x close();
25 187x }
26
27 164x udp_socket::udp_socket(capy::execution_context& ctx)
28 164x : io_object(handle(
29 ctx,
30 detail::use_backend_service<
31 detail::udp_service_of,
32
2/2
✓ Branch 2 → 3 taken 164 times.
✓ Branch 3 → 4 taken 164 times.
164x detail::udp_service>(ctx)))
33 {
34 164x }
35
36 std::error_code
37 151x udp_socket::open(family f) noexcept
38 {
39
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 150 times.
151x if (is_open())
40 1x return {};
41 150x return open_for_family(detail::native_family(f), SOCK_DGRAM, IPPROTO_UDP);
42 }
43
44 std::error_code
45 150x udp_socket::open_for_family(int family, int type, int protocol) noexcept
46 {
47 150x auto& svc = static_cast<detail::udp_service&>(h_.service());
48 150x std::error_code ec = svc.open_datagram_socket(
49 150x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
50 protocol);
51 150x return ec;
52 }
53
54 std::error_code
55 32x udp_socket::assign(native_handle_type fd) noexcept
56 {
57
2/2
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 5 taken 26 times.
32x if (is_open())
58 6x return make_error_code(error::already_open);
59 26x auto& svc = static_cast<detail::udp_service&>(h_.service());
60 26x std::error_code ec = svc.assign_socket(
61 26x static_cast<udp_socket::implementation&>(*h_.get()), fd);
62 26x return ec;
63 }
64
65 capy::results<native_handle_type>
66 5x udp_socket::release() noexcept
67 {
68
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 4 times.
5x if (!is_open())
69 return {
70 2x make_error_code(std::errc::bad_file_descriptor),
71 1x detail::invalid_native_handle};
72 4x return {std::error_code{}, get().release_socket()};
73 }
74
75 void
76 233x udp_socket::close() noexcept
77 {
78
2/2
✓ Branch 3 → 4 taken 94 times.
✓ Branch 3 → 5 taken 139 times.
233x if (!is_open())
79 94x return;
80 139x h_.service().close(h_);
81 }
82
83 std::error_code
84 84x udp_socket::bind(endpoint ep) noexcept
85 {
86
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 83 times.
84x if (!is_open())
87 1x return make_error_code(std::errc::bad_file_descriptor);
88 83x auto& svc = static_cast<detail::udp_service&>(h_.service());
89 83x return svc.bind_datagram(
90 166x static_cast<udp_socket::implementation&>(*h_.get()), ep);
91 }
92
93 std::error_code
94 5x udp_socket::shutdown(shutdown_type what) noexcept
95 {
96
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 4 times.
5x if (!is_open())
97 1x return make_error_code(std::errc::bad_file_descriptor);
98 4x return get().shutdown(what);
99 }
100
101 void
102 7x udp_socket::cancel() noexcept
103 {
104
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 6 times.
7x if (!is_open())
105 1x return;
106 6x get().cancel();
107 }
108
109 native_handle_type
110 12x udp_socket::native_handle() const noexcept
111 {
112
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 11 times.
12x if (!is_open())
113 {
114 #if BOOST_COROSIO_HAS_IOCP
115 1x return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
116 #else
117 return -1;
118 #endif
119 }
120 11x return get().native_handle();
121 }
122
123 endpoint
124 57x udp_socket::local_endpoint() const noexcept
125 {
126
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 56 times.
57x if (!is_open())
127 1x return endpoint{};
128 56x return get().local_endpoint();
129 }
130
131 endpoint
132 4x udp_socket::remote_endpoint() const noexcept
133 {
134
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 3 times.
4x if (!is_open())
135 1x return endpoint{};
136 3x return get().remote_endpoint();
137 }
138
139 } // namespace boost::corosio
140