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