basic_stream_handle.hpp 11 KB

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