TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/corosio
8 : //
9 :
10 : #ifndef BOOST_COROSIO_TCP_HPP
11 : #define BOOST_COROSIO_TCP_HPP
12 :
13 : #include <boost/corosio/detail/config.hpp>
14 :
15 : namespace boost::corosio {
16 :
17 : class tcp_socket;
18 : class tcp_acceptor;
19 :
20 : /** Encapsulate the TCP protocol for socket creation.
21 :
22 : This class identifies the TCP protocol and its address family
23 : (IPv4 or IPv6). It is used to parameterize socket and acceptor
24 : `open()` calls with a self-documenting type.
25 :
26 : The `family()`, `type()`, and `protocol()` members are
27 : implemented in the compiled library to avoid exposing
28 : platform socket headers. For an inline variant that includes
29 : platform headers, use @ref native_tcp.
30 :
31 : @par Example
32 : @code
33 : tcp_acceptor acc( ioc );
34 : acc.open( tcp::v6() ); // IPv6 socket
35 : acc.set_option( socket_option::reuse_address( true ) );
36 : acc.bind( endpoint( ipv6_address::any(), 8080 ) );
37 : acc.listen();
38 : @endcode
39 :
40 : @see native_tcp, tcp_socket, tcp_acceptor
41 : */
42 : class BOOST_COROSIO_DECL tcp
43 : {
44 : bool v6_;
45 HIT 8405 : explicit constexpr tcp(bool v6) noexcept : v6_(v6) {}
46 :
47 : public:
48 : /// Construct an IPv4 TCP protocol.
49 8379 : static constexpr tcp v4() noexcept
50 : {
51 8379 : return tcp(false);
52 : }
53 :
54 : /// Construct an IPv6 TCP protocol.
55 26 : static constexpr tcp v6() noexcept
56 : {
57 26 : return tcp(true);
58 : }
59 :
60 : /// Return true if this is IPv6.
61 : constexpr bool is_v6() const noexcept
62 : {
63 : return v6_;
64 : }
65 :
66 : /// Return the address family (AF_INET or AF_INET6).
67 : int family() const noexcept;
68 :
69 : /// Return the socket type (SOCK_STREAM).
70 : static int type() noexcept;
71 :
72 : /// Return the IP protocol (IPPROTO_TCP).
73 : static int protocol() noexcept;
74 :
75 : /// The associated socket type.
76 : using socket = tcp_socket;
77 :
78 : /// The associated acceptor type.
79 : using acceptor = tcp_acceptor;
80 :
81 : friend constexpr bool operator==(tcp a, tcp b) noexcept
82 : {
83 : return a.v6_ == b.v6_;
84 : }
85 :
86 : friend constexpr bool operator!=(tcp a, tcp b) noexcept
87 : {
88 : return a.v6_ != b.v6_;
89 : }
90 : };
91 :
92 : } // namespace boost::corosio
93 :
94 : #endif // BOOST_COROSIO_TCP_HPP
|