stream_handle_service.hpp 4.6 KB

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