fix doc example typo
[boost.git] / boost / asio / ssl / stream.hpp
blob448aa917f88c927ebf07486001ac17edb214a28b
1 //
2 // stream.hpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6 // Copyright (c) 2005-2008 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)
12 #ifndef BOOST_ASIO_SSL_STREAM_HPP
13 #define BOOST_ASIO_SSL_STREAM_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/asio/detail/push_options.hpp>
22 #include <cstddef>
23 #include <boost/config.hpp>
24 #include <boost/noncopyable.hpp>
25 #include <boost/type_traits.hpp>
26 #include <boost/asio/detail/pop_options.hpp>
28 #include <boost/asio/error.hpp>
29 #include <boost/asio/ssl/basic_context.hpp>
30 #include <boost/asio/ssl/stream_base.hpp>
31 #include <boost/asio/ssl/stream_service.hpp>
32 #include <boost/asio/detail/throw_error.hpp>
34 namespace boost {
35 namespace asio {
36 namespace ssl {
38 /// Provides stream-oriented functionality using SSL.
39 /**
40 * The stream class template provides asynchronous and blocking stream-oriented
41 * functionality using SSL.
43 * @par Thread Safety
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
47 * @par Example
48 * To use the SSL stream template with an ip::tcp::socket, you would write:
49 * @code
50 * boost::asio::io_service io_service;
51 * boost::asio::ssl::context context(io_service, boost::asio::ssl::context::sslv23);
52 * boost::asio::ssl::stream<boost::asio::ip::tcp::socket> sock(io_service, context);
53 * @endcode
55 * @par Concepts:
56 * AsyncReadStream, AsyncWriteStream, Stream, SyncRead_Stream, SyncWriteStream.
58 template <typename Stream, typename Service = stream_service>
59 class stream
60 : public stream_base,
61 private boost::noncopyable
63 public:
64 /// The type of the next layer.
65 typedef typename boost::remove_reference<Stream>::type next_layer_type;
67 /// The type of the lowest layer.
68 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
70 /// The type of the service that will be used to provide stream operations.
71 typedef Service service_type;
73 /// The native implementation type of the stream.
74 typedef typename service_type::impl_type impl_type;
76 /// Construct a stream.
77 /**
78 * This constructor creates a stream and initialises the underlying stream
79 * object.
81 * @param arg The argument to be passed to initialise the underlying stream.
83 * @param context The SSL context to be used for the stream.
85 template <typename Arg, typename Context_Service>
86 explicit stream(Arg& arg, basic_context<Context_Service>& context)
87 : next_layer_(arg),
88 service_(boost::asio::use_service<Service>(next_layer_.get_io_service())),
89 impl_(service_.null())
91 service_.create(impl_, next_layer_, context);
94 /// Destructor.
95 ~stream()
97 service_.destroy(impl_, next_layer_);
100 /// (Deprecated: use get_io_service().) Get the io_service associated with
101 /// the object.
103 * This function may be used to obtain the io_service object that the stream
104 * uses to dispatch handlers for asynchronous operations.
106 * @return A reference to the io_service object that stream will use to
107 * dispatch handlers. Ownership is not transferred to the caller.
109 boost::asio::io_service& io_service()
111 return next_layer_.get_io_service();
114 /// Get the io_service associated with the object.
116 * This function may be used to obtain the io_service object that the stream
117 * uses to dispatch handlers for asynchronous operations.
119 * @return A reference to the io_service object that stream will use to
120 * dispatch handlers. Ownership is not transferred to the caller.
122 boost::asio::io_service& get_io_service()
124 return next_layer_.get_io_service();
127 /// Get a reference to the next layer.
129 * This function returns a reference to the next layer in a stack of stream
130 * layers.
132 * @return A reference to the next layer in the stack of stream layers.
133 * Ownership is not transferred to the caller.
135 next_layer_type& next_layer()
137 return next_layer_;
140 /// Get a reference to the lowest layer.
142 * This function returns a reference to the lowest layer in a stack of
143 * stream layers.
145 * @return A reference to the lowest layer in the stack of stream layers.
146 * Ownership is not transferred to the caller.
148 lowest_layer_type& lowest_layer()
150 return next_layer_.lowest_layer();
153 /// Get a const reference to the lowest layer.
155 * This function returns a const reference to the lowest layer in a stack of
156 * stream layers.
158 * @return A const reference to the lowest layer in the stack of stream
159 * layers. Ownership is not transferred to the caller.
161 const lowest_layer_type& lowest_layer() const
163 return next_layer_.lowest_layer();
166 /// Get the underlying implementation in the native type.
168 * This function may be used to obtain the underlying implementation of the
169 * context. This is intended to allow access to stream functionality that is
170 * not otherwise provided.
172 impl_type impl()
174 return impl_;
177 /// Perform SSL handshaking.
179 * This function is used to perform SSL handshaking on the stream. The
180 * function call will block until handshaking is complete or an error occurs.
182 * @param type The type of handshaking to be performed, i.e. as a client or as
183 * a server.
185 * @throws boost::system::system_error Thrown on failure.
187 void handshake(handshake_type type)
189 boost::system::error_code ec;
190 service_.handshake(impl_, next_layer_, type, ec);
191 boost::asio::detail::throw_error(ec);
194 /// Perform SSL handshaking.
196 * This function is used to perform SSL handshaking on the stream. The
197 * function call will block until handshaking is complete or an error occurs.
199 * @param type The type of handshaking to be performed, i.e. as a client or as
200 * a server.
202 * @param ec Set to indicate what error occurred, if any.
204 boost::system::error_code handshake(handshake_type type,
205 boost::system::error_code& ec)
207 return service_.handshake(impl_, next_layer_, type, ec);
210 /// Start an asynchronous SSL handshake.
212 * This function is used to asynchronously perform an SSL handshake on the
213 * stream. This function call always returns immediately.
215 * @param type The type of handshaking to be performed, i.e. as a client or as
216 * a server.
218 * @param handler The handler to be called when the handshake operation
219 * completes. Copies will be made of the handler as required. The equivalent
220 * function signature of the handler must be:
221 * @code void handler(
222 * const boost::system::error_code& error // Result of operation.
223 * ); @endcode
225 template <typename HandshakeHandler>
226 void async_handshake(handshake_type type, HandshakeHandler handler)
228 service_.async_handshake(impl_, next_layer_, type, handler);
231 /// Shut down SSL on the stream.
233 * This function is used to shut down SSL on the stream. The function call
234 * will block until SSL has been shut down or an error occurs.
236 * @throws boost::system::system_error Thrown on failure.
238 void shutdown()
240 boost::system::error_code ec;
241 service_.shutdown(impl_, next_layer_, ec);
242 boost::asio::detail::throw_error(ec);
245 /// Shut down SSL on the stream.
247 * This function is used to shut down SSL on the stream. The function call
248 * will block until SSL has been shut down or an error occurs.
250 * @param ec Set to indicate what error occurred, if any.
252 boost::system::error_code shutdown(boost::system::error_code& ec)
254 return service_.shutdown(impl_, next_layer_, ec);
257 /// Asynchronously shut down SSL on the stream.
259 * This function is used to asynchronously shut down SSL on the stream. This
260 * function call always returns immediately.
262 * @param handler The handler to be called when the handshake operation
263 * completes. Copies will be made of the handler as required. The equivalent
264 * function signature of the handler must be:
265 * @code void handler(
266 * const boost::system::error_code& error // Result of operation.
267 * ); @endcode
269 template <typename ShutdownHandler>
270 void async_shutdown(ShutdownHandler handler)
272 service_.async_shutdown(impl_, next_layer_, handler);
275 /// Write some data to the stream.
277 * This function is used to write data on the stream. The function call will
278 * block until one or more bytes of data has been written successfully, or
279 * until an error occurs.
281 * @param buffers The data to be written.
283 * @returns The number of bytes written.
285 * @throws boost::system::system_error Thrown on failure.
287 * @note The write_some operation may not transmit all of the data to the
288 * peer. Consider using the @ref write function if you need to ensure that all
289 * data is written before the blocking operation completes.
291 template <typename ConstBufferSequence>
292 std::size_t write_some(const ConstBufferSequence& buffers)
294 boost::system::error_code ec;
295 std::size_t s = service_.write_some(impl_, next_layer_, buffers, ec);
296 boost::asio::detail::throw_error(ec);
297 return s;
300 /// Write some data to the stream.
302 * This function is used to write data on the stream. The function call will
303 * block until one or more bytes of data has been written successfully, or
304 * until an error occurs.
306 * @param buffers The data to be written to the stream.
308 * @param ec Set to indicate what error occurred, if any.
310 * @returns The number of bytes written. Returns 0 if an error occurred.
312 * @note The write_some operation may not transmit all of the data to the
313 * peer. Consider using the @ref write function if you need to ensure that all
314 * data is written before the blocking operation completes.
316 template <typename ConstBufferSequence>
317 std::size_t write_some(const ConstBufferSequence& buffers,
318 boost::system::error_code& ec)
320 return service_.write_some(impl_, next_layer_, buffers, ec);
323 /// Start an asynchronous write.
325 * This function is used to asynchronously write one or more bytes of data to
326 * the stream. The function call always returns immediately.
328 * @param buffers The data to be written to the stream. Although the buffers
329 * object may be copied as necessary, ownership of the underlying buffers is
330 * retained by the caller, which must guarantee that they remain valid until
331 * the handler is called.
333 * @param handler The handler to be called when the write operation completes.
334 * Copies will be made of the handler as required. The equivalent function
335 * signature of the handler must be:
336 * @code void handler(
337 * const boost::system::error_code& error, // Result of operation.
338 * std::size_t bytes_transferred // Number of bytes written.
339 * ); @endcode
341 * @note The async_write_some operation may not transmit all of the data to
342 * the peer. Consider using the @ref async_write function if you need to
343 * ensure that all data is written before the blocking operation completes.
345 template <typename ConstBufferSequence, typename WriteHandler>
346 void async_write_some(const ConstBufferSequence& buffers,
347 WriteHandler handler)
349 service_.async_write_some(impl_, next_layer_, buffers, handler);
352 /// Read some data from the stream.
354 * This function is used to read data from the stream. The function call will
355 * block until one or more bytes of data has been read successfully, or until
356 * an error occurs.
358 * @param buffers The buffers into which the data will be read.
360 * @returns The number of bytes read.
362 * @throws boost::system::system_error Thrown on failure.
364 * @note The read_some operation may not read all of the requested number of
365 * bytes. Consider using the @ref read function if you need to ensure that the
366 * requested amount of data is read before the blocking operation completes.
368 template <typename MutableBufferSequence>
369 std::size_t read_some(const MutableBufferSequence& buffers)
371 boost::system::error_code ec;
372 std::size_t s = service_.read_some(impl_, next_layer_, buffers, ec);
373 boost::asio::detail::throw_error(ec);
374 return s;
377 /// Read some data from the stream.
379 * This function is used to read data from the stream. The function call will
380 * block until one or more bytes of data has been read successfully, or until
381 * an error occurs.
383 * @param buffers The buffers into which the data will be read.
385 * @param ec Set to indicate what error occurred, if any.
387 * @returns The number of bytes read. Returns 0 if an error occurred.
389 * @note The read_some operation may not read all of the requested number of
390 * bytes. Consider using the @ref read function if you need to ensure that the
391 * requested amount of data is read before the blocking operation completes.
393 template <typename MutableBufferSequence>
394 std::size_t read_some(const MutableBufferSequence& buffers,
395 boost::system::error_code& ec)
397 return service_.read_some(impl_, next_layer_, buffers, ec);
400 /// Start an asynchronous read.
402 * This function is used to asynchronously read one or more bytes of data from
403 * the stream. The function call always returns immediately.
405 * @param buffers The buffers into which the data will be read. Although the
406 * buffers object may be copied as necessary, ownership of the underlying
407 * buffers is retained by the caller, which must guarantee that they remain
408 * valid until the handler is called.
410 * @param handler The handler to be called when the read operation completes.
411 * Copies will be made of the handler as required. The equivalent function
412 * signature of the handler must be:
413 * @code void handler(
414 * const boost::system::error_code& error, // Result of operation.
415 * std::size_t bytes_transferred // Number of bytes read.
416 * ); @endcode
418 * @note The async_read_some operation may not read all of the requested
419 * number of bytes. Consider using the @ref async_read function if you need to
420 * ensure that the requested amount of data is read before the asynchronous
421 * operation completes.
423 template <typename MutableBufferSequence, typename ReadHandler>
424 void async_read_some(const MutableBufferSequence& buffers,
425 ReadHandler handler)
427 service_.async_read_some(impl_, next_layer_, buffers, handler);
430 /// Peek at the incoming data on the stream.
432 * This function is used to peek at the incoming data on the stream, without
433 * removing it from the input queue. The function call will block until data
434 * has been read successfully or an error occurs.
436 * @param buffers The buffers into which the data will be read.
438 * @returns The number of bytes read.
440 * @throws boost::system::system_error Thrown on failure.
442 template <typename MutableBufferSequence>
443 std::size_t peek(const MutableBufferSequence& buffers)
445 boost::system::error_code ec;
446 std::size_t s = service_.peek(impl_, next_layer_, buffers, ec);
447 boost::asio::detail::throw_error(ec);
448 return s;
451 /// Peek at the incoming data on the stream.
453 * This function is used to peek at the incoming data on the stream, withoutxi
454 * removing it from the input queue. The function call will block until data
455 * has been read successfully or an error occurs.
457 * @param buffers The buffers into which the data will be read.
459 * @param ec Set to indicate what error occurred, if any.
461 * @returns The number of bytes read. Returns 0 if an error occurred.
463 template <typename MutableBufferSequence>
464 std::size_t peek(const MutableBufferSequence& buffers,
465 boost::system::error_code& ec)
467 return service_.peek(impl_, next_layer_, buffers, ec);
470 /// Determine the amount of data that may be read without blocking.
472 * This function is used to determine the amount of data, in bytes, that may
473 * be read from the stream without blocking.
475 * @returns The number of bytes of data that can be read without blocking.
477 * @throws boost::system::system_error Thrown on failure.
479 std::size_t in_avail()
481 boost::system::error_code ec;
482 std::size_t s = service_.in_avail(impl_, next_layer_, ec);
483 boost::asio::detail::throw_error(ec);
484 return s;
487 /// Determine the amount of data that may be read without blocking.
489 * This function is used to determine the amount of data, in bytes, that may
490 * be read from the stream without blocking.
492 * @param ec Set to indicate what error occurred, if any.
494 * @returns The number of bytes of data that can be read without blocking.
496 std::size_t in_avail(boost::system::error_code& ec)
498 return service_.in_avail(impl_, next_layer_, ec);
501 private:
502 /// The next layer.
503 Stream next_layer_;
505 /// The backend service implementation.
506 service_type& service_;
508 /// The underlying native implementation.
509 impl_type impl_;
512 } // namespace ssl
513 } // namespace asio
514 } // namespace boost
516 #include <boost/asio/detail/pop_options.hpp>
518 #endif // BOOST_ASIO_SSL_STREAM_HPP