/山火-ヤギの咆哮-/lib/boost_1_45_0/boost/asio/windows/basic_stream_handle.hpp

https://gitlab.com/kokeiro001/YamabiYagiNoHoko · C++ Header · 302 lines · 79 code · 20 blank · 203 comment · 2 complexity · 115ad5a16a7096ea1b59290ec97e426d MD5 · raw file

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