0.00% Lines (0/0)
0.00% Functions (0/0)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | 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 | - | /** @file native_tcp.hpp | ||||||
| 11 | - | |||||||
| 12 | - | Inline TCP protocol type using platform-specific constants. | ||||||
| 13 | - | All methods are `constexpr` or trivially inlined, giving zero | ||||||
| 14 | - | overhead compared to hand-written socket creation calls. | ||||||
| 15 | - | |||||||
| 16 | - | This header includes platform socket headers | ||||||
| 17 | - | (`<sys/socket.h>`, `<netinet/in.h>`, etc.). | ||||||
| 18 | - | For a version that avoids platform includes, use | ||||||
| 19 | - | `<boost/corosio/tcp.hpp>` (`boost::corosio::tcp`). | ||||||
| 20 | - | |||||||
| 21 | - | Both variants satisfy the same protocol-type interface and work | ||||||
| 22 | - | interchangeably with `tcp_socket::open` / `tcp_acceptor::open`. | ||||||
| 23 | - | |||||||
| 24 | - | @see boost::corosio::tcp | ||||||
| 25 | - | */ | ||||||
| 26 | - | |||||||
| 27 | - | #ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP | ||||||
| 28 | - | #define BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP | ||||||
| 29 | - | |||||||
| 30 | - | #ifdef _WIN32 | ||||||
| 31 | - | #include <winsock2.h> | ||||||
| 32 | - | #include <ws2tcpip.h> | ||||||
| 33 | - | #else | ||||||
| 34 | - | #include <netinet/in.h> | ||||||
| 35 | - | #include <sys/socket.h> | ||||||
| 36 | - | #endif | ||||||
| 37 | - | |||||||
| 38 | - | namespace boost::corosio { | ||||||
| 39 | - | |||||||
| 40 | - | class tcp_socket; | ||||||
| 41 | - | class tcp_acceptor; | ||||||
| 42 | - | |||||||
| 43 | - | } // namespace boost::corosio | ||||||
| 44 | - | |||||||
| 45 | - | namespace boost::corosio { | ||||||
| 46 | - | |||||||
| 47 | - | /** Inline TCP protocol type with platform constants. | ||||||
| 48 | - | |||||||
| 49 | - | Same shape as @ref boost::corosio::tcp but with inline | ||||||
| 50 | - | `family()`, `type()`, and `protocol()` methods that | ||||||
| 51 | - | resolve to compile-time constants. | ||||||
| 52 | - | |||||||
| 53 | - | @see boost::corosio::tcp | ||||||
| 54 | - | */ | ||||||
| 55 | - | class native_tcp | ||||||
| 56 | - | { | ||||||
| 57 | - | bool v6_; | ||||||
| DCB | 58 | - | 5145 | explicit constexpr native_tcp(bool v6) noexcept : v6_(v6) {} | ||||
| 59 | - | |||||||
| 60 | - | public: | ||||||
| 61 | - | /// Construct an IPv4 TCP protocol. | ||||||
| DCB | 62 | - | 5103 | static constexpr native_tcp v4() noexcept | ||||
| 63 | - | { | ||||||
| DCB | 64 | - | 5103 | return native_tcp(false); | ||||
| 65 | - | } | ||||||
| 66 | - | |||||||
| 67 | - | /// Construct an IPv6 TCP protocol. | ||||||
| DCB | 68 | - | 42 | static constexpr native_tcp v6() noexcept | ||||
| 69 | - | { | ||||||
| DCB | 70 | - | 42 | return native_tcp(true); | ||||
| 71 | - | } | ||||||
| 72 | - | |||||||
| 73 | - | /// Return true if this is IPv6. | ||||||
| 74 | - | constexpr bool is_v6() const noexcept | ||||||
| 75 | - | { | ||||||
| 76 | - | return v6_; | ||||||
| 77 | - | } | ||||||
| 78 | - | |||||||
| 79 | - | /// Return the address family (AF_INET or AF_INET6). | ||||||
| DCB | 80 | - | 5145 | int family() const noexcept | ||||
| 81 | - | { | ||||||
| DCB | 82 | - | 5145 | return v6_ ? AF_INET6 : AF_INET; | ||||
| 83 | - | } | ||||||
| 84 | - | |||||||
| 85 | - | /// Return the socket type (SOCK_STREAM). | ||||||
| DCB | 86 | - | 5145 | static constexpr int type() noexcept | ||||
| 87 | - | { | ||||||
| DCB | 88 | - | 5145 | return SOCK_STREAM; | ||||
| 89 | - | } | ||||||
| 90 | - | |||||||
| 91 | - | /// Return the IP protocol (IPPROTO_TCP). | ||||||
| DCB | 92 | - | 5145 | static constexpr int protocol() noexcept | ||||
| 93 | - | { | ||||||
| DCB | 94 | - | 5145 | return IPPROTO_TCP; | ||||
| 95 | - | } | ||||||
| 96 | - | |||||||
| 97 | - | /// The associated socket type. | ||||||
| 98 | - | using socket = tcp_socket; | ||||||
| 99 | - | |||||||
| 100 | - | /// The associated acceptor type. | ||||||
| 101 | - | using acceptor = tcp_acceptor; | ||||||
| 102 | - | |||||||
| 103 | - | friend constexpr bool operator==(native_tcp a, native_tcp b) noexcept | ||||||
| 104 | - | { | ||||||
| 105 | - | return a.v6_ == b.v6_; | ||||||
| 106 | - | } | ||||||
| 107 | - | |||||||
| 108 | - | friend constexpr bool operator!=(native_tcp a, native_tcp b) noexcept | ||||||
| 109 | - | { | ||||||
| 110 | - | return a.v6_ != b.v6_; | ||||||
| 111 | - | } | ||||||
| 112 | - | }; | ||||||
| 113 | - | |||||||
| 114 | - | } // namespace boost::corosio | ||||||
| 115 | - | |||||||
| 116 | - | #endif // BOOST_COROSIO_NATIVE_NATIVE_TCP_HPP | ||||||