stream.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. //
  2. // stream.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_SSL_STREAM_HPP
  12. #define ASIO_SSL_STREAM_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/push_options.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. #include <cstddef>
  19. #include <boost/config.hpp>
  20. #include <boost/noncopyable.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include "asio/detail/pop_options.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/ssl/basic_context.hpp"
  25. #include "asio/ssl/stream_base.hpp"
  26. #include "asio/ssl/stream_service.hpp"
  27. #include "asio/detail/throw_error.hpp"
  28. namespace asio {
  29. namespace ssl {
  30. /// Provides stream-oriented functionality using SSL.
  31. /**
  32. * The stream class template provides asynchronous and blocking stream-oriented
  33. * functionality using SSL.
  34. *
  35. * @par Thread Safety
  36. * @e Distinct @e objects: Safe.@n
  37. * @e Shared @e objects: Unsafe.
  38. *
  39. * @par Example
  40. * To use the SSL stream template with an ip::tcp::socket, you would write:
  41. * @code
  42. * asio::io_service io_service;
  43. * asio::ssl::context context(io_service, asio::ssl::context::sslv23);
  44. * asio::ssl::stream<asio::ip::tcp::socket> sock(io_service, context);
  45. * @endcode
  46. *
  47. * @par Concepts:
  48. * AsyncReadStream, AsyncWriteStream, Stream, SyncRead_Stream, SyncWriteStream.
  49. */
  50. template <typename Stream, typename Service = stream_service>
  51. class stream
  52. : public stream_base,
  53. private boost::noncopyable
  54. {
  55. public:
  56. /// The type of the next layer.
  57. typedef typename boost::remove_reference<Stream>::type next_layer_type;
  58. /// The type of the lowest layer.
  59. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  60. /// The type of the service that will be used to provide stream operations.
  61. typedef Service service_type;
  62. /// The native implementation type of the stream.
  63. typedef typename service_type::impl_type impl_type;
  64. /// Construct a stream.
  65. /**
  66. * This constructor creates a stream and initialises the underlying stream
  67. * object.
  68. *
  69. * @param arg The argument to be passed to initialise the underlying stream.
  70. *
  71. * @param context The SSL context to be used for the stream.
  72. */
  73. template <typename Arg, typename Context_Service>
  74. explicit stream(Arg& arg, basic_context<Context_Service>& context)
  75. : next_layer_(arg),
  76. service_(asio::use_service<Service>(next_layer_.get_io_service())),
  77. impl_(service_.null())
  78. {
  79. service_.create(impl_, next_layer_, context);
  80. }
  81. /// Destructor.
  82. ~stream()
  83. {
  84. service_.destroy(impl_, next_layer_);
  85. }
  86. /// (Deprecated: use get_io_service().) Get the io_service associated with
  87. /// the object.
  88. /**
  89. * This function may be used to obtain the io_service object that the stream
  90. * uses to dispatch handlers for asynchronous operations.
  91. *
  92. * @return A reference to the io_service object that stream will use to
  93. * dispatch handlers. Ownership is not transferred to the caller.
  94. */
  95. asio::io_service& io_service()
  96. {
  97. return next_layer_.get_io_service();
  98. }
  99. /// Get the io_service associated with the object.
  100. /**
  101. * This function may be used to obtain the io_service object that the stream
  102. * uses to dispatch handlers for asynchronous operations.
  103. *
  104. * @return A reference to the io_service object that stream will use to
  105. * dispatch handlers. Ownership is not transferred to the caller.
  106. */
  107. asio::io_service& get_io_service()
  108. {
  109. return next_layer_.get_io_service();
  110. }
  111. /// Get a reference to the next layer.
  112. /**
  113. * This function returns a reference to the next layer in a stack of stream
  114. * layers.
  115. *
  116. * @return A reference to the next layer in the stack of stream layers.
  117. * Ownership is not transferred to the caller.
  118. */
  119. next_layer_type& next_layer()
  120. {
  121. return next_layer_;
  122. }
  123. /// Get a reference to the lowest layer.
  124. /**
  125. * This function returns a reference to the lowest layer in a stack of
  126. * stream layers.
  127. *
  128. * @return A reference to the lowest layer in the stack of stream layers.
  129. * Ownership is not transferred to the caller.
  130. */
  131. lowest_layer_type& lowest_layer()
  132. {
  133. return next_layer_.lowest_layer();
  134. }
  135. /// Get a const reference to the lowest layer.
  136. /**
  137. * This function returns a const reference to the lowest layer in a stack of
  138. * stream layers.
  139. *
  140. * @return A const reference to the lowest layer in the stack of stream
  141. * layers. Ownership is not transferred to the caller.
  142. */
  143. const lowest_layer_type& lowest_layer() const
  144. {
  145. return next_layer_.lowest_layer();
  146. }
  147. /// Get the underlying implementation in the native type.
  148. /**
  149. * This function may be used to obtain the underlying implementation of the
  150. * context. This is intended to allow access to stream functionality that is
  151. * not otherwise provided.
  152. */
  153. impl_type impl()
  154. {
  155. return impl_;
  156. }
  157. /// Perform SSL handshaking.
  158. /**
  159. * This function is used to perform SSL handshaking on the stream. The
  160. * function call will block until handshaking is complete or an error occurs.
  161. *
  162. * @param type The type of handshaking to be performed, i.e. as a client or as
  163. * a server.
  164. *
  165. * @throws asio::system_error Thrown on failure.
  166. */
  167. void handshake(handshake_type type)
  168. {
  169. asio::error_code ec;
  170. service_.handshake(impl_, next_layer_, type, ec);
  171. asio::detail::throw_error(ec);
  172. }
  173. /// Perform SSL handshaking.
  174. /**
  175. * This function is used to perform SSL handshaking on the stream. The
  176. * function call will block until handshaking is complete or an error occurs.
  177. *
  178. * @param type The type of handshaking to be performed, i.e. as a client or as
  179. * a server.
  180. *
  181. * @param ec Set to indicate what error occurred, if any.
  182. */
  183. asio::error_code handshake(handshake_type type,
  184. asio::error_code& ec)
  185. {
  186. return service_.handshake(impl_, next_layer_, type, ec);
  187. }
  188. /// Start an asynchronous SSL handshake.
  189. /**
  190. * This function is used to asynchronously perform an SSL handshake on the
  191. * stream. This function call always returns immediately.
  192. *
  193. * @param type The type of handshaking to be performed, i.e. as a client or as
  194. * a server.
  195. *
  196. * @param handler The handler to be called when the handshake operation
  197. * completes. Copies will be made of the handler as required. The equivalent
  198. * function signature of the handler must be:
  199. * @code void handler(
  200. * const asio::error_code& error // Result of operation.
  201. * ); @endcode
  202. */
  203. template <typename HandshakeHandler>
  204. void async_handshake(handshake_type type, HandshakeHandler handler)
  205. {
  206. service_.async_handshake(impl_, next_layer_, type, handler);
  207. }
  208. /// Shut down SSL on the stream.
  209. /**
  210. * This function is used to shut down SSL on the stream. The function call
  211. * will block until SSL has been shut down or an error occurs.
  212. *
  213. * @throws asio::system_error Thrown on failure.
  214. */
  215. void shutdown()
  216. {
  217. asio::error_code ec;
  218. service_.shutdown(impl_, next_layer_, ec);
  219. asio::detail::throw_error(ec);
  220. }
  221. /// Shut down SSL on the stream.
  222. /**
  223. * This function is used to shut down SSL on the stream. The function call
  224. * will block until SSL has been shut down or an error occurs.
  225. *
  226. * @param ec Set to indicate what error occurred, if any.
  227. */
  228. asio::error_code shutdown(asio::error_code& ec)
  229. {
  230. return service_.shutdown(impl_, next_layer_, ec);
  231. }
  232. /// Asynchronously shut down SSL on the stream.
  233. /**
  234. * This function is used to asynchronously shut down SSL on the stream. This
  235. * function call always returns immediately.
  236. *
  237. * @param handler The handler to be called when the handshake operation
  238. * completes. Copies will be made of the handler as required. The equivalent
  239. * function signature of the handler must be:
  240. * @code void handler(
  241. * const asio::error_code& error // Result of operation.
  242. * ); @endcode
  243. */
  244. template <typename ShutdownHandler>
  245. void async_shutdown(ShutdownHandler handler)
  246. {
  247. service_.async_shutdown(impl_, next_layer_, handler);
  248. }
  249. /// Write some data to the stream.
  250. /**
  251. * This function is used to write data on the stream. The function call will
  252. * block until one or more bytes of data has been written successfully, or
  253. * until an error occurs.
  254. *
  255. * @param buffers The data to be written.
  256. *
  257. * @returns The number of bytes written.
  258. *
  259. * @throws asio::system_error Thrown on failure.
  260. *
  261. * @note The write_some operation may not transmit all of the data to the
  262. * peer. Consider using the @ref write function if you need to ensure that all
  263. * data is written before the blocking operation completes.
  264. */
  265. template <typename ConstBufferSequence>
  266. std::size_t write_some(const ConstBufferSequence& buffers)
  267. {
  268. asio::error_code ec;
  269. std::size_t s = service_.write_some(impl_, next_layer_, buffers, ec);
  270. asio::detail::throw_error(ec);
  271. return s;
  272. }
  273. /// Write some data to the stream.
  274. /**
  275. * This function is used to write data on the stream. The function call will
  276. * block until one or more bytes of data has been written successfully, or
  277. * until an error occurs.
  278. *
  279. * @param buffers The data to be written to the stream.
  280. *
  281. * @param ec Set to indicate what error occurred, if any.
  282. *
  283. * @returns The number of bytes written. Returns 0 if an error occurred.
  284. *
  285. * @note The write_some operation may not transmit all of the data to the
  286. * peer. Consider using the @ref write function if you need to ensure that all
  287. * data is written before the blocking operation completes.
  288. */
  289. template <typename ConstBufferSequence>
  290. std::size_t write_some(const ConstBufferSequence& buffers,
  291. asio::error_code& ec)
  292. {
  293. return service_.write_some(impl_, next_layer_, buffers, ec);
  294. }
  295. /// Start an asynchronous write.
  296. /**
  297. * This function is used to asynchronously write one or more bytes of data to
  298. * the stream. The function call always returns immediately.
  299. *
  300. * @param buffers The data to be written to the stream. Although the buffers
  301. * object may be copied as necessary, ownership of the underlying buffers is
  302. * retained by the caller, which must guarantee that they remain valid until
  303. * the handler is called.
  304. *
  305. * @param handler The handler to be called when the write operation completes.
  306. * Copies will be made of the handler as required. The equivalent function
  307. * signature of the handler must be:
  308. * @code void handler(
  309. * const asio::error_code& error, // Result of operation.
  310. * std::size_t bytes_transferred // Number of bytes written.
  311. * ); @endcode
  312. *
  313. * @note The async_write_some operation may not transmit all of the data to
  314. * the peer. Consider using the @ref async_write function if you need to
  315. * ensure that all data is written before the blocking operation completes.
  316. */
  317. template <typename ConstBufferSequence, typename WriteHandler>
  318. void async_write_some(const ConstBufferSequence& buffers,
  319. WriteHandler handler)
  320. {
  321. service_.async_write_some(impl_, next_layer_, buffers, handler);
  322. }
  323. /// Read some data from the stream.
  324. /**
  325. * This function is used to read data from the stream. The function call will
  326. * block until one or more bytes of data has been read successfully, or until
  327. * an error occurs.
  328. *
  329. * @param buffers The buffers into which the data will be read.
  330. *
  331. * @returns The number of bytes read.
  332. *
  333. * @throws asio::system_error Thrown on failure.
  334. *
  335. * @note The read_some operation may not read all of the requested number of
  336. * bytes. Consider using the @ref read function if you need to ensure that the
  337. * requested amount of data is read before the blocking operation completes.
  338. */
  339. template <typename MutableBufferSequence>
  340. std::size_t read_some(const MutableBufferSequence& buffers)
  341. {
  342. asio::error_code ec;
  343. std::size_t s = service_.read_some(impl_, next_layer_, buffers, ec);
  344. asio::detail::throw_error(ec);
  345. return s;
  346. }
  347. /// Read some data from the stream.
  348. /**
  349. * This function is used to read data from the stream. The function call will
  350. * block until one or more bytes of data has been read successfully, or until
  351. * an error occurs.
  352. *
  353. * @param buffers The buffers into which the data will be read.
  354. *
  355. * @param ec Set to indicate what error occurred, if any.
  356. *
  357. * @returns The number of bytes read. Returns 0 if an error occurred.
  358. *
  359. * @note The read_some operation may not read all of the requested number of
  360. * bytes. Consider using the @ref read function if you need to ensure that the
  361. * requested amount of data is read before the blocking operation completes.
  362. */
  363. template <typename MutableBufferSequence>
  364. std::size_t read_some(const MutableBufferSequence& buffers,
  365. asio::error_code& ec)
  366. {
  367. return service_.read_some(impl_, next_layer_, buffers, ec);
  368. }
  369. /// Start an asynchronous read.
  370. /**
  371. * This function is used to asynchronously read one or more bytes of data from
  372. * the stream. The function call always returns immediately.
  373. *
  374. * @param buffers The buffers into which the data will be read. Although the
  375. * buffers object may be copied as necessary, ownership of the underlying
  376. * buffers is retained by the caller, which must guarantee that they remain
  377. * valid until the handler is called.
  378. *
  379. * @param handler The handler to be called when the read operation completes.
  380. * Copies will be made of the handler as required. The equivalent function
  381. * signature of the handler must be:
  382. * @code void handler(
  383. * const asio::error_code& error, // Result of operation.
  384. * std::size_t bytes_transferred // Number of bytes read.
  385. * ); @endcode
  386. *
  387. * @note The async_read_some operation may not read all of the requested
  388. * number of bytes. Consider using the @ref async_read function if you need to
  389. * ensure that the requested amount of data is read before the asynchronous
  390. * operation completes.
  391. */
  392. template <typename MutableBufferSequence, typename ReadHandler>
  393. void async_read_some(const MutableBufferSequence& buffers,
  394. ReadHandler handler)
  395. {
  396. service_.async_read_some(impl_, next_layer_, buffers, handler);
  397. }
  398. /// Peek at the incoming data on the stream.
  399. /**
  400. * This function is used to peek at the incoming data on the stream, without
  401. * removing it from the input queue. The function call will block until data
  402. * has been read successfully or an error occurs.
  403. *
  404. * @param buffers The buffers into which the data will be read.
  405. *
  406. * @returns The number of bytes read.
  407. *
  408. * @throws asio::system_error Thrown on failure.
  409. */
  410. template <typename MutableBufferSequence>
  411. std::size_t peek(const MutableBufferSequence& buffers)
  412. {
  413. asio::error_code ec;
  414. std::size_t s = service_.peek(impl_, next_layer_, buffers, ec);
  415. asio::detail::throw_error(ec);
  416. return s;
  417. }
  418. /// Peek at the incoming data on the stream.
  419. /**
  420. * This function is used to peek at the incoming data on the stream, withoutxi
  421. * removing it from the input queue. The function call will block until data
  422. * has been read successfully or an error occurs.
  423. *
  424. * @param buffers The buffers into which the data will be read.
  425. *
  426. * @param ec Set to indicate what error occurred, if any.
  427. *
  428. * @returns The number of bytes read. Returns 0 if an error occurred.
  429. */
  430. template <typename MutableBufferSequence>
  431. std::size_t peek(const MutableBufferSequence& buffers,
  432. asio::error_code& ec)
  433. {
  434. return service_.peek(impl_, next_layer_, buffers, ec);
  435. }
  436. /// Determine the amount of data that may be read without blocking.
  437. /**
  438. * This function is used to determine the amount of data, in bytes, that may
  439. * be read from the stream without blocking.
  440. *
  441. * @returns The number of bytes of data that can be read without blocking.
  442. *
  443. * @throws asio::system_error Thrown on failure.
  444. */
  445. std::size_t in_avail()
  446. {
  447. asio::error_code ec;
  448. std::size_t s = service_.in_avail(impl_, next_layer_, ec);
  449. asio::detail::throw_error(ec);
  450. return s;
  451. }
  452. /// Determine the amount of data that may be read without blocking.
  453. /**
  454. * This function is used to determine the amount of data, in bytes, that may
  455. * be read from the stream without blocking.
  456. *
  457. * @param ec Set to indicate what error occurred, if any.
  458. *
  459. * @returns The number of bytes of data that can be read without blocking.
  460. */
  461. std::size_t in_avail(asio::error_code& ec)
  462. {
  463. return service_.in_avail(impl_, next_layer_, ec);
  464. }
  465. private:
  466. /// The next layer.
  467. Stream next_layer_;
  468. /// The backend service implementation.
  469. service_type& service_;
  470. /// The underlying native implementation.
  471. impl_type impl_;
  472. };
  473. } // namespace ssl
  474. } // namespace asio
  475. #include "asio/detail/pop_options.hpp"
  476. #endif // ASIO_SSL_STREAM_HPP