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