stream.hpp 16 KB

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