src/corosio/src/udp_socket.cpp

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