TLA Line data 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 : if ( auto ec = sock.open( udp::v4() ) )
35 : return;
36 : if ( auto ec = sock.bind( endpoint( ipv4_address::any(), 9000 ) ) )
37 : return;
38 : @endcode
39 :
40 : @see native_udp, udp_socket
41 : */
42 : class BOOST_COROSIO_DECL udp
43 : {
44 : bool v6_;
45 HIT 233 : explicit constexpr udp(bool v6) noexcept : v6_(v6) {}
46 :
47 : public:
48 : /// Construct an IPv4 UDP protocol.
49 209 : static constexpr udp v4() noexcept
50 : {
51 209 : return udp(false);
52 : }
53 :
54 : /// Construct an IPv6 UDP protocol.
55 24 : static constexpr udp v6() noexcept
56 : {
57 24 : return udp(true);
58 : }
59 :
60 : /// Return true if this is IPv6.
61 : constexpr bool is_v6() const noexcept
62 : {
63 : return v6_;
64 : }
65 :
66 : /// Return the address family (AF_INET or AF_INET6).
67 : int family() const noexcept;
68 :
69 : /// Return the socket type (SOCK_DGRAM).
70 : static int type() noexcept;
71 :
72 : /// Return the IP protocol (IPPROTO_UDP).
73 : static int protocol() noexcept;
74 :
75 : /// The socket type to use with this protocol, @ref udp_socket.
76 : using socket = udp_socket;
77 :
78 : /// Test for equality.
79 : friend constexpr bool operator==(udp a, udp b) noexcept
80 : {
81 : return a.v6_ == b.v6_;
82 : }
83 :
84 : /// Test for inequality.
85 : friend constexpr bool operator!=(udp a, udp b) noexcept
86 : {
87 : return a.v6_ != b.v6_;
88 : }
89 : };
90 :
91 : } // namespace boost::corosio
92 :
93 : #endif // BOOST_COROSIO_UDP_HPP
|