src/corosio/src/udp_socket.cpp

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