src/corosio/src/udp_socket.cpp

100.0% Lines (66/66) 100.0% List of functions (13/13) 78.1% Branches (25/32)
udp_socket.cpp
f(x) Functions (13)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
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/udp_socket.hpp>
11 #include <boost/corosio/detail/except.hpp>
12 #include <boost/corosio/detail/platform.hpp>
13
14 #include <boost/corosio/detail/udp_service.hpp>
15
16 namespace boost::corosio {
17
18 594x udp_socket::~udp_socket()
19 594x {
20 319x close();
21 594x }
22
23 546x udp_socket::udp_socket(capy::execution_context& ctx)
24 273x : io_object(create_handle<detail::udp_service>(ctx))
25 546x {
26 546x }
27
28 std::error_code
29 287x udp_socket::open(udp proto) noexcept
30 {
31
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 285 times.
287x if (is_open())
32 2x return {};
33 285x return open_for_family(proto.family(), proto.type(), proto.protocol());
34 287x }
35
36 std::error_code
37 285x udp_socket::open_for_family(int family, int type, int protocol) noexcept
38 {
39 285x auto& svc = static_cast<detail::udp_service&>(h_.service());
40
2/4
✓ Branch 0 taken 285 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 285 times.
✗ Branch 3 not taken.
570x std::error_code ec = svc.open_datagram_socket(
41 285x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
42 285x protocol);
43 285x return ec;
44 }
45
46 std::error_code
47 22x udp_socket::assign(native_handle_type fd) noexcept
48 {
49 22x auto& svc = static_cast<detail::udp_service&>(h_.service());
50
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
44x std::error_code ec = svc.assign_socket(
51 22x static_cast<udp_socket::implementation&>(*h_.get()), fd);
52 22x return ec;
53 }
54
55 native_handle_type
56 6x udp_socket::release()
57 {
58
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6x if (!is_open())
59 2x detail::throw_system_error(
60 2x make_error_code(std::errc::bad_file_descriptor),
61 "udp_socket::release");
62 4x return get().release_socket();
63 }
64
65 void
66 411x udp_socket::close() noexcept
67 {
68
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 125 times.
411x if (!is_open())
69 125x return;
70
1/2
✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
286x h_.service().close(h_);
71 411x }
72
73 std::error_code
74 179x udp_socket::bind(endpoint ep) noexcept
75 {
76
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 177 times.
179x if (!is_open())
77 2x return make_error_code(std::errc::bad_file_descriptor);
78 177x auto& svc = static_cast<detail::udp_service&>(h_.service());
79
2/4
✓ Branch 0 taken 177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 177 times.
✗ Branch 3 not taken.
354x return svc.bind_datagram(
80 177x static_cast<udp_socket::implementation&>(*h_.get()), ep);
81 179x }
82
83 std::error_code
84 8x udp_socket::shutdown(shutdown_type what) noexcept
85 {
86
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8x if (!is_open())
87 2x return make_error_code(std::errc::bad_file_descriptor);
88 6x return get().shutdown(what);
89 8x }
90
91 void
92 22x udp_socket::cancel() noexcept
93 {
94
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22x if (!is_open())
95 2x return;
96 20x get().cancel();
97 22x }
98
99 native_handle_type
100 18x udp_socket::native_handle() const noexcept
101 {
102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18x if (!is_open())
103 {
104 #if BOOST_COROSIO_HAS_IOCP
105 return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
106 #else
107 2x return -1;
108 #endif
109 }
110 16x return get().native_handle();
111 18x }
112
113 endpoint
114 124x udp_socket::local_endpoint() const noexcept
115 {
116
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 122 times.
124x if (!is_open())
117 2x return endpoint{};
118 122x return get().local_endpoint();
119 124x }
120
121 endpoint
122 8x udp_socket::remote_endpoint() const noexcept
123 {
124
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8x if (!is_open())
125 2x return endpoint{};
126 6x return get().remote_endpoint();
127 8x }
128
129 } // namespace boost::corosio
130