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