basic_stream_handle.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // basic_stream_handle.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_BASIC_STREAM_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_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/windows/basic_handle.hpp>
  22. #include <boost/asio/windows/stream_handle_service.hpp>
  23. #include <boost/asio/detail/throw_error.hpp>
  24. #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  25. || defined(GENERATING_DOCUMENTATION)
  26. namespace boost {
  27. namespace asio {
  28. namespace windows {
  29. /// Provides stream-oriented handle functionality.
  30. /**
  31. * The windows::basic_stream_handle class template provides asynchronous and
  32. * blocking stream-oriented handle functionality.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. *
  38. * @par Concepts:
  39. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  40. */
  41. template <typename StreamHandleService = stream_handle_service>
  42. class basic_stream_handle
  43. : public basic_handle<StreamHandleService>
  44. {
  45. public:
  46. /// The native representation of a handle.
  47. typedef typename StreamHandleService::native_type native_type;
  48. /// Construct a basic_stream_handle without opening it.
  49. /**
  50. * This constructor creates a stream handle without opening it. The handle
  51. * needs to be opened and then connected or accepted before data can be sent
  52. * or received on it.
  53. *
  54. * @param io_service The io_service object that the stream handle will use to
  55. * dispatch handlers for any asynchronous operations performed on the handle.
  56. */
  57. explicit basic_stream_handle(boost::asio::io_service& io_service)
  58. : basic_handle<StreamHandleService>(io_service)
  59. {
  60. }
  61. /// Construct a basic_stream_handle on an existing native handle.
  62. /**
  63. * This constructor creates a stream handle object to hold an existing native
  64. * handle.
  65. *
  66. * @param io_service The io_service object that the stream handle will use to
  67. * dispatch handlers for any asynchronous operations performed on the handle.
  68. *
  69. * @param native_handle The new underlying handle implementation.
  70. *
  71. * @throws boost::system::system_error Thrown on failure.
  72. */
  73. basic_stream_handle(boost::asio::io_service& io_service,
  74. const native_type& native_handle)
  75. : basic_handle<StreamHandleService>(io_service, native_handle)
  76. {
  77. }
  78. /// Write some data to the handle.
  79. /**
  80. * This function is used to write data to the stream handle. The function call
  81. * will block until one or more bytes of the data has been written
  82. * successfully, or until an error occurs.
  83. *
  84. * @param buffers One or more data buffers to be written to the handle.
  85. *
  86. * @returns The number of bytes written.
  87. *
  88. * @throws boost::system::system_error Thrown on failure. An error code of
  89. * boost::asio::error::eof indicates that the connection was closed by the
  90. * peer.
  91. *
  92. * @note The write_some operation may not transmit all of the data to the
  93. * peer. Consider using the @ref write function if you need to ensure that
  94. * all data is written before the blocking operation completes.
  95. *
  96. * @par Example
  97. * To write a single data buffer use the @ref buffer function as follows:
  98. * @code
  99. * handle.write_some(boost::asio::buffer(data, size));
  100. * @endcode
  101. * See the @ref buffer documentation for information on writing multiple
  102. * buffers in one go, and how to use it with arrays, boost::array or
  103. * std::vector.
  104. */
  105. template <typename ConstBufferSequence>
  106. std::size_t write_some(const ConstBufferSequence& buffers)
  107. {
  108. boost::system::error_code ec;
  109. std::size_t s = this->service.write_some(this->implementation, buffers, ec);
  110. boost::asio::detail::throw_error(ec);
  111. return s;
  112. }
  113. /// Write some data to the handle.
  114. /**
  115. * This function is used to write data to the stream handle. The function call
  116. * will block until one or more bytes of the data has been written
  117. * successfully, or until an error occurs.
  118. *
  119. * @param buffers One or more data buffers to be written to the handle.
  120. *
  121. * @param ec Set to indicate what error occurred, if any.
  122. *
  123. * @returns The number of bytes written. Returns 0 if an error occurred.
  124. *
  125. * @note The write_some operation may not transmit all of the data to the
  126. * peer. Consider using the @ref write function if you need to ensure that
  127. * all data is written before the blocking operation completes.
  128. */
  129. template <typename ConstBufferSequence>
  130. std::size_t write_some(const ConstBufferSequence& buffers,
  131. boost::system::error_code& ec)
  132. {
  133. return this->service.write_some(this->implementation, buffers, ec);
  134. }
  135. /// Start an asynchronous write.
  136. /**
  137. * This function is used to asynchronously write data to the stream handle.
  138. * The function call always returns immediately.
  139. *
  140. * @param buffers One or more data buffers to be written to the handle.
  141. * Although the buffers object may be copied as necessary, ownership of the
  142. * underlying memory blocks is retained by the caller, which must guarantee
  143. * that they remain valid until the handler is called.
  144. *
  145. * @param handler The handler to be called when the write operation completes.
  146. * Copies will be made of the handler as required. The function signature of
  147. * the handler must be:
  148. * @code void handler(
  149. * const boost::system::error_code& error, // Result of operation.
  150. * std::size_t bytes_transferred // Number of bytes written.
  151. * ); @endcode
  152. * Regardless of whether the asynchronous operation completes immediately or
  153. * not, the handler will not be invoked from within this function. Invocation
  154. * of the handler will be performed in a manner equivalent to using
  155. * boost::asio::io_service::post().
  156. *
  157. * @note The write operation may not transmit all of the data to the peer.
  158. * Consider using the @ref async_write function if you need to ensure that all
  159. * data is written before the asynchronous operation completes.
  160. *
  161. * @par Example
  162. * To write a single data buffer use the @ref buffer function as follows:
  163. * @code
  164. * handle.async_write_some(boost::asio::buffer(data, size), handler);
  165. * @endcode
  166. * See the @ref buffer documentation for information on writing multiple
  167. * buffers in one go, and how to use it with arrays, boost::array or
  168. * std::vector.
  169. */
  170. template <typename ConstBufferSequence, typename WriteHandler>
  171. void async_write_some(const ConstBufferSequence& buffers,
  172. WriteHandler handler)
  173. {
  174. this->service.async_write_some(this->implementation, buffers, handler);
  175. }
  176. /// Read some data from the handle.
  177. /**
  178. * This function is used to read data from the stream handle. The function
  179. * call will block until one or more bytes of data has been read successfully,
  180. * or until an error occurs.
  181. *
  182. * @param buffers One or more buffers into which the data will be read.
  183. *
  184. * @returns The number of bytes read.
  185. *
  186. * @throws boost::system::system_error Thrown on failure. An error code of
  187. * boost::asio::error::eof indicates that the connection was closed by the
  188. * peer.
  189. *
  190. * @note The read_some operation may not read all of the requested number of
  191. * bytes. Consider using the @ref read function if you need to ensure that
  192. * the requested amount of data is read before the blocking operation
  193. * completes.
  194. *
  195. * @par Example
  196. * To read into a single data buffer use the @ref buffer function as follows:
  197. * @code
  198. * handle.read_some(boost::asio::buffer(data, size));
  199. * @endcode
  200. * See the @ref buffer documentation for information on reading into multiple
  201. * buffers in one go, and how to use it with arrays, boost::array or
  202. * std::vector.
  203. */
  204. template <typename MutableBufferSequence>
  205. std::size_t read_some(const MutableBufferSequence& buffers)
  206. {
  207. boost::system::error_code ec;
  208. std::size_t s = this->service.read_some(this->implementation, buffers, ec);
  209. boost::asio::detail::throw_error(ec);
  210. return s;
  211. }
  212. /// Read some data from the handle.
  213. /**
  214. * This function is used to read data from the stream handle. The function
  215. * call will block until one or more bytes of data has been read successfully,
  216. * or until an error occurs.
  217. *
  218. * @param buffers One or more buffers into which the data will be read.
  219. *
  220. * @param ec Set to indicate what error occurred, if any.
  221. *
  222. * @returns The number of bytes read. Returns 0 if an error occurred.
  223. *
  224. * @note The read_some operation may not read all of the requested number of
  225. * bytes. Consider using the @ref read function if you need to ensure that
  226. * the requested amount of data is read before the blocking operation
  227. * completes.
  228. */
  229. template <typename MutableBufferSequence>
  230. std::size_t read_some(const MutableBufferSequence& buffers,
  231. boost::system::error_code& ec)
  232. {
  233. return this->service.read_some(this->implementation, buffers, ec);
  234. }
  235. /// Start an asynchronous read.
  236. /**
  237. * This function is used to asynchronously read data from the stream handle.
  238. * The function call always returns immediately.
  239. *
  240. * @param buffers One or more buffers into which the data will be read.
  241. * Although the buffers object may be copied as necessary, ownership of the
  242. * underlying memory blocks is retained by the caller, which must guarantee
  243. * that they remain valid until the handler is called.
  244. *
  245. * @param handler The handler to be called when the read operation completes.
  246. * Copies will be made of the handler as required. The function signature of
  247. * the handler must be:
  248. * @code void handler(
  249. * const boost::system::error_code& error, // Result of operation.
  250. * std::size_t bytes_transferred // Number of bytes read.
  251. * ); @endcode
  252. * Regardless of whether the asynchronous operation completes immediately or
  253. * not, the handler will not be invoked from within this function. Invocation
  254. * of the handler will be performed in a manner equivalent to using
  255. * boost::asio::io_service::post().
  256. *
  257. * @note The read operation may not read all of the requested number of bytes.
  258. * Consider using the @ref async_read function if you need to ensure that the
  259. * requested amount of data is read before the asynchronous operation
  260. * completes.
  261. *
  262. * @par Example
  263. * To read into a single data buffer use the @ref buffer function as follows:
  264. * @code
  265. * handle.async_read_some(boost::asio::buffer(data, size), handler);
  266. * @endcode
  267. * See the @ref buffer documentation for information on reading into multiple
  268. * buffers in one go, and how to use it with arrays, boost::array or
  269. * std::vector.
  270. */
  271. template <typename MutableBufferSequence, typename ReadHandler>
  272. void async_read_some(const MutableBufferSequence& buffers,
  273. ReadHandler handler)
  274. {
  275. this->service.async_read_some(this->implementation, buffers, handler);
  276. }
  277. };
  278. } // namespace windows
  279. } // namespace asio
  280. } // namespace boost
  281. #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  282. // || defined(GENERATING_DOCUMENTATION)
  283. #include <boost/asio/detail/pop_options.hpp>
  284. #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP