src/corosio/src/udp_socket.cpp

100.0% Lines (58 / 58) 100.0% Functions (13 / 13)
udp_socket.cpp
f(x) Functions (13)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #include <boost/corosio/udp_socket.hpp>
12 #include <boost/corosio/error.hpp>
13 #include <boost/corosio/native/detail/endpoint_convert.hpp>
14 #include <boost/corosio/detail/except.hpp>
15 #include <boost/corosio/detail/platform.hpp>
16
17 #include <boost/corosio/detail/udp_service.hpp>
18
19 #include "src/detail/use_backend_service.hpp"
20
21 namespace boost::corosio {
22
23 501x udp_socket::~udp_socket()
24 {
25 501x close();
26 501x }
27
28 432x udp_socket::udp_socket(capy::execution_context& ctx)
29 432x : io_object(handle(
30 ctx,
31 detail::use_backend_service<
32 detail::udp_service_of,
33 432x detail::udp_service>(ctx)))
34 {
35 432x }
36
37 std::error_code
38 450x udp_socket::open(family f) noexcept
39 {
40 450x if (is_open())
41 3x return {};
42 447x return open_for_family(detail::native_family(f), SOCK_DGRAM, IPPROTO_UDP);
43 }
44
45 std::error_code
46 447x udp_socket::open_for_family(int family, int type, int protocol) noexcept
47 {
48 447x auto& svc = static_cast<detail::udp_service&>(h_.service());
49 447x std::error_code ec = svc.open_datagram_socket(
50 447x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
51 protocol);
52 447x return ec;
53 }
54
55 std::error_code
56 36x udp_socket::assign(native_handle_type fd) noexcept
57 {
58 36x if (is_open())
59 15x return make_error_code(error::already_open);
60 21x auto& svc = static_cast<detail::udp_service&>(h_.service());
61 21x std::error_code ec = svc.assign_socket(
62 21x static_cast<udp_socket::implementation&>(*h_.get()), fd);
63 21x return ec;
64 }
65
66 native_handle_type
67 9x udp_socket::release()
68 {
69 9x if (!is_open())
70 3x detail::throw_system_error(
71 3x make_error_code(std::errc::bad_file_descriptor),
72 "udp_socket::release");
73 6x return get().release_socket();
74 }
75
76 void
77 640x udp_socket::close() noexcept
78 {
79 640x if (!is_open())
80 192x return;
81 448x h_.service().close(h_);
82 }
83
84 std::error_code
85 258x udp_socket::bind(endpoint ep) noexcept
86 {
87 258x if (!is_open())
88 3x return make_error_code(std::errc::bad_file_descriptor);
89 255x auto& svc = static_cast<detail::udp_service&>(h_.service());
90 255x return svc.bind_datagram(
91 510x static_cast<udp_socket::implementation&>(*h_.get()), ep);
92 }
93
94 std::error_code
95 11x udp_socket::shutdown(shutdown_type what) noexcept
96 {
97 11x if (!is_open())
98 3x return make_error_code(std::errc::bad_file_descriptor);
99 8x return get().shutdown(what);
100 }
101
102 void
103 56x udp_socket::cancel() noexcept
104 {
105 56x if (!is_open())
106 3x return;
107 53x get().cancel();
108 }
109
110 native_handle_type
111 35x udp_socket::native_handle() const noexcept
112 {
113 35x if (!is_open())
114 {
115 #if BOOST_COROSIO_HAS_IOCP
116 return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
117 #else
118 3x return -1;
119 #endif
120 }
121 32x return get().native_handle();
122 }
123
124 endpoint
125 186x udp_socket::local_endpoint() const noexcept
126 {
127 186x if (!is_open())
128 3x return endpoint{};
129 183x return get().local_endpoint();
130 }
131
132 endpoint
133 12x udp_socket::remote_endpoint() const noexcept
134 {
135 12x if (!is_open())
136 3x return endpoint{};
137 9x return get().remote_endpoint();
138 }
139
140 } // namespace boost::corosio
141