src/corosio/src/udp_socket.cpp

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