src/corosio/src/udp_socket.cpp

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