stream_handle_service.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // stream_handle_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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_WINDOWS_STREAM_HANDLE_SERVICE_HPP
  11. #define BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/io_service.hpp>
  22. #include <boost/asio/detail/service_base.hpp>
  23. #include <boost/asio/detail/win_iocp_handle_service.hpp>
  24. #if !defined(BOOST_ASIO_DISABLE_WINDOWS_STREAM_HANDLE)
  25. # if defined(BOOST_ASIO_HAS_IOCP)
  26. # define BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE 1
  27. # endif // defined(BOOST_ASIO_HAS_IOCP)
  28. #endif // !defined(BOOST_ASIO_DISABLE_WINDOWS_STREAM_HANDLE)
  29. #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  30. || defined(GENERATING_DOCUMENTATION)
  31. namespace boost {
  32. namespace asio {
  33. namespace windows {
  34. /// Default service implementation for a stream handle.
  35. class stream_handle_service
  36. #if defined(GENERATING_DOCUMENTATION)
  37. : public boost::asio::io_service::service
  38. #else
  39. : public boost::asio::detail::service_base<stream_handle_service>
  40. #endif
  41. {
  42. public:
  43. #if defined(GENERATING_DOCUMENTATION)
  44. /// The unique service identifier.
  45. static boost::asio::io_service::id id;
  46. #endif
  47. private:
  48. // The type of the platform-specific implementation.
  49. typedef detail::win_iocp_handle_service service_impl_type;
  50. public:
  51. /// The type of a stream handle implementation.
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined implementation_type;
  54. #else
  55. typedef service_impl_type::implementation_type implementation_type;
  56. #endif
  57. /// The native handle type.
  58. #if defined(GENERATING_DOCUMENTATION)
  59. typedef implementation_defined native_type;
  60. #else
  61. typedef service_impl_type::native_type native_type;
  62. #endif
  63. /// Construct a new stream handle service for the specified io_service.
  64. explicit stream_handle_service(boost::asio::io_service& io_service)
  65. : boost::asio::detail::service_base<stream_handle_service>(io_service),
  66. service_impl_(boost::asio::use_service<service_impl_type>(io_service))
  67. {
  68. }
  69. /// Destroy all user-defined handler objects owned by the service.
  70. void shutdown_service()
  71. {
  72. }
  73. /// Construct a new stream handle implementation.
  74. void construct(implementation_type& impl)
  75. {
  76. service_impl_.construct(impl);
  77. }
  78. /// Destroy a stream handle implementation.
  79. void destroy(implementation_type& impl)
  80. {
  81. service_impl_.destroy(impl);
  82. }
  83. /// Assign an existing native handle to a stream handle.
  84. boost::system::error_code assign(implementation_type& impl,
  85. const native_type& native_handle, boost::system::error_code& ec)
  86. {
  87. return service_impl_.assign(impl, native_handle, ec);
  88. }
  89. /// Determine whether the handle is open.
  90. bool is_open(const implementation_type& impl) const
  91. {
  92. return service_impl_.is_open(impl);
  93. }
  94. /// Close a stream handle implementation.
  95. boost::system::error_code close(implementation_type& impl,
  96. boost::system::error_code& ec)
  97. {
  98. return service_impl_.close(impl, ec);
  99. }
  100. /// Get the native handle implementation.
  101. native_type native(implementation_type& impl)
  102. {
  103. return service_impl_.native(impl);
  104. }
  105. /// Cancel all asynchronous operations associated with the handle.
  106. boost::system::error_code cancel(implementation_type& impl,
  107. boost::system::error_code& ec)
  108. {
  109. return service_impl_.cancel(impl, ec);
  110. }
  111. /// Write the given data to the stream.
  112. template <typename ConstBufferSequence>
  113. std::size_t write_some(implementation_type& impl,
  114. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  115. {
  116. return service_impl_.write_some(impl, buffers, ec);
  117. }
  118. /// Start an asynchronous write.
  119. template <typename ConstBufferSequence, typename WriteHandler>
  120. void async_write_some(implementation_type& impl,
  121. const ConstBufferSequence& buffers, WriteHandler handler)
  122. {
  123. service_impl_.async_write_some(impl, buffers, handler);
  124. }
  125. /// Read some data from the stream.
  126. template <typename MutableBufferSequence>
  127. std::size_t read_some(implementation_type& impl,
  128. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  129. {
  130. return service_impl_.read_some(impl, buffers, ec);
  131. }
  132. /// Start an asynchronous read.
  133. template <typename MutableBufferSequence, typename ReadHandler>
  134. void async_read_some(implementation_type& impl,
  135. const MutableBufferSequence& buffers, ReadHandler handler)
  136. {
  137. service_impl_.async_read_some(impl, buffers, handler);
  138. }
  139. private:
  140. // The service that provides the platform-specific implementation.
  141. service_impl_type& service_impl_;
  142. };
  143. } // namespace windows
  144. } // namespace asio
  145. } // namespace boost
  146. #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  147. // || defined(GENERATING_DOCUMENTATION)
  148. #include <boost/asio/detail/pop_options.hpp>
  149. #endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP