stream_protocol.hpp 3.4 KB

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