Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // generic/stream_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_STREAM_PROTOCOL_HPP
0012 #define BOOST_ASIO_GENERIC_STREAM_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_socket_iostream.hpp>
0022 #include <boost/asio/basic_stream_socket.hpp>
0023 #include <boost/asio/detail/socket_types.hpp>
0024 #include <boost/asio/detail/throw_exception.hpp>
0025 #include <boost/asio/generic/basic_endpoint.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace generic {
0032 
0033 /// Encapsulates the flags needed for a generic stream-oriented socket.
0034 /**
0035  * The boost::asio::generic::stream_protocol class contains flags necessary for
0036  * stream-oriented sockets of any address family and protocol.
0037  *
0038  * @par Examples
0039  * Constructing using a native address family and socket protocol:
0040  * @code stream_protocol p(AF_INET, IPPROTO_TCP); @endcode
0041  * Constructing from a specific protocol type:
0042  * @code stream_protocol p(boost::asio::ip::tcp::v4()); @endcode
0043  *
0044  * @par Thread Safety
0045  * @e Distinct @e objects: Safe.@n
0046  * @e Shared @e objects: Safe.
0047  *
0048  * @par Concepts:
0049  * Protocol.
0050  */
0051 class stream_protocol
0052 {
0053 public:
0054   /// Construct a protocol object for a specific address family and protocol.
0055   stream_protocol(int address_family, int socket_protocol)
0056     : family_(address_family),
0057       protocol_(socket_protocol)
0058   {
0059   }
0060 
0061   /// Construct a generic protocol object from a specific protocol.
0062   /**
0063    * @throws @c bad_cast Thrown if the source protocol is not stream-oriented.
0064    */
0065   template <typename Protocol>
0066   stream_protocol(const Protocol& source_protocol)
0067     : family_(source_protocol.family()),
0068       protocol_(source_protocol.protocol())
0069   {
0070     if (source_protocol.type() != type())
0071     {
0072       std::bad_cast ex;
0073       boost::asio::detail::throw_exception(ex);
0074     }
0075   }
0076 
0077   /// Obtain an identifier for the type of the protocol.
0078   int type() const noexcept
0079   {
0080     return BOOST_ASIO_OS_DEF(SOCK_STREAM);
0081   }
0082 
0083   /// Obtain an identifier for the protocol.
0084   int protocol() const noexcept
0085   {
0086     return protocol_;
0087   }
0088 
0089   /// Obtain an identifier for the protocol family.
0090   int family() const noexcept
0091   {
0092     return family_;
0093   }
0094 
0095   /// Compare two protocols for equality.
0096   friend bool operator==(const stream_protocol& p1, const stream_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 stream_protocol& p1, const stream_protocol& p2)
0103   {
0104     return !(p1 == p2);
0105   }
0106 
0107   /// The type of an endpoint.
0108   typedef basic_endpoint<stream_protocol> endpoint;
0109 
0110   /// The generic socket type.
0111   typedef basic_stream_socket<stream_protocol> socket;
0112 
0113 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0114   /// The generic socket iostream type.
0115   typedef basic_socket_iostream<stream_protocol> iostream;
0116 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0117 
0118 private:
0119   int family_;
0120   int protocol_;
0121 };
0122 
0123 } // namespace generic
0124 } // namespace asio
0125 } // namespace boost
0126 
0127 #include <boost/asio/detail/pop_options.hpp>
0128 
0129 #endif // BOOST_ASIO_GENERIC_STREAM_PROTOCOL_HPP