src/corosio/src/udp_socket.cpp

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