src/corosio/src/udp_socket.cpp

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