src/corosio/src/udp_socket.cpp

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