stream_service.hpp 5.5 KB

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