include/boost/corosio/udp.hpp

100.0% Lines (5/5) 100.0% List of functions (3/3)
udp.hpp
f(x) Functions (3)
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 #ifndef BOOST_COROSIO_UDP_HPP
11 #define BOOST_COROSIO_UDP_HPP
12
13 #include <boost/corosio/detail/config.hpp>
14
15 namespace boost::corosio {
16
17 class udp_socket;
18
19 /** Encapsulate the UDP protocol for socket creation.
20
21 This class identifies the UDP protocol and its address family
22 (IPv4 or IPv6). It is used to parameterize `udp_socket::open()`
23 calls with a self-documenting type.
24
25 The `family()`, `type()`, and `protocol()` members return the
26 three integers passed to the operating system's `socket()`
27 call. Their values are platform-defined constants taken from
28 the system socket headers. For an inline variant that includes
29 those headers, use @ref native_udp.
30
31 @par Example
32 @code
33 udp_socket sock( ioc );
34 sock.open( udp::v4() );
35 sock.bind( endpoint( ipv4_address::any(), 9000 ) );
36 @endcode
37
38 @see native_udp, udp_socket
39 */
40 class BOOST_COROSIO_DECL udp
41 {
42 bool v6_;
43 162x explicit constexpr udp(bool v6) noexcept : v6_(v6) {}
44
45 public:
46 /// Construct an IPv4 UDP protocol.
47 142x static constexpr udp v4() noexcept
48 {
49 142x return udp(false);
50 }
51
52 /// Construct an IPv6 UDP protocol.
53 20x static constexpr udp v6() noexcept
54 {
55 20x return udp(true);
56 }
57
58 /// Return true if this is IPv6.
59 constexpr bool is_v6() const noexcept
60 {
61 return v6_;
62 }
63
64 /// Return the address family (AF_INET or AF_INET6).
65 int family() const noexcept;
66
67 /// Return the socket type (SOCK_DGRAM).
68 static int type() noexcept;
69
70 /// Return the IP protocol (IPPROTO_UDP).
71 static int protocol() noexcept;
72
73 /// The socket type to use with this protocol, @ref udp_socket.
74 using socket = udp_socket;
75
76 /// Test for equality.
77 friend constexpr bool operator==(udp a, udp b) noexcept
78 {
79 return a.v6_ == b.v6_;
80 }
81
82 /// Test for inequality.
83 friend constexpr bool operator!=(udp a, udp b) noexcept
84 {
85 return a.v6_ != b.v6_;
86 }
87 };
88
89 } // namespace boost::corosio
90
91 #endif // BOOST_COROSIO_UDP_HPP
92