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