Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:53

0001 //
0002 // generic/datagram_protocol.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP
0012 #define BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #include <typeinfo>
0021 #include <boost/asio/basic_datagram_socket.hpp>
0022 #include <boost/asio/detail/socket_types.hpp>
0023 #include <boost/asio/detail/throw_exception.hpp>
0024 #include <boost/asio/generic/basic_endpoint.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace generic {
0031 
0032 /// Encapsulates the flags needed for a generic datagram-oriented socket.
0033 /**
0034  * The boost::asio::generic::datagram_protocol class contains flags necessary
0035  * for datagram-oriented sockets of any address family and protocol.
0036  *
0037  * @par Examples
0038  * Constructing using a native address family and socket protocol:
0039  * @code datagram_protocol p(AF_INET, IPPROTO_UDP); @endcode
0040  * Constructing from a specific protocol type:
0041  * @code datagram_protocol p(boost::asio::ip::udp::v4()); @endcode
0042  *
0043  * @par Thread Safety
0044  * @e Distinct @e objects: Safe.@n
0045  * @e Shared @e objects: Safe.
0046  *
0047  * @par Concepts:
0048  * Protocol.
0049  */
0050 class datagram_protocol
0051 {
0052 public:
0053   /// Construct a protocol object for a specific address family and protocol.
0054   datagram_protocol(int address_family, int socket_protocol)
0055     : family_(address_family),
0056       protocol_(socket_protocol)
0057   {
0058   }
0059 
0060   /// Construct a generic protocol object from a specific protocol.
0061   /**
0062    * @throws @c bad_cast Thrown if the source protocol is not datagram-oriented.
0063    */
0064   template <typename Protocol>
0065   datagram_protocol(const Protocol& source_protocol)
0066     : family_(source_protocol.family()),
0067       protocol_(source_protocol.protocol())
0068   {
0069     if (source_protocol.type() != type())
0070     {
0071       std::bad_cast ex;
0072       boost::asio::detail::throw_exception(ex);
0073     }
0074   }
0075 
0076   /// Obtain an identifier for the type of the protocol.
0077   int type() const noexcept
0078   {
0079     return BOOST_ASIO_OS_DEF(SOCK_DGRAM);
0080   }
0081 
0082   /// Obtain an identifier for the protocol.
0083   int protocol() const noexcept
0084   {
0085     return protocol_;
0086   }
0087 
0088   /// Obtain an identifier for the protocol family.
0089   int family() const noexcept
0090   {
0091     return family_;
0092   }
0093 
0094   /// Compare two protocols for equality.
0095   friend bool operator==(const datagram_protocol& p1,
0096       const datagram_protocol& p2)
0097   {
0098     return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_;
0099   }
0100 
0101   /// Compare two protocols for inequality.
0102   friend bool operator!=(const datagram_protocol& p1,
0103       const datagram_protocol& p2)
0104   {
0105     return !(p1 == p2);
0106   }
0107 
0108   /// The type of an endpoint.
0109   typedef basic_endpoint<datagram_protocol> endpoint;
0110 
0111   /// The generic socket type.
0112   typedef basic_datagram_socket<datagram_protocol> socket;
0113 
0114 private:
0115   int family_;
0116   int protocol_;
0117 };
0118 
0119 } // namespace generic
0120 } // namespace asio
0121 } // namespace boost
0122 
0123 #include <boost/asio/detail/pop_options.hpp>
0124 
0125 #endif // BOOST_ASIO_GENERIC_DATAGRAM_PROTOCOL_HPP