100.00% Lines (7/7) 100.00% Functions (4/4)
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_TCP_HPP 11   #ifndef BOOST_COROSIO_TCP_HPP
12   #define BOOST_COROSIO_TCP_HPP 12   #define BOOST_COROSIO_TCP_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 tcp_socket; 18   class tcp_socket;
19   class tcp_acceptor; 19   class tcp_acceptor;
20   20  
21   /** Encapsulate the TCP protocol for socket creation. 21   /** Encapsulate the TCP protocol for socket creation.
22   22  
23   This class identifies the TCP protocol and its address family 23   This class identifies the TCP protocol and its address family
24   (IPv4 or IPv6). It is used to parameterize socket and acceptor 24   (IPv4 or IPv6). It is used to parameterize socket and acceptor
25   `open()` calls with a self-documenting type. 25   `open()` calls with a self-documenting type.
26   26  
27   The `family()`, `type()`, and `protocol()` members return the 27   The `family()`, `type()`, and `protocol()` members return the
28   three integers passed to the operating system's `socket()` 28   three integers passed to the operating system's `socket()`
29   call. Their values are platform-defined constants taken from 29   call. Their values are platform-defined constants taken from
30   the system socket headers. For an inline variant that includes 30   the system socket headers. For an inline variant that includes
31   those headers, use @ref native_tcp. 31   those headers, use @ref native_tcp.
32   32  
33   @par Example 33   @par Example
34   @par !example tcp 34   @par !example tcp
35   35  
36   @see native_tcp, tcp_socket, tcp_acceptor 36   @see native_tcp, tcp_socket, tcp_acceptor
37   */ 37   */
38   class BOOST_COROSIO_DECL tcp 38   class BOOST_COROSIO_DECL tcp
39   { 39   {
40   bool v6_; 40   bool v6_;
HITCBC 41   5234 explicit constexpr tcp(bool v6) noexcept : v6_(v6) {} 41   5256 explicit constexpr tcp(bool v6) noexcept : v6_(v6) {}
42   42  
43   public: 43   public:
44   /// Construct an IPv4 TCP protocol. 44   /// Construct an IPv4 TCP protocol.
HITCBC 45   5180 static constexpr tcp v4() noexcept 45   5202 static constexpr tcp v4() noexcept
46   { 46   {
HITCBC 47   5180 return tcp(false); 47   5202 return tcp(false);
48   } 48   }
49   49  
50   /// Construct an IPv6 TCP protocol. 50   /// Construct an IPv6 TCP protocol.
HITCBC 51   54 static constexpr tcp v6() noexcept 51   54 static constexpr tcp v6() noexcept
52   { 52   {
HITCBC 53   54 return tcp(true); 53   54 return tcp(true);
54   } 54   }
55   55  
56   /// Return true if this is IPv6. 56   /// Return true if this is IPv6.
57   constexpr bool is_v6() const noexcept 57   constexpr bool is_v6() const noexcept
58   { 58   {
59   return v6_; 59   return v6_;
60   } 60   }
61   61  
62   /// Return the address family (AF_INET or AF_INET6). 62   /// Return the address family (AF_INET or AF_INET6).
63   int family() const noexcept; 63   int family() const noexcept;
64   64  
65   /// Return the socket type (SOCK_STREAM). 65   /// Return the socket type (SOCK_STREAM).
66   static int type() noexcept; 66   static int type() noexcept;
67   67  
68   /// Return the IP protocol (IPPROTO_TCP). 68   /// Return the IP protocol (IPPROTO_TCP).
69   static int protocol() noexcept; 69   static int protocol() noexcept;
70   70  
71   /// The socket type to use with this protocol, @ref tcp_socket. 71   /// The socket type to use with this protocol, @ref tcp_socket.
72   using socket = tcp_socket; 72   using socket = tcp_socket;
73   73  
74   /// The acceptor type to use with this protocol, @ref tcp_acceptor. 74   /// The acceptor type to use with this protocol, @ref tcp_acceptor.
75   using acceptor = tcp_acceptor; 75   using acceptor = tcp_acceptor;
76   76  
77   /// Test for equality. 77   /// Test for equality.
HITCBC 78   12 friend constexpr bool operator==(tcp a, tcp b) noexcept 78   12 friend constexpr bool operator==(tcp a, tcp b) noexcept
79   { 79   {
HITCBC 80   12 return a.v6_ == b.v6_; 80   12 return a.v6_ == b.v6_;
81   } 81   }
82   82  
83   /// Test for inequality. 83   /// Test for inequality.
84   friend constexpr bool operator!=(tcp a, tcp b) noexcept 84   friend constexpr bool operator!=(tcp a, tcp b) noexcept
85   { 85   {
86   return a.v6_ != b.v6_; 86   return a.v6_ != b.v6_;
87   } 87   }
88   }; 88   };
89   89  
90   } // namespace boost::corosio 90   } // namespace boost::corosio
91   91  
92   #endif // BOOST_COROSIO_TCP_HPP 92   #endif // BOOST_COROSIO_TCP_HPP