stream_service.hpp 5.5 KB

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