stream_service.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // stream_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_SSL_STREAM_SERVICE_HPP
  12. #define BOOST_ASIO_SSL_STREAM_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. #include <cstddef>
  19. #include <boost/config.hpp>
  20. #include <boost/noncopyable.hpp>
  21. #include <boost/asio/detail/pop_options.hpp>
  22. #include <boost/asio/io_service.hpp>
  23. #include <boost/asio/detail/service_base.hpp>
  24. #include <boost/asio/ssl/basic_context.hpp>
  25. #include <boost/asio/ssl/stream_base.hpp>
  26. #include <boost/asio/ssl/detail/openssl_stream_service.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace ssl {
  30. /// Default service implementation for an SSL stream.
  31. class stream_service
  32. #if defined(GENERATING_DOCUMENTATION)
  33. : public boost::asio::io_service::service
  34. #else
  35. : public boost::asio::detail::service_base<stream_service>
  36. #endif
  37. {
  38. private:
  39. // The type of the platform-specific implementation.
  40. typedef detail::openssl_stream_service service_impl_type;
  41. public:
  42. #if defined(GENERATING_DOCUMENTATION)
  43. /// The unique service identifier.
  44. static boost::asio::io_service::id id;
  45. #endif
  46. /// The type of a stream implementation.
  47. #if defined(GENERATING_DOCUMENTATION)
  48. typedef implementation_defined impl_type;
  49. #else
  50. typedef service_impl_type::impl_type impl_type;
  51. #endif
  52. /// Construct a new stream service for the specified io_service.
  53. explicit stream_service(boost::asio::io_service& io_service)
  54. : boost::asio::detail::service_base<stream_service>(io_service),
  55. service_impl_(boost::asio::use_service<service_impl_type>(io_service))
  56. {
  57. }
  58. /// Destroy all user-defined handler objects owned by the service.
  59. void shutdown_service()
  60. {
  61. }
  62. /// Return a null stream implementation.
  63. impl_type null() const
  64. {
  65. return service_impl_.null();
  66. }
  67. /// Create a new stream implementation.
  68. template <typename Stream, typename Context_Service>
  69. void create(impl_type& impl, Stream& next_layer,
  70. basic_context<Context_Service>& context)
  71. {
  72. service_impl_.create(impl, next_layer, context);
  73. }
  74. /// Destroy a stream implementation.
  75. template <typename Stream>
  76. void destroy(impl_type& impl, Stream& next_layer)
  77. {
  78. service_impl_.destroy(impl, next_layer);
  79. }
  80. /// Perform SSL handshaking.
  81. template <typename Stream>
  82. boost::system::error_code handshake(impl_type& impl, Stream& next_layer,
  83. stream_base::handshake_type type, boost::system::error_code& ec)
  84. {
  85. return service_impl_.handshake(impl, next_layer, type, ec);
  86. }
  87. /// Start an asynchronous SSL handshake.
  88. template <typename Stream, typename HandshakeHandler>
  89. void async_handshake(impl_type& impl, Stream& next_layer,
  90. stream_base::handshake_type type, HandshakeHandler handler)
  91. {
  92. service_impl_.async_handshake(impl, next_layer, type, handler);
  93. }
  94. /// Shut down SSL on the stream.
  95. template <typename Stream>
  96. boost::system::error_code shutdown(impl_type& impl, Stream& next_layer,
  97. boost::system::error_code& ec)
  98. {
  99. return service_impl_.shutdown(impl, next_layer, ec);
  100. }
  101. /// Asynchronously shut down SSL on the stream.
  102. template <typename Stream, typename ShutdownHandler>
  103. void async_shutdown(impl_type& impl, Stream& next_layer,
  104. ShutdownHandler handler)
  105. {
  106. service_impl_.async_shutdown(impl, next_layer, handler);
  107. }
  108. /// Write some data to the stream.
  109. template <typename Stream, typename ConstBufferSequence>
  110. std::size_t write_some(impl_type& impl, Stream& next_layer,
  111. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  112. {
  113. return service_impl_.write_some(impl, next_layer, buffers, ec);
  114. }
  115. /// Start an asynchronous write.
  116. template <typename Stream, typename ConstBufferSequence,
  117. typename WriteHandler>
  118. void async_write_some(impl_type& impl, Stream& next_layer,
  119. const ConstBufferSequence& buffers, WriteHandler handler)
  120. {
  121. service_impl_.async_write_some(impl, next_layer, buffers, handler);
  122. }
  123. /// Read some data from the stream.
  124. template <typename Stream, typename MutableBufferSequence>
  125. std::size_t read_some(impl_type& impl, Stream& next_layer,
  126. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  127. {
  128. return service_impl_.read_some(impl, next_layer, buffers, ec);
  129. }
  130. /// Start an asynchronous read.
  131. template <typename Stream, typename MutableBufferSequence,
  132. typename ReadHandler>
  133. void async_read_some(impl_type& impl, Stream& next_layer,
  134. const MutableBufferSequence& buffers, ReadHandler handler)
  135. {
  136. service_impl_.async_read_some(impl, next_layer, buffers, handler);
  137. }
  138. /// Peek at the incoming data on the stream.
  139. template <typename Stream, typename MutableBufferSequence>
  140. std::size_t peek(impl_type& impl, Stream& next_layer,
  141. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  142. {
  143. return service_impl_.peek(impl, next_layer, buffers, ec);
  144. }
  145. /// Determine the amount of data that may be read without blocking.
  146. template <typename Stream>
  147. std::size_t in_avail(impl_type& impl, Stream& next_layer,
  148. boost::system::error_code& ec)
  149. {
  150. return service_impl_.in_avail(impl, next_layer, ec);
  151. }
  152. private:
  153. // The service that provides the platform-specific implementation.
  154. service_impl_type& service_impl_;
  155. };
  156. } // namespace ssl
  157. } // namespace asio
  158. } // namespace boost
  159. #include <boost/asio/detail/pop_options.hpp>
  160. #endif // BOOST_ASIO_SSL_STREAM_SERVICE_HPP