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  
/** @file native_tcp.hpp
10  
/** @file native_tcp.hpp
11  

11  

12  
    Inline TCP protocol type using platform-specific constants.
12  
    Inline TCP protocol type using platform-specific constants.
13  
    All methods are `constexpr` or trivially inlined, giving zero
13  
    All methods are `constexpr` or trivially inlined, giving zero
14  
    overhead compared to hand-written socket creation calls.
14  
    overhead compared to hand-written socket creation calls.
15  

15  

16  
    This header includes platform socket headers
16  
    This header includes platform socket headers
17  
    (`<sys/socket.h>`, `<netinet/in.h>`, etc.).
17  
    (`<sys/socket.h>`, `<netinet/in.h>`, etc.).
18  
    For a version that avoids platform includes, use
18  
    For a version that avoids platform includes, use
19  
    `<boost/corosio/tcp.hpp>` (`boost::corosio::tcp`).
19  
    `<boost/corosio/tcp.hpp>` (`boost::corosio::tcp`).
20  

20  

21  
    Both variants satisfy the same protocol-type interface and work
21  
    Both variants satisfy the same protocol-type interface and work
22  
    interchangeably with `tcp_socket::open` / `tcp_acceptor::open`.
22  
    interchangeably with `tcp_socket::open` / `tcp_acceptor::open`.
23  

23  

24  
    @see boost::corosio::tcp
24  
    @see boost::corosio::tcp
25  
*/
25  
*/
26  

26  

27  
#ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP
27  
#ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP
28  
#define BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP
28  
#define BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP
29  

29  

30  
#ifdef _WIN32
30  
#ifdef _WIN32
31  
#include <winsock2.h>
31  
#include <winsock2.h>
32  
#include <ws2tcpip.h>
32  
#include <ws2tcpip.h>
33  
#else
33  
#else
34  
#include <netinet/in.h>
34  
#include <netinet/in.h>
35  
#include <sys/socket.h>
35  
#include <sys/socket.h>
36  
#endif
36  
#endif
37  

37  

38  
namespace boost::corosio {
38  
namespace boost::corosio {
39  

39  

40  
class tcp_socket;
40  
class tcp_socket;
41  
class tcp_acceptor;
41  
class tcp_acceptor;
42  

42  

43  
} // namespace boost::corosio
43  
} // namespace boost::corosio
44  

44  

45  
namespace boost::corosio {
45  
namespace boost::corosio {
46  

46  

47  
/** Inline TCP protocol type with platform constants.
47  
/** Inline TCP protocol type with platform constants.
48  

48  

49  
    Same shape as @ref boost::corosio::tcp but with inline
49  
    Same shape as @ref boost::corosio::tcp but with inline
50  
    `family()`, `type()`, and `protocol()` methods that
50  
    `family()`, `type()`, and `protocol()` methods that
51  
    resolve to compile-time constants.
51  
    resolve to compile-time constants.
52  

52  

53  
    @see boost::corosio::tcp
53  
    @see boost::corosio::tcp
54  
*/
54  
*/
55  
class native_tcp
55  
class native_tcp
56  
{
56  
{
57  
    bool v6_;
57  
    bool v6_;
58  
    explicit constexpr native_tcp( bool v6 ) noexcept : v6_( v6 ) {}
58  
    explicit constexpr native_tcp( bool v6 ) noexcept : v6_( v6 ) {}
59  

59  

60  
public:
60  
public:
61  
    /// Construct an IPv4 TCP protocol.
61  
    /// Construct an IPv4 TCP protocol.
62  
    static constexpr native_tcp v4() noexcept { return native_tcp( false ); }
62  
    static constexpr native_tcp v4() noexcept { return native_tcp( false ); }
63  

63  

64  
    /// Construct an IPv6 TCP protocol.
64  
    /// Construct an IPv6 TCP protocol.
65  
    static constexpr native_tcp v6() noexcept { return native_tcp( true ); }
65  
    static constexpr native_tcp v6() noexcept { return native_tcp( true ); }
66  

66  

67  
    /// Return true if this is IPv6.
67  
    /// Return true if this is IPv6.
68  
    constexpr bool is_v6() const noexcept { return v6_; }
68  
    constexpr bool is_v6() const noexcept { return v6_; }
69  

69  

70  
    /// Return the address family (AF_INET or AF_INET6).
70  
    /// Return the address family (AF_INET or AF_INET6).
71  
    int family() const noexcept
71  
    int family() const noexcept
72  
    {
72  
    {
73  
        return v6_ ? AF_INET6 : AF_INET;
73  
        return v6_ ? AF_INET6 : AF_INET;
74  
    }
74  
    }
75  

75  

76  
    /// Return the socket type (SOCK_STREAM).
76  
    /// Return the socket type (SOCK_STREAM).
77  
    static constexpr int type() noexcept { return SOCK_STREAM; }
77  
    static constexpr int type() noexcept { return SOCK_STREAM; }
78  

78  

79  
    /// Return the IP protocol (IPPROTO_TCP).
79  
    /// Return the IP protocol (IPPROTO_TCP).
80  
    static constexpr int protocol() noexcept { return IPPROTO_TCP; }
80  
    static constexpr int protocol() noexcept { return IPPROTO_TCP; }
81  

81  

82  
    /// The associated socket type.
82  
    /// The associated socket type.
83  
    using socket = tcp_socket;
83  
    using socket = tcp_socket;
84  

84  

85  
    /// The associated acceptor type.
85  
    /// The associated acceptor type.
86  
    using acceptor = tcp_acceptor;
86  
    using acceptor = tcp_acceptor;
87  

87  

88  
    friend constexpr bool operator==( native_tcp a, native_tcp b ) noexcept
88  
    friend constexpr bool operator==( native_tcp a, native_tcp b ) noexcept
89  
    {
89  
    {
90  
        return a.v6_ == b.v6_;
90  
        return a.v6_ == b.v6_;
91  
    }
91  
    }
92  

92  

93  
    friend constexpr bool operator!=( native_tcp a, native_tcp b ) noexcept
93  
    friend constexpr bool operator!=( native_tcp a, native_tcp b ) noexcept
94  
    {
94  
    {
95  
        return a.v6_ != b.v6_;
95  
        return a.v6_ != b.v6_;
96  
    }
96  
    }
97  
};
97  
};
98  

98  

99  
} // namespace boost::corosio
99  
} // namespace boost::corosio
100  

100  

101  
#endif // BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP
101  
#endif // BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP