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_udp.hpp
 
11 +

 
12 +
    Inline UDP 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/udp.hpp>` (`boost::corosio::udp`).
 
20 +

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

 
24 +
    @see boost::corosio::udp
 
25 +
*/
 
26 +

 
27 +
#ifndef BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
 
28 +
#define BOOST_COROSIO_NATIVE_NATIVE_UDP_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 udp_socket;
 
41 +

 
42 +
} // namespace boost::corosio
 
43 +

 
44 +
namespace boost::corosio {
 
45 +

 
46 +
/** Inline UDP protocol type with platform constants.
 
47 +

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

 
52 +
    @see boost::corosio::udp
 
53 +
*/
 
54 +
class native_udp
 
55 +
{
 
56 +
    bool v6_;
 
57 +
    explicit constexpr native_udp(bool v6) noexcept : v6_(v6) {}
 
58 +

 
59 +
public:
 
60 +
    /// Construct an IPv4 UDP protocol.
 
61 +
    static constexpr native_udp v4() noexcept
 
62 +
    {
 
63 +
        return native_udp(false);
 
64 +
    }
 
65 +

 
66 +
    /// Construct an IPv6 UDP protocol.
 
67 +
    static constexpr native_udp v6() noexcept
 
68 +
    {
 
69 +
        return native_udp(true);
 
70 +
    }
 
71 +

 
72 +
    /// Return true if this is IPv6.
 
73 +
    constexpr bool is_v6() const noexcept
 
74 +
    {
 
75 +
        return v6_;
 
76 +
    }
 
77 +

 
78 +
    /// Return the address family (AF_INET or AF_INET6).
 
79 +
    int family() const noexcept
 
80 +
    {
 
81 +
        return v6_ ? AF_INET6 : AF_INET;
 
82 +
    }
 
83 +

 
84 +
    /// Return the socket type (SOCK_DGRAM).
 
85 +
    static constexpr int type() noexcept
 
86 +
    {
 
87 +
        return SOCK_DGRAM;
 
88 +
    }
 
89 +

 
90 +
    /// Return the IP protocol (IPPROTO_UDP).
 
91 +
    static constexpr int protocol() noexcept
 
92 +
    {
 
93 +
        return IPPROTO_UDP;
 
94 +
    }
 
95 +

 
96 +
    /// The associated socket type.
 
97 +
    using socket = udp_socket;
 
98 +

 
99 +
    friend constexpr bool operator==(native_udp a, native_udp b) noexcept
 
100 +
    {
 
101 +
        return a.v6_ == b.v6_;
 
102 +
    }
 
103 +

 
104 +
    friend constexpr bool operator!=(native_udp a, native_udp b) noexcept
 
105 +
    {
 
106 +
        return a.v6_ != b.v6_;
 
107 +
    }
 
108 +
};
 
109 +

 
110 +
} // namespace boost::corosio
 
111 +

 
112 +
#endif // BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP