stream.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. //
  2. // ssl/stream.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_SSL_STREAM_HPP
  11. #define BOOST_ASIO_SSL_STREAM_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. #include <boost/asio/async_result.hpp>
  17. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/non_const_lvalue.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/ssl/context.hpp>
  23. #include <boost/asio/ssl/detail/buffered_handshake_op.hpp>
  24. #include <boost/asio/ssl/detail/handshake_op.hpp>
  25. #include <boost/asio/ssl/detail/io.hpp>
  26. #include <boost/asio/ssl/detail/read_op.hpp>
  27. #include <boost/asio/ssl/detail/shutdown_op.hpp>
  28. #include <boost/asio/ssl/detail/stream_core.hpp>
  29. #include <boost/asio/ssl/detail/write_op.hpp>
  30. #include <boost/asio/ssl/stream_base.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace ssl {
  35. /// Provides stream-oriented functionality using SSL.
  36. /**
  37. * The stream class template provides asynchronous and blocking stream-oriented
  38. * functionality using SSL.
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe. The application must also ensure that all
  43. * asynchronous operations are performed within the same implicit or explicit
  44. * strand.
  45. *
  46. * @par Example
  47. * To use the SSL stream template with an ip::tcp::socket, you would write:
  48. * @code
  49. * boost::asio::io_context my_context;
  50. * boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
  51. * boost::asio::ssl::stream<asio:ip::tcp::socket> sock(my_context, ctx);
  52. * @endcode
  53. *
  54. * @par Concepts:
  55. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  56. */
  57. template <typename Stream>
  58. class stream :
  59. public stream_base,
  60. private noncopyable
  61. {
  62. public:
  63. /// The native handle type of the SSL stream.
  64. typedef SSL* native_handle_type;
  65. /// Structure for use with deprecated impl_type.
  66. struct impl_struct
  67. {
  68. SSL* ssl;
  69. };
  70. /// The type of the next layer.
  71. typedef typename remove_reference<Stream>::type next_layer_type;
  72. /// The type of the lowest layer.
  73. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  74. /// The type of the executor associated with the object.
  75. typedef typename lowest_layer_type::executor_type executor_type;
  76. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  77. /// Construct a stream.
  78. /**
  79. * This constructor creates a stream and initialises the underlying stream
  80. * object.
  81. *
  82. * @param arg The argument to be passed to initialise the underlying stream.
  83. *
  84. * @param ctx The SSL context to be used for the stream.
  85. */
  86. template <typename Arg>
  87. stream(Arg&& arg, context& ctx)
  88. : next_layer_(BOOST_ASIO_MOVE_CAST(Arg)(arg)),
  89. core_(ctx.native_handle(), next_layer_.lowest_layer().get_executor())
  90. {
  91. }
  92. #else // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  93. template <typename Arg>
  94. stream(Arg& arg, context& ctx)
  95. : next_layer_(arg),
  96. core_(ctx.native_handle(), next_layer_.lowest_layer().get_executor())
  97. {
  98. }
  99. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  100. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  101. /// Move-construct a stream from another.
  102. /**
  103. * @param other The other stream object from which the move will occur. Must
  104. * have no outstanding asynchronous operations associated with it. Following
  105. * the move, @c other has a valid but unspecified state where the only safe
  106. * operation is destruction.
  107. */
  108. stream(stream&& other)
  109. : next_layer_(BOOST_ASIO_MOVE_CAST(Stream)(other.next_layer_)),
  110. core_(BOOST_ASIO_MOVE_CAST(detail::stream_core)(other.core_))
  111. {
  112. }
  113. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  114. /// Destructor.
  115. /**
  116. * @note A @c stream object must not be destroyed while there are pending
  117. * asynchronous operations associated with it.
  118. */
  119. ~stream()
  120. {
  121. }
  122. /// Get the executor associated with the object.
  123. /**
  124. * This function may be used to obtain the executor object that the stream
  125. * uses to dispatch handlers for asynchronous operations.
  126. *
  127. * @return A copy of the executor that stream will use to dispatch handlers.
  128. */
  129. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  130. {
  131. return next_layer_.lowest_layer().get_executor();
  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 context functionality that is
  137. * not otherwise provided.
  138. *
  139. * @par Example
  140. * The native_handle() function returns a pointer of type @c SSL* that is
  141. * suitable for passing to functions such as @c SSL_get_verify_result and
  142. * @c SSL_get_peer_certificate:
  143. * @code
  144. * boost::asio::ssl::stream<asio:ip::tcp::socket> sock(my_context, ctx);
  145. *
  146. * // ... establish connection and perform handshake ...
  147. *
  148. * if (X509* cert = SSL_get_peer_certificate(sock.native_handle()))
  149. * {
  150. * if (SSL_get_verify_result(sock.native_handle()) == X509_V_OK)
  151. * {
  152. * // ...
  153. * }
  154. * }
  155. * @endcode
  156. */
  157. native_handle_type native_handle()
  158. {
  159. return core_.engine_.native_handle();
  160. }
  161. /// Get a reference to the next layer.
  162. /**
  163. * This function returns a reference to the next layer in a stack of stream
  164. * layers.
  165. *
  166. * @return A reference to the next layer in the stack of stream layers.
  167. * Ownership is not transferred to the caller.
  168. */
  169. const next_layer_type& next_layer() const
  170. {
  171. return next_layer_;
  172. }
  173. /// Get a reference to the next layer.
  174. /**
  175. * This function returns a reference to the next layer in a stack of stream
  176. * layers.
  177. *
  178. * @return A reference to the next layer in the stack of stream layers.
  179. * Ownership is not transferred to the caller.
  180. */
  181. next_layer_type& next_layer()
  182. {
  183. return next_layer_;
  184. }
  185. /// Get a reference to the lowest layer.
  186. /**
  187. * This function returns a reference to the lowest layer in a stack of
  188. * stream layers.
  189. *
  190. * @return A reference to the lowest layer in the stack of stream layers.
  191. * Ownership is not transferred to the caller.
  192. */
  193. lowest_layer_type& lowest_layer()
  194. {
  195. return next_layer_.lowest_layer();
  196. }
  197. /// Get a reference to the lowest layer.
  198. /**
  199. * This function returns a reference to the lowest layer in a stack of
  200. * stream layers.
  201. *
  202. * @return A reference to the lowest layer in the stack of stream layers.
  203. * Ownership is not transferred to the caller.
  204. */
  205. const lowest_layer_type& lowest_layer() const
  206. {
  207. return next_layer_.lowest_layer();
  208. }
  209. /// Set the peer verification mode.
  210. /**
  211. * This function may be used to configure the peer verification mode used by
  212. * the stream. The new mode will override the mode inherited from the context.
  213. *
  214. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  215. * available values.
  216. *
  217. * @throws boost::system::system_error Thrown on failure.
  218. *
  219. * @note Calls @c SSL_set_verify.
  220. */
  221. void set_verify_mode(verify_mode v)
  222. {
  223. boost::system::error_code ec;
  224. set_verify_mode(v, ec);
  225. boost::asio::detail::throw_error(ec, "set_verify_mode");
  226. }
  227. /// Set the peer verification mode.
  228. /**
  229. * This function may be used to configure the peer verification mode used by
  230. * the stream. The new mode will override the mode inherited from the context.
  231. *
  232. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  233. * available values.
  234. *
  235. * @param ec Set to indicate what error occurred, if any.
  236. *
  237. * @note Calls @c SSL_set_verify.
  238. */
  239. BOOST_ASIO_SYNC_OP_VOID set_verify_mode(
  240. verify_mode v, boost::system::error_code& ec)
  241. {
  242. core_.engine_.set_verify_mode(v, ec);
  243. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  244. }
  245. /// Set the peer verification depth.
  246. /**
  247. * This function may be used to configure the maximum verification depth
  248. * allowed by the stream.
  249. *
  250. * @param depth Maximum depth for the certificate chain verification that
  251. * shall be allowed.
  252. *
  253. * @throws boost::system::system_error Thrown on failure.
  254. *
  255. * @note Calls @c SSL_set_verify_depth.
  256. */
  257. void set_verify_depth(int depth)
  258. {
  259. boost::system::error_code ec;
  260. set_verify_depth(depth, ec);
  261. boost::asio::detail::throw_error(ec, "set_verify_depth");
  262. }
  263. /// Set the peer verification depth.
  264. /**
  265. * This function may be used to configure the maximum verification depth
  266. * allowed by the stream.
  267. *
  268. * @param depth Maximum depth for the certificate chain verification that
  269. * shall be allowed.
  270. *
  271. * @param ec Set to indicate what error occurred, if any.
  272. *
  273. * @note Calls @c SSL_set_verify_depth.
  274. */
  275. BOOST_ASIO_SYNC_OP_VOID set_verify_depth(
  276. int depth, boost::system::error_code& ec)
  277. {
  278. core_.engine_.set_verify_depth(depth, ec);
  279. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  280. }
  281. /// Set the callback used to verify peer certificates.
  282. /**
  283. * This function is used to specify a callback function that will be called
  284. * by the implementation when it needs to verify a peer certificate.
  285. *
  286. * @param callback The function object to be used for verifying a certificate.
  287. * The function signature of the handler must be:
  288. * @code bool verify_callback(
  289. * bool preverified, // True if the certificate passed pre-verification.
  290. * verify_context& ctx // The peer certificate and other context.
  291. * ); @endcode
  292. * The return value of the callback is true if the certificate has passed
  293. * verification, false otherwise.
  294. *
  295. * @throws boost::system::system_error Thrown on failure.
  296. *
  297. * @note Calls @c SSL_set_verify.
  298. */
  299. template <typename VerifyCallback>
  300. void set_verify_callback(VerifyCallback callback)
  301. {
  302. boost::system::error_code ec;
  303. this->set_verify_callback(callback, ec);
  304. boost::asio::detail::throw_error(ec, "set_verify_callback");
  305. }
  306. /// Set the callback used to verify peer certificates.
  307. /**
  308. * This function is used to specify a callback function that will be called
  309. * by the implementation when it needs to verify a peer certificate.
  310. *
  311. * @param callback The function object to be used for verifying a certificate.
  312. * The function signature of the handler must be:
  313. * @code bool verify_callback(
  314. * bool preverified, // True if the certificate passed pre-verification.
  315. * verify_context& ctx // The peer certificate and other context.
  316. * ); @endcode
  317. * The return value of the callback is true if the certificate has passed
  318. * verification, false otherwise.
  319. *
  320. * @param ec Set to indicate what error occurred, if any.
  321. *
  322. * @note Calls @c SSL_set_verify.
  323. */
  324. template <typename VerifyCallback>
  325. BOOST_ASIO_SYNC_OP_VOID set_verify_callback(VerifyCallback callback,
  326. boost::system::error_code& ec)
  327. {
  328. core_.engine_.set_verify_callback(
  329. new detail::verify_callback<VerifyCallback>(callback), ec);
  330. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  331. }
  332. /// Perform SSL handshaking.
  333. /**
  334. * This function is used to perform SSL handshaking on the stream. The
  335. * function call will block until handshaking is complete or an error occurs.
  336. *
  337. * @param type The type of handshaking to be performed, i.e. as a client or as
  338. * a server.
  339. *
  340. * @throws boost::system::system_error Thrown on failure.
  341. */
  342. void handshake(handshake_type type)
  343. {
  344. boost::system::error_code ec;
  345. handshake(type, ec);
  346. boost::asio::detail::throw_error(ec, "handshake");
  347. }
  348. /// Perform SSL handshaking.
  349. /**
  350. * This function is used to perform SSL handshaking on the stream. The
  351. * function call will block until handshaking is complete or an error occurs.
  352. *
  353. * @param type The type of handshaking to be performed, i.e. as a client or as
  354. * a server.
  355. *
  356. * @param ec Set to indicate what error occurred, if any.
  357. */
  358. BOOST_ASIO_SYNC_OP_VOID handshake(handshake_type type,
  359. boost::system::error_code& ec)
  360. {
  361. detail::io(next_layer_, core_, detail::handshake_op(type), ec);
  362. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  363. }
  364. /// Perform SSL handshaking.
  365. /**
  366. * This function is used to perform SSL handshaking on the stream. The
  367. * function call will block until handshaking is complete or an error occurs.
  368. *
  369. * @param type The type of handshaking to be performed, i.e. as a client or as
  370. * a server.
  371. *
  372. * @param buffers The buffered data to be reused for the handshake.
  373. *
  374. * @throws boost::system::system_error Thrown on failure.
  375. */
  376. template <typename ConstBufferSequence>
  377. void handshake(handshake_type type, const ConstBufferSequence& buffers)
  378. {
  379. boost::system::error_code ec;
  380. handshake(type, buffers, ec);
  381. boost::asio::detail::throw_error(ec, "handshake");
  382. }
  383. /// Perform SSL handshaking.
  384. /**
  385. * This function is used to perform SSL handshaking on the stream. The
  386. * function call will block until handshaking is complete or an error occurs.
  387. *
  388. * @param type The type of handshaking to be performed, i.e. as a client or as
  389. * a server.
  390. *
  391. * @param buffers The buffered data to be reused for the handshake.
  392. *
  393. * @param ec Set to indicate what error occurred, if any.
  394. */
  395. template <typename ConstBufferSequence>
  396. BOOST_ASIO_SYNC_OP_VOID handshake(handshake_type type,
  397. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  398. {
  399. detail::io(next_layer_, core_,
  400. detail::buffered_handshake_op<ConstBufferSequence>(type, buffers), ec);
  401. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  402. }
  403. /// Start an asynchronous SSL handshake.
  404. /**
  405. * This function is used to asynchronously perform an SSL handshake on the
  406. * stream. This function call always returns immediately.
  407. *
  408. * @param type The type of handshaking to be performed, i.e. as a client or as
  409. * a server.
  410. *
  411. * @param handler The handler to be called when the handshake operation
  412. * completes. Copies will be made of the handler as required. The equivalent
  413. * function signature of the handler must be:
  414. * @code void handler(
  415. * const boost::system::error_code& error // Result of operation.
  416. * ); @endcode
  417. */
  418. template <
  419. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  420. HandshakeHandler
  421. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  422. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(HandshakeHandler,
  423. void (boost::system::error_code))
  424. async_handshake(handshake_type type,
  425. BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler
  426. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  427. {
  428. return async_initiate<HandshakeHandler,
  429. void (boost::system::error_code)>(
  430. initiate_async_handshake(this), handler, type);
  431. }
  432. /// Start an asynchronous SSL handshake.
  433. /**
  434. * This function is used to asynchronously perform an SSL handshake on the
  435. * stream. This function call always returns immediately.
  436. *
  437. * @param type The type of handshaking to be performed, i.e. as a client or as
  438. * a server.
  439. *
  440. * @param buffers The buffered data to be reused for the handshake. Although
  441. * the buffers object may be copied as necessary, ownership of the underlying
  442. * buffers is retained by the caller, which must guarantee that they remain
  443. * valid until the handler is called.
  444. *
  445. * @param handler The handler to be called when the handshake operation
  446. * completes. Copies will be made of the handler as required. The equivalent
  447. * function signature of the handler must be:
  448. * @code void handler(
  449. * const boost::system::error_code& error, // Result of operation.
  450. * std::size_t bytes_transferred // Amount of buffers used in handshake.
  451. * ); @endcode
  452. */
  453. template <typename ConstBufferSequence,
  454. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  455. std::size_t)) BufferedHandshakeHandler
  456. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  457. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(BufferedHandshakeHandler,
  458. void (boost::system::error_code, std::size_t))
  459. async_handshake(handshake_type type, const ConstBufferSequence& buffers,
  460. BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler
  461. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  462. {
  463. return async_initiate<BufferedHandshakeHandler,
  464. void (boost::system::error_code, std::size_t)>(
  465. initiate_async_buffered_handshake(this), handler, type, buffers);
  466. }
  467. /// Shut down SSL on the stream.
  468. /**
  469. * This function is used to shut down SSL on the stream. The function call
  470. * will block until SSL has been shut down or an error occurs.
  471. *
  472. * @throws boost::system::system_error Thrown on failure.
  473. */
  474. void shutdown()
  475. {
  476. boost::system::error_code ec;
  477. shutdown(ec);
  478. boost::asio::detail::throw_error(ec, "shutdown");
  479. }
  480. /// Shut down SSL on the stream.
  481. /**
  482. * This function is used to shut down SSL on the stream. The function call
  483. * will block until SSL has been shut down or an error occurs.
  484. *
  485. * @param ec Set to indicate what error occurred, if any.
  486. */
  487. BOOST_ASIO_SYNC_OP_VOID shutdown(boost::system::error_code& ec)
  488. {
  489. detail::io(next_layer_, core_, detail::shutdown_op(), ec);
  490. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  491. }
  492. /// Asynchronously shut down SSL on the stream.
  493. /**
  494. * This function is used to asynchronously shut down SSL on the stream. This
  495. * function call always returns immediately.
  496. *
  497. * @param handler The handler to be called when the handshake operation
  498. * completes. Copies will be made of the handler as required. The equivalent
  499. * function signature of the handler must be:
  500. * @code void handler(
  501. * const boost::system::error_code& error // Result of operation.
  502. * ); @endcode
  503. */
  504. template <
  505. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  506. ShutdownHandler
  507. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  508. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ShutdownHandler,
  509. void (boost::system::error_code))
  510. async_shutdown(
  511. BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler
  512. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  513. {
  514. return async_initiate<ShutdownHandler,
  515. void (boost::system::error_code)>(
  516. initiate_async_shutdown(this), handler);
  517. }
  518. /// Write some data to the stream.
  519. /**
  520. * This function is used to write data on the stream. The function call will
  521. * block until one or more bytes of data has been written successfully, or
  522. * until an error occurs.
  523. *
  524. * @param buffers The data to be written.
  525. *
  526. * @returns The number of bytes written.
  527. *
  528. * @throws boost::system::system_error Thrown on failure.
  529. *
  530. * @note The write_some operation may not transmit all of the data to the
  531. * peer. Consider using the @ref write function if you need to ensure that all
  532. * data is written before the blocking operation completes.
  533. */
  534. template <typename ConstBufferSequence>
  535. std::size_t write_some(const ConstBufferSequence& buffers)
  536. {
  537. boost::system::error_code ec;
  538. std::size_t n = write_some(buffers, ec);
  539. boost::asio::detail::throw_error(ec, "write_some");
  540. return n;
  541. }
  542. /// Write some data to the stream.
  543. /**
  544. * This function is used to write data on the stream. The function call will
  545. * block until one or more bytes of data has been written successfully, or
  546. * until an error occurs.
  547. *
  548. * @param buffers The data to be written to the stream.
  549. *
  550. * @param ec Set to indicate what error occurred, if any.
  551. *
  552. * @returns The number of bytes written. Returns 0 if an error occurred.
  553. *
  554. * @note The write_some operation may not transmit all of the data to the
  555. * peer. Consider using the @ref write function if you need to ensure that all
  556. * data is written before the blocking operation completes.
  557. */
  558. template <typename ConstBufferSequence>
  559. std::size_t write_some(const ConstBufferSequence& buffers,
  560. boost::system::error_code& ec)
  561. {
  562. return detail::io(next_layer_, core_,
  563. detail::write_op<ConstBufferSequence>(buffers), ec);
  564. }
  565. /// Start an asynchronous write.
  566. /**
  567. * This function is used to asynchronously write one or more bytes of data to
  568. * the stream. The function call always returns immediately.
  569. *
  570. * @param buffers The data to be written to the stream. Although the buffers
  571. * object may be copied as necessary, ownership of the underlying buffers is
  572. * retained by the caller, which must guarantee that they remain valid until
  573. * the handler is called.
  574. *
  575. * @param handler The handler to be called when the write operation completes.
  576. * Copies will be made of the handler as required. The equivalent function
  577. * signature of the handler must be:
  578. * @code void handler(
  579. * const boost::system::error_code& error, // Result of operation.
  580. * std::size_t bytes_transferred // Number of bytes written.
  581. * ); @endcode
  582. *
  583. * @note The async_write_some operation may not transmit all of the data to
  584. * the peer. Consider using the @ref async_write function if you need to
  585. * ensure that all data is written before the asynchronous operation
  586. * completes.
  587. */
  588. template <typename ConstBufferSequence,
  589. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  590. std::size_t)) WriteHandler
  591. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  592. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  593. void (boost::system::error_code, std::size_t))
  594. async_write_some(const ConstBufferSequence& buffers,
  595. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  596. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  597. {
  598. return async_initiate<WriteHandler,
  599. void (boost::system::error_code, std::size_t)>(
  600. initiate_async_write_some(this), handler, buffers);
  601. }
  602. /// Read some data from the stream.
  603. /**
  604. * This function is used to read data from the stream. The function call will
  605. * block until one or more bytes of data has been read successfully, or until
  606. * an error occurs.
  607. *
  608. * @param buffers The buffers into which the data will be read.
  609. *
  610. * @returns The number of bytes read.
  611. *
  612. * @throws boost::system::system_error Thrown on failure.
  613. *
  614. * @note The read_some operation may not read all of the requested number of
  615. * bytes. Consider using the @ref read function if you need to ensure that the
  616. * requested amount of data is read before the blocking operation completes.
  617. */
  618. template <typename MutableBufferSequence>
  619. std::size_t read_some(const MutableBufferSequence& buffers)
  620. {
  621. boost::system::error_code ec;
  622. std::size_t n = read_some(buffers, ec);
  623. boost::asio::detail::throw_error(ec, "read_some");
  624. return n;
  625. }
  626. /// Read some data from the stream.
  627. /**
  628. * This function is used to read data from the stream. The function call will
  629. * block until one or more bytes of data has been read successfully, or until
  630. * an error occurs.
  631. *
  632. * @param buffers The buffers into which the data will be read.
  633. *
  634. * @param ec Set to indicate what error occurred, if any.
  635. *
  636. * @returns The number of bytes read. Returns 0 if an error occurred.
  637. *
  638. * @note The read_some operation may not read all of the requested number of
  639. * bytes. Consider using the @ref read function if you need to ensure that the
  640. * requested amount of data is read before the blocking operation completes.
  641. */
  642. template <typename MutableBufferSequence>
  643. std::size_t read_some(const MutableBufferSequence& buffers,
  644. boost::system::error_code& ec)
  645. {
  646. return detail::io(next_layer_, core_,
  647. detail::read_op<MutableBufferSequence>(buffers), ec);
  648. }
  649. /// Start an asynchronous read.
  650. /**
  651. * This function is used to asynchronously read one or more bytes of data from
  652. * the stream. The function call always returns immediately.
  653. *
  654. * @param buffers The buffers into which the data will be read. Although the
  655. * buffers object may be copied as necessary, ownership of the underlying
  656. * buffers is retained by the caller, which must guarantee that they remain
  657. * valid until the handler is called.
  658. *
  659. * @param handler The handler to be called when the read operation completes.
  660. * Copies will be made of the handler as required. The equivalent function
  661. * signature of the handler must be:
  662. * @code void handler(
  663. * const boost::system::error_code& error, // Result of operation.
  664. * std::size_t bytes_transferred // Number of bytes read.
  665. * ); @endcode
  666. *
  667. * @note The async_read_some operation may not read all of the requested
  668. * number of bytes. Consider using the @ref async_read function if you need to
  669. * ensure that the requested amount of data is read before the asynchronous
  670. * operation completes.
  671. */
  672. template <typename MutableBufferSequence,
  673. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  674. std::size_t)) ReadHandler
  675. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  676. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  677. void (boost::system::error_code, std::size_t))
  678. async_read_some(const MutableBufferSequence& buffers,
  679. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  680. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  681. {
  682. return async_initiate<ReadHandler,
  683. void (boost::system::error_code, std::size_t)>(
  684. initiate_async_read_some(this), handler, buffers);
  685. }
  686. private:
  687. class initiate_async_handshake
  688. {
  689. public:
  690. typedef typename stream::executor_type executor_type;
  691. explicit initiate_async_handshake(stream* self)
  692. : self_(self)
  693. {
  694. }
  695. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  696. {
  697. return self_->get_executor();
  698. }
  699. template <typename HandshakeHandler>
  700. void operator()(BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler,
  701. handshake_type type) const
  702. {
  703. // If you get an error on the following line it means that your handler
  704. // does not meet the documented type requirements for a HandshakeHandler.
  705. BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;
  706. boost::asio::detail::non_const_lvalue<HandshakeHandler> handler2(handler);
  707. detail::async_io(self_->next_layer_, self_->core_,
  708. detail::handshake_op(type), handler2.value);
  709. }
  710. private:
  711. stream* self_;
  712. };
  713. class initiate_async_buffered_handshake
  714. {
  715. public:
  716. typedef typename stream::executor_type executor_type;
  717. explicit initiate_async_buffered_handshake(stream* self)
  718. : self_(self)
  719. {
  720. }
  721. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  722. {
  723. return self_->get_executor();
  724. }
  725. template <typename BufferedHandshakeHandler, typename ConstBufferSequence>
  726. void operator()(BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler,
  727. handshake_type type, const ConstBufferSequence& buffers) const
  728. {
  729. // If you get an error on the following line it means that your
  730. // handler does not meet the documented type requirements for a
  731. // BufferedHandshakeHandler.
  732. BOOST_ASIO_BUFFERED_HANDSHAKE_HANDLER_CHECK(
  733. BufferedHandshakeHandler, handler) type_check;
  734. boost::asio::detail::non_const_lvalue<
  735. BufferedHandshakeHandler> handler2(handler);
  736. detail::async_io(self_->next_layer_, self_->core_,
  737. detail::buffered_handshake_op<ConstBufferSequence>(type, buffers),
  738. handler2.value);
  739. }
  740. private:
  741. stream* self_;
  742. };
  743. class initiate_async_shutdown
  744. {
  745. public:
  746. typedef typename stream::executor_type executor_type;
  747. explicit initiate_async_shutdown(stream* self)
  748. : self_(self)
  749. {
  750. }
  751. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  752. {
  753. return self_->get_executor();
  754. }
  755. template <typename ShutdownHandler>
  756. void operator()(BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler) const
  757. {
  758. // If you get an error on the following line it means that your handler
  759. // does not meet the documented type requirements for a ShutdownHandler.
  760. BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(ShutdownHandler, handler) type_check;
  761. boost::asio::detail::non_const_lvalue<ShutdownHandler> handler2(handler);
  762. detail::async_io(self_->next_layer_, self_->core_,
  763. detail::shutdown_op(), handler2.value);
  764. }
  765. private:
  766. stream* self_;
  767. };
  768. class initiate_async_write_some
  769. {
  770. public:
  771. typedef typename stream::executor_type executor_type;
  772. explicit initiate_async_write_some(stream* self)
  773. : self_(self)
  774. {
  775. }
  776. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  777. {
  778. return self_->get_executor();
  779. }
  780. template <typename WriteHandler, typename ConstBufferSequence>
  781. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  782. const ConstBufferSequence& buffers) const
  783. {
  784. // If you get an error on the following line it means that your handler
  785. // does not meet the documented type requirements for a WriteHandler.
  786. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  787. boost::asio::detail::non_const_lvalue<WriteHandler> handler2(handler);
  788. detail::async_io(self_->next_layer_, self_->core_,
  789. detail::write_op<ConstBufferSequence>(buffers), handler2.value);
  790. }
  791. private:
  792. stream* self_;
  793. };
  794. class initiate_async_read_some
  795. {
  796. public:
  797. typedef typename stream::executor_type executor_type;
  798. explicit initiate_async_read_some(stream* self)
  799. : self_(self)
  800. {
  801. }
  802. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  803. {
  804. return self_->get_executor();
  805. }
  806. template <typename ReadHandler, typename MutableBufferSequence>
  807. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  808. const MutableBufferSequence& buffers) const
  809. {
  810. // If you get an error on the following line it means that your handler
  811. // does not meet the documented type requirements for a ReadHandler.
  812. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  813. boost::asio::detail::non_const_lvalue<ReadHandler> handler2(handler);
  814. detail::async_io(self_->next_layer_, self_->core_,
  815. detail::read_op<MutableBufferSequence>(buffers), handler2.value);
  816. }
  817. private:
  818. stream* self_;
  819. };
  820. Stream next_layer_;
  821. detail::stream_core core_;
  822. };
  823. } // namespace ssl
  824. } // namespace asio
  825. } // namespace boost
  826. #include <boost/asio/detail/pop_options.hpp>
  827. #endif // BOOST_ASIO_SSL_STREAM_HPP