• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ASIO_GENERIC_STREAM_PROTOCOL_HPP
12 #define ASIO_GENERIC_STREAM_PROTOCOL_HPP
13 
14 
15 #include "asio/detail/config.hpp"
16 
17 #include <typeinfo>
18 #include "asio/basic_socket_iostream.hpp"
19 #include "asio/basic_stream_socket.hpp"
20 #include "asio/detail/socket_types.hpp"
21 #include "asio/detail/throw_exception.hpp"
22 #include "asio/generic/basic_endpoint.hpp"
23 
24 #include "asio/detail/push_options.hpp"
25 
26 namespace asio {
27 namespace generic {
28 
29 /// Encapsulates the flags needed for a generic stream-oriented socket.
30 /**
31  * The asio::generic::stream_protocol class contains flags necessary for
32  * stream-oriented sockets of any address family and protocol.
33  *
34  * @par Examples
35  * Constructing using a native address family and socket protocol:
36  * @code stream_protocol p(AF_INET, IPPROTO_TCP); @endcode
37  * Constructing from a specific protocol type:
38  * @code stream_protocol p(asio::ip::tcp::v4()); @endcode
39  *
40  * @par Thread Safety
41  * @e Distinct @e objects: Safe.@n
42  * @e Shared @e objects: Safe.
43  *
44  * @par Concepts:
45  * Protocol.
46  */
47 class stream_protocol
48 {
49 public:
50   /// Construct a protocol object for a specific address family and protocol.
stream_protocol(int address_family,int socket_protocol)51   stream_protocol(int address_family, int socket_protocol)
52     : family_(address_family),
53       protocol_(socket_protocol)
54   {
55   }
56 
57   /// Construct a generic protocol object from a specific protocol.
58   /**
59    * @throws @c bad_cast Thrown if the source protocol is not stream-oriented.
60    */
61   template <typename Protocol>
stream_protocol(const Protocol & source_protocol)62   stream_protocol(const Protocol& source_protocol)
63     : family_(source_protocol.family()),
64       protocol_(source_protocol.protocol())
65   {
66     if (source_protocol.type() != type())
67     {
68       std::bad_cast ex;
69       asio::detail::throw_exception(ex);
70     }
71   }
72 
73   /// Obtain an identifier for the type of the protocol.
type() const74   int type() const
75   {
76     return ASIO_OS_DEF(SOCK_STREAM);
77   }
78 
79   /// Obtain an identifier for the protocol.
protocol() const80   int protocol() const
81   {
82     return protocol_;
83   }
84 
85   /// Obtain an identifier for the protocol family.
family() const86   int family() const
87   {
88     return family_;
89   }
90 
91   /// Compare two protocols for equality.
operator ==(const stream_protocol & p1,const stream_protocol & p2)92   friend bool operator==(const stream_protocol& p1, const stream_protocol& p2)
93   {
94     return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_;
95   }
96 
97   /// Compare two protocols for inequality.
operator !=(const stream_protocol & p1,const stream_protocol & p2)98   friend bool operator!=(const stream_protocol& p1, const stream_protocol& p2)
99   {
100     return !(p1 == p2);
101   }
102 
103   /// The type of an endpoint.
104   typedef basic_endpoint<stream_protocol> endpoint;
105 
106   /// The generic socket type.
107   typedef basic_stream_socket<stream_protocol> socket;
108 
109 
110 private:
111   int family_;
112   int protocol_;
113 };
114 
115 } // namespace generic
116 } // namespace asio
117 
118 #include "asio/detail/pop_options.hpp"
119 
120 #endif // ASIO_GENERIC_STREAM_PROTOCOL_HPP
121