1//
2// generic/stream_protocol.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 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_STREAM_PROTOCOL_HPP
12#define BOOST_ASIO_GENERIC_STREAM_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_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>
26
27#include <boost/asio/detail/push_options.hpp>
28
29namespace boost {
30namespace asio {
31namespace generic {
32
33/// Encapsulates the flags needed for a generic stream-oriented socket.
34/**
35 * The boost::asio::generic::stream_protocol class contains flags necessary for
36 * stream-oriented sockets of any address family and protocol.
37 *
38 * @par Examples
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
43 *
44 * @par Thread Safety
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Safe.
47 *
48 * @par Concepts:
49 * Protocol.
50 */
51class stream_protocol
52{
53public:
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)
58 {
59 }
60
61 /// Construct a generic protocol object from a specific protocol.
62 /**
63 * @throws @c bad_cast Thrown if the source protocol is not stream-oriented.
64 */
65 template <typename Protocol>
66 stream_protocol(const Protocol& source_protocol)
67 : family_(source_protocol.family()),
68 protocol_(source_protocol.protocol())
69 {
70 if (source_protocol.type() != type())
71 {
72 std::bad_cast ex;
73 boost::asio::detail::throw_exception(ex);
74 }
75 }
76
77 /// Obtain an identifier for the type of the protocol.
78 int type() const
79 {
80 return BOOST_ASIO_OS_DEF(SOCK_STREAM);
81 }
82
83 /// Obtain an identifier for the protocol.
84 int protocol() const
85 {
86 return protocol_;
87 }
88
89 /// Obtain an identifier for the protocol family.
90 int family() const
91 {
92 return family_;
93 }
94
95 /// Compare two protocols for equality.
96 friend bool operator==(const stream_protocol& p1, const stream_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 stream_protocol& p1, const stream_protocol& p2)
103 {
104 return !(p1 == p2);
105 }
106
107 /// The type of an endpoint.
108 typedef basic_endpoint<stream_protocol> endpoint;
109
110 /// The generic socket type.
111 typedef basic_stream_socket<stream_protocol> socket;
112
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)
117
118private:
119 int family_;
120 int protocol_;
121};
122
123} // namespace generic
124} // namespace asio
125} // namespace boost
126
127#include <boost/asio/detail/pop_options.hpp>
128
129#endif // BOOST_ASIO_GENERIC_STREAM_PROTOCOL_HPP
130