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/platform.hpp>
15
16 #include <boost/corosio/detail/udp_service.hpp>
17
18 #include "src/detail/use_backend_service.hpp"
19
20 namespace boost::corosio {
21
22 444x udp_socket::~udp_socket()
23 {
24 444x close();
25 444x }
26
27 398x udp_socket::udp_socket(capy::execution_context& ctx)
28 398x : io_object(handle(
29 ctx,
30 detail::use_backend_service<
31 detail::udp_service_of,
32 398x detail::udp_service>(ctx)))
33 {
34 398x }
35
36 std::error_code
37 346x udp_socket::open(family f) noexcept
38 {
39 346x if (is_open())
40 2x return {};
41 344x return open_for_family(detail::native_family(f), SOCK_DGRAM, IPPROTO_UDP);
42 }
43
44 std::error_code
45 344x udp_socket::open_for_family(int family, int type, int protocol) noexcept
46 {
47 344x auto& svc = static_cast<detail::udp_service&>(h_.service());
48 344x std::error_code ec = svc.open_datagram_socket(
49 344x static_cast<udp_socket::implementation&>(*h_.get()), family, type,
50 protocol);
51 344x return ec;
52 }
53
54 std::error_code
55 24x udp_socket::assign(native_handle_type fd) noexcept
56 {
57 24x if (is_open())
58 10x return make_error_code(error::already_open);
59 14x auto& svc = static_cast<detail::udp_service&>(h_.service());
60 14x std::error_code ec = svc.assign_socket(
61 14x static_cast<udp_socket::implementation&>(*h_.get()), fd);
62 14x return ec;
63 }
64
65 capy::results<native_handle_type>
66 6x udp_socket::release() noexcept
67 {
68 6x if (!is_open())
69 return {
70 4x make_error_code(std::errc::bad_file_descriptor),
71 2x detail::invalid_native_handle};
72 4x return {std::error_code{}, get().release_socket()};
73 }
74
75 void
76 538x udp_socket::close() noexcept
77 {
78 538x if (!is_open())
79 193x return;
80 345x h_.service().close(h_);
81 }
82
83 std::error_code
84 193x udp_socket::bind(endpoint ep) noexcept
85 {
86 193x if (!is_open())
87 2x return make_error_code(std::errc::bad_file_descriptor);
88 191x auto& svc = static_cast<detail::udp_service&>(h_.service());
89 191x return svc.bind_datagram(
90 382x static_cast<udp_socket::implementation&>(*h_.get()), ep);
91 }
92
93 std::error_code
94 8x udp_socket::shutdown(shutdown_type what) noexcept
95 {
96 8x if (!is_open())
97 2x return make_error_code(std::errc::bad_file_descriptor);
98 6x return get().shutdown(what);
99 }
100
101 void
102 61x udp_socket::cancel() noexcept
103 {
104 61x if (!is_open())
105 2x return;
106 59x get().cancel();
107 }
108
109 native_handle_type
110 22x udp_socket::native_handle() const noexcept
111 {
112 22x if (!is_open())
113 {
114 #if BOOST_COROSIO_HAS_IOCP
115 return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
116 #else
117 2x return -1;
118 #endif
119 }
120 20x return get().native_handle();
121 }
122
123 endpoint
124 138x udp_socket::local_endpoint() const noexcept
125 {
126 138x if (!is_open())
127 2x return endpoint{};
128 136x return get().local_endpoint();
129 }
130
131 endpoint
132 8x udp_socket::remote_endpoint() const noexcept
133 {
134 8x if (!is_open())
135 2x return endpoint{};
136 6x return get().remote_endpoint();
137 }
138
139 } // namespace boost::corosio
140