stream_service.hpp 5.4 KB

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