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   // 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_TCP_HPP 10   #ifndef BOOST_COROSIO_TCP_HPP
11   #define BOOST_COROSIO_TCP_HPP 11   #define BOOST_COROSIO_TCP_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 tcp_socket; 17   class tcp_socket;
18   class tcp_acceptor; 18   class tcp_acceptor;
19   19  
20   /** Encapsulate the TCP protocol for socket creation. 20   /** Encapsulate the TCP protocol for socket creation.
21   21  
22   This class identifies the TCP protocol and its address family 22   This class identifies the TCP protocol and its address family
23   (IPv4 or IPv6). It is used to parameterize socket and acceptor 23   (IPv4 or IPv6). It is used to parameterize socket and acceptor
24   `open()` calls with a self-documenting type. 24   `open()` 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_tcp. 30   those headers, use @ref native_tcp.
31   31  
32   @par Example 32   @par Example
33   @code 33   @code
34   tcp_acceptor acc( ioc ); 34   tcp_acceptor acc( ioc );
35 - acc.open( tcp::v6() ); // IPv6 socket 35 + if ( auto ec = acc.open( tcp::v6() ) ) // IPv6 socket
  36 + return;
36   acc.set_option( socket_option::reuse_address( true ) ); 37   acc.set_option( socket_option::reuse_address( true ) );
37 - acc.bind( endpoint( ipv6_address::any(), 8080 ) ); 38 + if ( auto ec = acc.bind( endpoint( ipv6_address::any(), 8080 ) ) )
38 - acc.listen(); 39 + return;
  40 + if ( auto ec = acc.listen() )
  41 + return;
39   @endcode 42   @endcode
40   43  
41   @see native_tcp, tcp_socket, tcp_acceptor 44   @see native_tcp, tcp_socket, tcp_acceptor
42   */ 45   */
43   class BOOST_COROSIO_DECL tcp 46   class BOOST_COROSIO_DECL tcp
44   { 47   {
45   bool v6_; 48   bool v6_;
HITCBC 46   6222 explicit constexpr tcp(bool v6) noexcept : v6_(v6) {} 49   7517 explicit constexpr tcp(bool v6) noexcept : v6_(v6) {}
47   50  
48   public: 51   public:
49   /// Construct an IPv4 TCP protocol. 52   /// Construct an IPv4 TCP protocol.
HITCBC 50   6170 static constexpr tcp v4() noexcept 53   7465 static constexpr tcp v4() noexcept
51   { 54   {
HITCBC 52   6170 return tcp(false); 55   7465 return tcp(false);
53   } 56   }
54   57  
55   /// Construct an IPv6 TCP protocol. 58   /// Construct an IPv6 TCP protocol.
HITCBC 56   52 static constexpr tcp v6() noexcept 59   52 static constexpr tcp v6() noexcept
57   { 60   {
HITCBC 58   52 return tcp(true); 61   52 return tcp(true);
59   } 62   }
60   63  
61   /// Return true if this is IPv6. 64   /// Return true if this is IPv6.
62   constexpr bool is_v6() const noexcept 65   constexpr bool is_v6() const noexcept
63   { 66   {
64   return v6_; 67   return v6_;
65   } 68   }
66   69  
67   /// Return the address family (AF_INET or AF_INET6). 70   /// Return the address family (AF_INET or AF_INET6).
68   int family() const noexcept; 71   int family() const noexcept;
69   72  
70   /// Return the socket type (SOCK_STREAM). 73   /// Return the socket type (SOCK_STREAM).
71   static int type() noexcept; 74   static int type() noexcept;
72   75  
73   /// Return the IP protocol (IPPROTO_TCP). 76   /// Return the IP protocol (IPPROTO_TCP).
74   static int protocol() noexcept; 77   static int protocol() noexcept;
75   78  
76   /// The socket type to use with this protocol, @ref tcp_socket. 79   /// The socket type to use with this protocol, @ref tcp_socket.
77   using socket = tcp_socket; 80   using socket = tcp_socket;
78   81  
79   /// The acceptor type to use with this protocol, @ref tcp_acceptor. 82   /// The acceptor type to use with this protocol, @ref tcp_acceptor.
80   using acceptor = tcp_acceptor; 83   using acceptor = tcp_acceptor;
81   84  
82   /// Test for equality. 85   /// Test for equality.
HITCBC 83   12 friend constexpr bool operator==(tcp a, tcp b) noexcept 86   12 friend constexpr bool operator==(tcp a, tcp b) noexcept
84   { 87   {
HITCBC 85   12 return a.v6_ == b.v6_; 88   12 return a.v6_ == b.v6_;
86   } 89   }
87   90  
88   /// Test for inequality. 91   /// Test for inequality.
89   friend constexpr bool operator!=(tcp a, tcp b) noexcept 92   friend constexpr bool operator!=(tcp a, tcp b) noexcept
90   { 93   {
91   return a.v6_ != b.v6_; 94   return a.v6_ != b.v6_;
92   } 95   }
93   }; 96   };
94   97  
95   } // namespace boost::corosio 98   } // namespace boost::corosio
96   99  
97   #endif // BOOST_COROSIO_TCP_HPP 100   #endif // BOOST_COROSIO_TCP_HPP