src/corosio/src/udp_socket.cpp

97.7% Lines (42/43) 100.0% List of functions (10/10) 94.4% Branches (17/18)
udp_socket.cpp
f(x) Functions (10)
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 93x udp_socket::~udp_socket()
19 {
20 93x close();
21 93x }
22
23 73x udp_socket::udp_socket(capy::execution_context& ctx)
24
1/1
✓ Branch 2 → 3 taken 73 times.
73x : io_object(create_handle<detail::udp_service>(ctx))
25 {
26 73x }
27
28 void
29 81x udp_socket::open(udp proto)
30 {
31
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 80 times.
81x if (is_open())
32 1x return;
33 80x open_for_family(proto.family(), proto.type(), proto.protocol());
34 }
35
36 void
37 80x udp_socket::open_for_family(int family, int type, int protocol)
38 {
39 80x auto& svc = static_cast<detail::udp_service&>(h_.service());
40
1/1
✓ Branch 4 → 5 taken 80 times.
80x std::error_code ec = svc.open_datagram_socket(
41 80x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
42 protocol);
43
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 80 times.
80x if (ec)
44 detail::throw_system_error(ec, "udp_socket::open");
45 80x }
46
47 void
48 130x udp_socket::close()
49 {
50
2/2
✓ Branch 3 → 4 taken 50 times.
✓ Branch 3 → 5 taken 80 times.
130x if (!is_open())
51 50x return;
52 80x h_.service().close(h_);
53 }
54
55 std::error_code
56 47x udp_socket::bind(endpoint ep)
57 {
58
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 46 times.
47x if (!is_open())
59 1x detail::throw_logic_error("bind: socket not open");
60 46x auto& svc = static_cast<detail::udp_service&>(h_.service());
61 46x return svc.bind_datagram(
62 92x static_cast<udp_socket::implementation&>(*h_.get()), ep);
63 }
64
65 void
66 6x udp_socket::cancel()
67 {
68
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 5 times.
6x if (!is_open())
69 1x return;
70 5x get().cancel();
71 }
72
73 native_handle_type
74 3x udp_socket::native_handle() const noexcept
75 {
76
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2 times.
3x if (!is_open())
77 1x return -1;
78 2x return get().native_handle();
79 }
80
81 endpoint
82 27x udp_socket::local_endpoint() const noexcept
83 {
84
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 26 times.
27x if (!is_open())
85 1x return endpoint{};
86 26x return get().local_endpoint();
87 }
88
89 endpoint
90 3x udp_socket::remote_endpoint() const noexcept
91 {
92
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2 times.
3x if (!is_open())
93 1x return endpoint{};
94 2x return get().remote_endpoint();
95 }
96
97 } // namespace boost::corosio
98