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 are
26  
    The `family()`, `type()`, and `protocol()` members are
27  
    implemented in the compiled library to avoid exposing
27  
    implemented in the compiled library to avoid exposing
28  
    platform socket headers. For an inline variant that includes
28  
    platform socket headers. For an inline variant that includes
29  
    platform headers, use @ref native_tcp.
29  
    platform headers, use @ref native_tcp.
30  

30  

31  
    @par Example
31  
    @par Example
32  
    @code
32  
    @code
33  
    tcp_acceptor acc( ioc );
33  
    tcp_acceptor acc( ioc );
34  
    acc.open( tcp::v6() );  // IPv6 socket
34  
    acc.open( tcp::v6() );  // IPv6 socket
35  
    acc.set_option( socket_option::reuse_address( true ) );
35  
    acc.set_option( socket_option::reuse_address( true ) );
36  
    acc.bind( endpoint( ipv6_address::any(), 8080 ) );
36  
    acc.bind( endpoint( ipv6_address::any(), 8080 ) );
37  
    acc.listen();
37  
    acc.listen();
38  
    @endcode
38  
    @endcode
39  

39  

40  
    @see native_tcp, tcp_socket, tcp_acceptor
40  
    @see native_tcp, tcp_socket, tcp_acceptor
41  
*/
41  
*/
42  
class BOOST_COROSIO_DECL tcp
42  
class BOOST_COROSIO_DECL tcp
43  
{
43  
{
44  
    bool v6_;
44  
    bool v6_;
45  
    explicit constexpr tcp( bool v6 ) noexcept : v6_( v6 ) {}
45  
    explicit constexpr tcp( bool v6 ) noexcept : v6_( v6 ) {}
46  

46  

47  
public:
47  
public:
48  
    /// Construct an IPv4 TCP protocol.
48  
    /// Construct an IPv4 TCP protocol.
49  
    static constexpr tcp v4() noexcept { return tcp( false ); }
49  
    static constexpr tcp v4() noexcept { return tcp( false ); }
50  

50  

51  
    /// Construct an IPv6 TCP protocol.
51  
    /// Construct an IPv6 TCP protocol.
52  
    static constexpr tcp v6() noexcept { return tcp( true ); }
52  
    static constexpr tcp v6() noexcept { return tcp( true ); }
53  

53  

54  
    /// Return true if this is IPv6.
54  
    /// Return true if this is IPv6.
55  
    constexpr bool is_v6() const noexcept { return v6_; }
55  
    constexpr bool is_v6() const noexcept { return v6_; }
56  

56  

57  
    /// Return the address family (AF_INET or AF_INET6).
57  
    /// Return the address family (AF_INET or AF_INET6).
58  
    int family() const noexcept;
58  
    int family() const noexcept;
59  

59  

60  
    /// Return the socket type (SOCK_STREAM).
60  
    /// Return the socket type (SOCK_STREAM).
61  
    static int type() noexcept;
61  
    static int type() noexcept;
62  

62  

63  
    /// Return the IP protocol (IPPROTO_TCP).
63  
    /// Return the IP protocol (IPPROTO_TCP).
64  
    static int protocol() noexcept;
64  
    static int protocol() noexcept;
65  

65  

66  
    /// The associated socket type.
66  
    /// The associated socket type.
67  
    using socket = tcp_socket;
67  
    using socket = tcp_socket;
68  

68  

69  
    /// The associated acceptor type.
69  
    /// The associated acceptor type.
70  
    using acceptor = tcp_acceptor;
70  
    using acceptor = tcp_acceptor;
71  

71  

72  
    friend constexpr bool operator==( tcp a, tcp b ) noexcept
72  
    friend constexpr bool operator==( tcp a, tcp b ) noexcept
73  
    {
73  
    {
74  
        return a.v6_ == b.v6_;
74  
        return a.v6_ == b.v6_;
75  
    }
75  
    }
76  

76  

77  
    friend constexpr bool operator!=( tcp a, tcp b ) noexcept
77  
    friend constexpr bool operator!=( tcp a, tcp b ) noexcept
78  
    {
78  
    {
79  
        return a.v6_ != b.v6_;
79  
        return a.v6_ != b.v6_;
80  
    }
80  
    }
81  
};
81  
};
82  

82  

83  
} // namespace boost::corosio
83  
} // namespace boost::corosio
84  

84  

85  
#endif // BOOST_COROSIO_TCP_HPP
85  
#endif // BOOST_COROSIO_TCP_HPP