2 // generic/stream_protocol.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_GENERIC_STREAM_PROTOCOL_HPP
12 #define BOOST_ASIO_GENERIC_STREAM_PROTOCOL_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/basic_socket_iostream.hpp>
22 #include <boost/asio/basic_stream_socket.hpp>
23 #include <boost/asio/detail/socket_types.hpp>
24 #include <boost/asio/detail/throw_exception.hpp>
25 #include <boost/asio/generic/basic_endpoint.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 /// Encapsulates the flags needed for a generic stream-oriented socket.
35 * The boost::asio::generic::stream_protocol class contains flags necessary for
36 * stream-oriented sockets of any address family and protocol.
39 * Constructing using a native address family and socket protocol:
40 * @code stream_protocol p(AF_INET, IPPROTO_TCP); @endcode
41 * Constructing from a specific protocol type:
42 * @code stream_protocol p(boost::asio::ip::tcp::v4()); @endcode
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Safe.
54 /// Construct a protocol object for a specific address family and protocol.
55 stream_protocol(int address_family, int socket_protocol)
56 : family_(address_family),
57 protocol_(socket_protocol)
61 /// Construct a generic protocol object from a specific protocol.
63 * @throws @c bad_cast Thrown if the source protocol is not stream-oriented.
65 template <typename Protocol>
66 stream_protocol(const Protocol& source_protocol)
67 : family_(source_protocol.family()),
68 protocol_(source_protocol.protocol())
70 if (source_protocol.type() != type())
73 boost::asio::detail::throw_exception(ex);
77 /// Obtain an identifier for the type of the protocol.
80 return BOOST_ASIO_OS_DEF(SOCK_STREAM);
83 /// Obtain an identifier for the protocol.
89 /// Obtain an identifier for the protocol family.
95 /// Compare two protocols for equality.
96 friend bool operator==(const stream_protocol& p1, const stream_protocol& p2)
98 return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_;
101 /// Compare two protocols for inequality.
102 friend bool operator!=(const stream_protocol& p1, const stream_protocol& p2)
107 /// The type of an endpoint.
108 typedef basic_endpoint<stream_protocol> endpoint;
110 /// The generic socket type.
111 typedef basic_stream_socket<stream_protocol> socket;
113 #if !defined(BOOST_ASIO_NO_IOSTREAM)
114 /// The generic socket iostream type.
115 typedef basic_socket_iostream<stream_protocol> iostream;
116 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
123 } // namespace generic
127 #include <boost/asio/detail/pop_options.hpp>
129 #endif // BOOST_ASIO_GENERIC_STREAM_PROTOCOL_HPP