1 // 2 // generic/seq_packet_protocol.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 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_SEQ_PACKET_PROTOCOL_HPP 12 #define BOOST_ASIO_GENERIC_SEQ_PACKET_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_seq_packet_socket.hpp> 22 #include <boost/asio/detail/socket_types.hpp> 23 #include <boost/asio/detail/throw_exception.hpp> 24 #include <boost/asio/generic/basic_endpoint.hpp> 25 26 #include <boost/asio/detail/push_options.hpp> 27 28 namespace boost { 29 namespace asio { 30 namespace generic { 31 32 /// Encapsulates the flags needed for a generic sequenced packet socket. 33 /** 34 * The boost::asio::generic::seq_packet_protocol class contains flags necessary 35 * for seq_packet-oriented sockets of any address family and protocol. 36 * 37 * @par Examples 38 * Constructing using a native address family and socket protocol: 39 * @code seq_packet_protocol p(AF_INET, IPPROTO_SCTP); @endcode 40 * 41 * @par Thread Safety 42 * @e Distinct @e objects: Safe.@n 43 * @e Shared @e objects: Safe. 44 * 45 * @par Concepts: 46 * Protocol. 47 */ 48 class seq_packet_protocol 49 { 50 public: 51 /// Construct a protocol object for a specific address family and protocol. seq_packet_protocol(int address_family,int socket_protocol)52 seq_packet_protocol(int address_family, int socket_protocol) 53 : family_(address_family), 54 protocol_(socket_protocol) 55 { 56 } 57 58 /// Construct a generic protocol object from a specific protocol. 59 /** 60 * @throws @c bad_cast Thrown if the source protocol is not based around 61 * sequenced packets. 62 */ 63 template <typename Protocol> seq_packet_protocol(const Protocol & source_protocol)64 seq_packet_protocol(const Protocol& source_protocol) 65 : family_(source_protocol.family()), 66 protocol_(source_protocol.protocol()) 67 { 68 if (source_protocol.type() != type()) 69 { 70 std::bad_cast ex; 71 boost::asio::detail::throw_exception(ex); 72 } 73 } 74 75 /// Obtain an identifier for the type of the protocol. type() const76 int type() const BOOST_ASIO_NOEXCEPT 77 { 78 return BOOST_ASIO_OS_DEF(SOCK_SEQPACKET); 79 } 80 81 /// Obtain an identifier for the protocol. protocol() const82 int protocol() const BOOST_ASIO_NOEXCEPT 83 { 84 return protocol_; 85 } 86 87 /// Obtain an identifier for the protocol family. family() const88 int family() const BOOST_ASIO_NOEXCEPT 89 { 90 return family_; 91 } 92 93 /// Compare two protocols for equality. operator ==(const seq_packet_protocol & p1,const seq_packet_protocol & p2)94 friend bool operator==(const seq_packet_protocol& p1, 95 const seq_packet_protocol& p2) 96 { 97 return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_; 98 } 99 100 /// Compare two protocols for inequality. operator !=(const seq_packet_protocol & p1,const seq_packet_protocol & p2)101 friend bool operator!=(const seq_packet_protocol& p1, 102 const seq_packet_protocol& p2) 103 { 104 return !(p1 == p2); 105 } 106 107 /// The type of an endpoint. 108 typedef basic_endpoint<seq_packet_protocol> endpoint; 109 110 /// The generic socket type. 111 typedef basic_seq_packet_socket<seq_packet_protocol> socket; 112 113 private: 114 int family_; 115 int protocol_; 116 }; 117 118 } // namespace generic 119 } // namespace asio 120 } // namespace boost 121 122 #include <boost/asio/detail/pop_options.hpp> 123 124 #endif // BOOST_ASIO_GENERIC_SEQ_PACKET_PROTOCOL_HPP 125