1 | // |
2 | // generic/datagram_protocol.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP |
12 | #define BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | |
20 | #include <typeinfo> |
21 | #include <boost/asio/basic_datagram_socket.hpp> |
22 | #include <boost/asio/detail/socket_types.hpp> |
23 | #include <boost/asio/detail/throw_exception.hpp> |
24 | #include <boost/asio/generic/basic_endpoint.hpp> |
25 | |
26 | #include <boost/asio/detail/push_options.hpp> |
27 | |
28 | namespace boost { |
29 | namespace asio { |
30 | namespace generic { |
31 | |
32 | /// Encapsulates the flags needed for a generic datagram-oriented socket. |
33 | /** |
34 | * The boost::asio::generic::datagram_protocol class contains flags necessary |
35 | * for datagram-oriented sockets of any address family and protocol. |
36 | * |
37 | * @par Examples |
38 | * Constructing using a native address family and socket protocol: |
39 | * @code datagram_protocol p(AF_INET, IPPROTO_UDP); @endcode |
40 | * Constructing from a specific protocol type: |
41 | * @code datagram_protocol p(boost::asio::ip::udp::v4()); @endcode |
42 | * |
43 | * @par Thread Safety |
44 | * @e Distinct @e objects: Safe.@n |
45 | * @e Shared @e objects: Safe. |
46 | * |
47 | * @par Concepts: |
48 | * Protocol. |
49 | */ |
50 | class datagram_protocol |
51 | { |
52 | public: |
53 | /// Construct a protocol object for a specific address family and protocol. |
54 | datagram_protocol(int address_family, int socket_protocol) |
55 | : family_(address_family), |
56 | protocol_(socket_protocol) |
57 | { |
58 | } |
59 | |
60 | /// Construct a generic protocol object from a specific protocol. |
61 | /** |
62 | * @throws @c bad_cast Thrown if the source protocol is not datagram-oriented. |
63 | */ |
64 | template <typename Protocol> |
65 | datagram_protocol(const Protocol& source_protocol) |
66 | : family_(source_protocol.family()), |
67 | protocol_(source_protocol.protocol()) |
68 | { |
69 | if (source_protocol.type() != type()) |
70 | { |
71 | std::bad_cast ex; |
72 | boost::asio::detail::throw_exception(e: ex); |
73 | } |
74 | } |
75 | |
76 | /// Obtain an identifier for the type of the protocol. |
77 | int type() const noexcept |
78 | { |
79 | return BOOST_ASIO_OS_DEF(SOCK_DGRAM); |
80 | } |
81 | |
82 | /// Obtain an identifier for the protocol. |
83 | int protocol() const noexcept |
84 | { |
85 | return protocol_; |
86 | } |
87 | |
88 | /// Obtain an identifier for the protocol family. |
89 | int family() const noexcept |
90 | { |
91 | return family_; |
92 | } |
93 | |
94 | /// Compare two protocols for equality. |
95 | friend bool operator==(const datagram_protocol& p1, |
96 | const datagram_protocol& p2) |
97 | { |
98 | return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_; |
99 | } |
100 | |
101 | /// Compare two protocols for inequality. |
102 | friend bool operator!=(const datagram_protocol& p1, |
103 | const datagram_protocol& p2) |
104 | { |
105 | return !(p1 == p2); |
106 | } |
107 | |
108 | /// The type of an endpoint. |
109 | typedef basic_endpoint<datagram_protocol> endpoint; |
110 | |
111 | /// The generic socket type. |
112 | typedef basic_datagram_socket<datagram_protocol> socket; |
113 | |
114 | private: |
115 | int family_; |
116 | int protocol_; |
117 | }; |
118 | |
119 | } // namespace generic |
120 | } // namespace asio |
121 | } // namespace boost |
122 | |
123 | #include <boost/asio/detail/pop_options.hpp> |
124 | |
125 | #endif // BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP |
126 | |