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