seq_packet_protocol.hpp 3.2 KB

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