src/corosio/src/udp_socket.cpp

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