include/boost/corosio/tcp.hpp

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