src/corosio/src/udp_socket.cpp

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