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 486x udp_socket::~udp_socket()
19 486x {
20 263x close();
21 486x }
22
23 442x udp_socket::udp_socket(capy::execution_context& ctx)
24 221x : io_object(create_handle<detail::udp_service>(ctx))
25 442x {
26 442x }
27
28 std::error_code
29 233x udp_socket::open(udp proto) noexcept
30 {
31
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 231 times.
233x if (is_open())
32 2x return {};
33 231x return open_for_family(proto.family(), proto.type(), proto.protocol());
34 233x }
35
36 std::error_code
37 231x udp_socket::open_for_family(int family, int type, int protocol) noexcept
38 {
39 231x auto& svc = static_cast<detail::udp_service&>(h_.service());
40
2/4
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
462x std::error_code ec = svc.open_datagram_socket(
41 231x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
42 231x protocol);
43 231x return ec;
44 }
45
46 std::error_code
47 18x udp_socket::assign(native_handle_type fd) noexcept
48 {
49 18x auto& svc = static_cast<detail::udp_service&>(h_.service());
50
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
36x std::error_code ec = svc.assign_socket(
51 18x static_cast<udp_socket::implementation&>(*h_.get()), fd);
52 18x return ec;
53 }
54
55 native_handle_type
56 4x udp_socket::release()
57 {
58
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4x if (!is_open())
59 2x detail::throw_system_error(
60 2x make_error_code(std::errc::bad_file_descriptor),
61 "udp_socket::release");
62 2x return get().release_socket();
63 }
64
65 void
66 351x udp_socket::close() noexcept
67 {
68
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 118 times.
351x if (!is_open())
69 118x return;
70
1/2
✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
233x h_.service().close(h_);
71 351x }
72
73 std::error_code
74 139x udp_socket::bind(endpoint ep) noexcept
75 {
76
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 137 times.
139x if (!is_open())
77 2x return make_error_code(std::errc::bad_file_descriptor);
78 137x auto& svc = static_cast<detail::udp_service&>(h_.service());
79
2/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
274x return svc.bind_datagram(
80 137x static_cast<udp_socket::implementation&>(*h_.get()), ep);
81 139x }
82
83 std::error_code
84 4x udp_socket::shutdown(shutdown_type what) noexcept
85 {
86
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4x if (!is_open())
87 2x return make_error_code(std::errc::bad_file_descriptor);
88 2x return get().shutdown(what);
89 4x }
90
91 void
92 15x udp_socket::cancel() noexcept
93 {
94
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15x if (!is_open())
95 2x return;
96 13x get().cancel();
97 15x }
98
99 native_handle_type
100 16x udp_socket::native_handle() const noexcept
101 {
102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16x 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 14x return get().native_handle();
111 16x }
112
113 endpoint
114 90x udp_socket::local_endpoint() const noexcept
115 {
116
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 88 times.
90x if (!is_open())
117 2x return endpoint{};
118 88x return get().local_endpoint();
119 90x }
120
121 endpoint
122 6x udp_socket::remote_endpoint() const noexcept
123 {
124
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6x if (!is_open())
125 2x return endpoint{};
126 4x return get().remote_endpoint();
127 6x }
128
129 } // namespace boost::corosio
130