annotate DEPENDENCIES/generic/include/boost/asio/ssl/old/stream.hpp @ 125:34e428693f5d vext

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