100.00% Lines (5/5) 100.00% Functions (3/3)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
3   // Copyright (c) 2026 Michael Vandeberg 3   // Copyright (c) 2026 Michael Vandeberg
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/cppalliance/corosio 8   // Official repository: https://github.com/cppalliance/corosio
9   // 9   //
10   10  
11   #ifndef BOOST_COROSIO_UDP_HPP 11   #ifndef BOOST_COROSIO_UDP_HPP
12   #define BOOST_COROSIO_UDP_HPP 12   #define BOOST_COROSIO_UDP_HPP
13   13  
14   #include <boost/corosio/detail/config.hpp> 14   #include <boost/corosio/detail/config.hpp>
15   15  
16   namespace boost::corosio { 16   namespace boost::corosio {
17   17  
18   class udp_socket; 18   class udp_socket;
19   19  
20   /** Encapsulate the UDP protocol for socket creation. 20   /** Encapsulate the UDP protocol for socket creation.
21   21  
22   This class identifies the UDP protocol and its address family 22   This class identifies the UDP protocol and its address family
23   (IPv4 or IPv6). It is used to parameterize `udp_socket::open()` 23   (IPv4 or IPv6). It is used to parameterize `udp_socket::open()`
24   calls with a self-documenting type. 24   calls with a self-documenting type.
25   25  
26   The `family()`, `type()`, and `protocol()` members return the 26   The `family()`, `type()`, and `protocol()` members return the
27   three integers passed to the operating system's `socket()` 27   three integers passed to the operating system's `socket()`
28   call. Their values are platform-defined constants taken from 28   call. Their values are platform-defined constants taken from
29   the system socket headers. For an inline variant that includes 29   the system socket headers. For an inline variant that includes
30   those headers, use @ref native_udp. 30   those headers, use @ref native_udp.
31   31  
32   @par Example 32   @par Example
33   @par !example udp 33   @par !example udp
34   34  
35   @see native_udp, udp_socket 35   @see native_udp, udp_socket
36   */ 36   */
37   class BOOST_COROSIO_DECL udp 37   class BOOST_COROSIO_DECL udp
38   { 38   {
39   bool v6_; 39   bool v6_;
HITCBC 40   321 explicit constexpr udp(bool v6) noexcept : v6_(v6) {} 40   321 explicit constexpr udp(bool v6) noexcept : v6_(v6) {}
41   41  
42   public: 42   public:
43   /// Construct an IPv4 UDP protocol. 43   /// Construct an IPv4 UDP protocol.
HITCBC 44   297 static constexpr udp v4() noexcept 44   297 static constexpr udp v4() noexcept
45   { 45   {
HITCBC 46   297 return udp(false); 46   297 return udp(false);
47   } 47   }
48   48  
49   /// Construct an IPv6 UDP protocol. 49   /// Construct an IPv6 UDP protocol.
HITCBC 50   24 static constexpr udp v6() noexcept 50   24 static constexpr udp v6() noexcept
51   { 51   {
HITCBC 52   24 return udp(true); 52   24 return udp(true);
53   } 53   }
54   54  
55   /// Return true if this is IPv6. 55   /// Return true if this is IPv6.
56   constexpr bool is_v6() const noexcept 56   constexpr bool is_v6() const noexcept
57   { 57   {
58   return v6_; 58   return v6_;
59   } 59   }
60   60  
61   /// Return the address family (AF_INET or AF_INET6). 61   /// Return the address family (AF_INET or AF_INET6).
62   int family() const noexcept; 62   int family() const noexcept;
63   63  
64   /// Return the socket type (SOCK_DGRAM). 64   /// Return the socket type (SOCK_DGRAM).
65   static int type() noexcept; 65   static int type() noexcept;
66   66  
67   /// Return the IP protocol (IPPROTO_UDP). 67   /// Return the IP protocol (IPPROTO_UDP).
68   static int protocol() noexcept; 68   static int protocol() noexcept;
69   69  
70   /// The socket type to use with this protocol, @ref udp_socket. 70   /// The socket type to use with this protocol, @ref udp_socket.
71   using socket = udp_socket; 71   using socket = udp_socket;
72   72  
73   /// Test for equality. 73   /// Test for equality.
74   friend constexpr bool operator==(udp a, udp b) noexcept 74   friend constexpr bool operator==(udp a, udp b) noexcept
75   { 75   {
76   return a.v6_ == b.v6_; 76   return a.v6_ == b.v6_;
77   } 77   }
78   78  
79   /// Test for inequality. 79   /// Test for inequality.
80   friend constexpr bool operator!=(udp a, udp b) noexcept 80   friend constexpr bool operator!=(udp a, udp b) noexcept
81   { 81   {
82   return a.v6_ != b.v6_; 82   return a.v6_ != b.v6_;
83   } 83   }
84   }; 84   };
85   85  
86   } // namespace boost::corosio 86   } // namespace boost::corosio
87   87  
88   #endif // BOOST_COROSIO_UDP_HPP 88   #endif // BOOST_COROSIO_UDP_HPP