basic_stream_descriptor.hpp 11 KB

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