stream_descriptor_service.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // stream_descriptor_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_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
  11. #define BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_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/epoll_reactor.hpp>
  23. #include <boost/asio/detail/kqueue_reactor.hpp>
  24. #include <boost/asio/detail/select_reactor.hpp>
  25. #include <boost/asio/detail/service_base.hpp>
  26. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  27. #if !defined(BOOST_ASIO_DISABLE_POSIX_STREAM_DESCRIPTOR)
  28. # if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  29. # define BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR 1
  30. # endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  31. #endif // !defined(BOOST_ASIO_DISABLE_POSIX_STREAM_DESCRIPTOR)
  32. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
  33. || defined(GENERATING_DOCUMENTATION)
  34. namespace boost {
  35. namespace asio {
  36. namespace posix {
  37. /// Default service implementation for a stream descriptor.
  38. class stream_descriptor_service
  39. #if defined(GENERATING_DOCUMENTATION)
  40. : public boost::asio::io_service::service
  41. #else
  42. : public boost::asio::detail::service_base<stream_descriptor_service>
  43. #endif
  44. {
  45. public:
  46. #if defined(GENERATING_DOCUMENTATION)
  47. /// The unique service identifier.
  48. static boost::asio::io_service::id id;
  49. #endif
  50. private:
  51. // The type of the platform-specific implementation.
  52. #if defined(BOOST_ASIO_HAS_EPOLL)
  53. typedef detail::reactive_descriptor_service<
  54. detail::epoll_reactor<false> > service_impl_type;
  55. #elif defined(BOOST_ASIO_HAS_KQUEUE)
  56. typedef detail::reactive_descriptor_service<
  57. detail::kqueue_reactor<false> > service_impl_type;
  58. #elif defined(BOOST_ASIO_HAS_DEV_POLL)
  59. typedef detail::reactive_descriptor_service<
  60. detail::dev_poll_reactor<false> > service_impl_type;
  61. #else
  62. typedef detail::reactive_descriptor_service<
  63. detail::select_reactor<false> > service_impl_type;
  64. #endif
  65. public:
  66. /// The type of a stream descriptor implementation.
  67. #if defined(GENERATING_DOCUMENTATION)
  68. typedef implementation_defined implementation_type;
  69. #else
  70. typedef service_impl_type::implementation_type implementation_type;
  71. #endif
  72. /// The native descriptor type.
  73. #if defined(GENERATING_DOCUMENTATION)
  74. typedef implementation_defined native_type;
  75. #else
  76. typedef service_impl_type::native_type native_type;
  77. #endif
  78. /// Construct a new stream descriptor service for the specified io_service.
  79. explicit stream_descriptor_service(boost::asio::io_service& io_service)
  80. : boost::asio::detail::service_base<stream_descriptor_service>(io_service),
  81. service_impl_(boost::asio::use_service<service_impl_type>(io_service))
  82. {
  83. }
  84. /// Destroy all user-defined descriptorr objects owned by the service.
  85. void shutdown_service()
  86. {
  87. }
  88. /// Construct a new stream descriptor implementation.
  89. void construct(implementation_type& impl)
  90. {
  91. service_impl_.construct(impl);
  92. }
  93. /// Destroy a stream descriptor implementation.
  94. void destroy(implementation_type& impl)
  95. {
  96. service_impl_.destroy(impl);
  97. }
  98. /// Assign an existing native descriptor to a stream descriptor.
  99. boost::system::error_code assign(implementation_type& impl,
  100. const native_type& native_descriptor, boost::system::error_code& ec)
  101. {
  102. return service_impl_.assign(impl, native_descriptor, ec);
  103. }
  104. /// Determine whether the descriptor is open.
  105. bool is_open(const implementation_type& impl) const
  106. {
  107. return service_impl_.is_open(impl);
  108. }
  109. /// Close a stream descriptor implementation.
  110. boost::system::error_code close(implementation_type& impl,
  111. boost::system::error_code& ec)
  112. {
  113. return service_impl_.close(impl, ec);
  114. }
  115. /// Get the native descriptor implementation.
  116. native_type native(implementation_type& impl)
  117. {
  118. return service_impl_.native(impl);
  119. }
  120. /// Cancel all asynchronous operations associated with the descriptor.
  121. boost::system::error_code cancel(implementation_type& impl,
  122. boost::system::error_code& ec)
  123. {
  124. return service_impl_.cancel(impl, ec);
  125. }
  126. /// Perform an IO control command on the descriptor.
  127. template <typename IoControlCommand>
  128. boost::system::error_code io_control(implementation_type& impl,
  129. IoControlCommand& command, boost::system::error_code& ec)
  130. {
  131. return service_impl_.io_control(impl, command, ec);
  132. }
  133. /// Write the given data to the stream.
  134. template <typename ConstBufferSequence>
  135. std::size_t write_some(implementation_type& impl,
  136. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  137. {
  138. return service_impl_.write_some(impl, buffers, ec);
  139. }
  140. /// Start an asynchronous write.
  141. template <typename ConstBufferSequence, typename WriteHandler>
  142. void async_write_some(implementation_type& impl,
  143. const ConstBufferSequence& buffers, WriteHandler descriptorr)
  144. {
  145. service_impl_.async_write_some(impl, buffers, descriptorr);
  146. }
  147. /// Read some data from the stream.
  148. template <typename MutableBufferSequence>
  149. std::size_t read_some(implementation_type& impl,
  150. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  151. {
  152. return service_impl_.read_some(impl, buffers, ec);
  153. }
  154. /// Start an asynchronous read.
  155. template <typename MutableBufferSequence, typename ReadHandler>
  156. void async_read_some(implementation_type& impl,
  157. const MutableBufferSequence& buffers, ReadHandler descriptorr)
  158. {
  159. service_impl_.async_read_some(impl, buffers, descriptorr);
  160. }
  161. private:
  162. // The service that provides the platform-specific implementation.
  163. service_impl_type& service_impl_;
  164. };
  165. } // namespace posix
  166. } // namespace asio
  167. } // namespace boost
  168. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  169. // || defined(GENERATING_DOCUMENTATION)
  170. #include <boost/asio/detail/pop_options.hpp>
  171. #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP