PageRenderTime 48ms CodeModel.GetById 11ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/basic_stream_socket.hpp

http://hadesmem.googlecode.com/
C++ Header | 797 lines | 196 code | 40 blank | 561 comment | 2 complexity | 691e1fd5cb85220fad02fb8d587a799e MD5 | raw file
  1//
  2// basic_stream_socket.hpp
  3// ~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 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
 11#ifndef BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
 12#define BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/config.hpp>
 19#include <cstddef>
 20#include <boost/asio/basic_socket.hpp>
 21#include <boost/asio/detail/handler_type_requirements.hpp>
 22#include <boost/asio/detail/throw_error.hpp>
 23#include <boost/asio/error.hpp>
 24#include <boost/asio/stream_socket_service.hpp>
 25
 26#include <boost/asio/detail/push_options.hpp>
 27
 28namespace boost {
 29namespace asio {
 30
 31/// Provides stream-oriented socket functionality.
 32/**
 33 * The basic_stream_socket class template provides asynchronous and blocking
 34 * stream-oriented socket functionality.
 35 *
 36 * @par Thread Safety
 37 * @e Distinct @e objects: Safe.@n
 38 * @e Shared @e objects: Unsafe.
 39 *
 40 * @par Concepts:
 41 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
 42 */
 43template <typename Protocol,
 44    typename StreamSocketService = stream_socket_service<Protocol> >
 45class basic_stream_socket
 46  : public basic_socket<Protocol, StreamSocketService>
 47{
 48public:
 49  /// (Deprecated: Use native_handle_type.) The native representation of a
 50  /// socket.
 51  typedef typename StreamSocketService::native_handle_type native_type;
 52
 53  /// The native representation of a socket.
 54  typedef typename StreamSocketService::native_handle_type native_handle_type;
 55
 56  /// The protocol type.
 57  typedef Protocol protocol_type;
 58
 59  /// The endpoint type.
 60  typedef typename Protocol::endpoint endpoint_type;
 61
 62  /// Construct a basic_stream_socket without opening it.
 63  /**
 64   * This constructor creates a stream socket without opening it. The socket
 65   * needs to be opened and then connected or accepted before data can be sent
 66   * or received on it.
 67   *
 68   * @param io_service The io_service object that the stream socket will use to
 69   * dispatch handlers for any asynchronous operations performed on the socket.
 70   */
 71  explicit basic_stream_socket(boost::asio::io_service& io_service)
 72    : basic_socket<Protocol, StreamSocketService>(io_service)
 73  {
 74  }
 75
 76  /// Construct and open a basic_stream_socket.
 77  /**
 78   * This constructor creates and opens a stream socket. The socket needs to be
 79   * connected or accepted before data can be sent or received on it.
 80   *
 81   * @param io_service The io_service object that the stream socket will use to
 82   * dispatch handlers for any asynchronous operations performed on the socket.
 83   *
 84   * @param protocol An object specifying protocol parameters to be used.
 85   *
 86   * @throws boost::system::system_error Thrown on failure.
 87   */
 88  basic_stream_socket(boost::asio::io_service& io_service,
 89      const protocol_type& protocol)
 90    : basic_socket<Protocol, StreamSocketService>(io_service, protocol)
 91  {
 92  }
 93
 94  /// Construct a basic_stream_socket, opening it and binding it to the given
 95  /// local endpoint.
 96  /**
 97   * This constructor creates a stream socket and automatically opens it bound
 98   * to the specified endpoint on the local machine. The protocol used is the
 99   * protocol associated with the given endpoint.
100   *
101   * @param io_service The io_service object that the stream socket will use to
102   * dispatch handlers for any asynchronous operations performed on the socket.
103   *
104   * @param endpoint An endpoint on the local machine to which the stream
105   * socket will be bound.
106   *
107   * @throws boost::system::system_error Thrown on failure.
108   */
109  basic_stream_socket(boost::asio::io_service& io_service,
110      const endpoint_type& endpoint)
111    : basic_socket<Protocol, StreamSocketService>(io_service, endpoint)
112  {
113  }
114
115  /// Construct a basic_stream_socket on an existing native socket.
116  /**
117   * This constructor creates a stream socket object to hold an existing native
118   * socket.
119   *
120   * @param io_service The io_service object that the stream socket will use to
121   * dispatch handlers for any asynchronous operations performed on the socket.
122   *
123   * @param protocol An object specifying protocol parameters to be used.
124   *
125   * @param native_socket The new underlying socket implementation.
126   *
127   * @throws boost::system::system_error Thrown on failure.
128   */
129  basic_stream_socket(boost::asio::io_service& io_service,
130      const protocol_type& protocol, const native_handle_type& native_socket)
131    : basic_socket<Protocol, StreamSocketService>(
132        io_service, protocol, native_socket)
133  {
134  }
135
136#if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
137  /// Move-construct a basic_stream_socket from another.
138  /**
139   * This constructor moves a stream socket from one object to another.
140   *
141   * @param other The other basic_stream_socket object from which the move
142   * will occur.
143   *
144   * @note Following the move, the moved-from object is in the same state as if
145   * constructed using the @c basic_stream_socket(io_service&) constructor.
146   */
147  basic_stream_socket(basic_stream_socket&& other)
148    : basic_socket<Protocol, StreamSocketService>(
149        BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other))
150  {
151  }
152
153  /// Move-assign a basic_stream_socket from another.
154  /**
155   * This assignment operator moves a stream socket from one object to another.
156   *
157   * @param other The other basic_stream_socket object from which the move
158   * will occur.
159   *
160   * @note Following the move, the moved-from object is in the same state as if
161   * constructed using the @c basic_stream_socket(io_service&) constructor.
162   */
163  basic_stream_socket& operator=(basic_stream_socket&& other)
164  {
165    basic_socket<Protocol, StreamSocketService>::operator=(
166        BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other));
167    return *this;
168  }
169#endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
170
171  /// Send some data on the socket.
172  /**
173   * This function is used to send data on the stream socket. The function
174   * call will block until one or more bytes of the data has been sent
175   * successfully, or an until error occurs.
176   *
177   * @param buffers One or more data buffers to be sent on the socket.
178   *
179   * @returns The number of bytes sent.
180   *
181   * @throws boost::system::system_error Thrown on failure.
182   *
183   * @note The send operation may not transmit all of the data to the peer.
184   * Consider using the @ref write function if you need to ensure that all data
185   * is written before the blocking operation completes.
186   *
187   * @par Example
188   * To send a single data buffer use the @ref buffer function as follows:
189   * @code
190   * socket.send(boost::asio::buffer(data, size));
191   * @endcode
192   * See the @ref buffer documentation for information on sending multiple
193   * buffers in one go, and how to use it with arrays, boost::array or
194   * std::vector.
195   */
196  template <typename ConstBufferSequence>
197  std::size_t send(const ConstBufferSequence& buffers)
198  {
199    boost::system::error_code ec;
200    std::size_t s = this->get_service().send(
201        this->get_implementation(), buffers, 0, ec);
202    boost::asio::detail::throw_error(ec, "send");
203    return s;
204  }
205
206  /// Send some data on the socket.
207  /**
208   * This function is used to send data on the stream socket. The function
209   * call will block until one or more bytes of the data has been sent
210   * successfully, or an until error occurs.
211   *
212   * @param buffers One or more data buffers to be sent on the socket.
213   *
214   * @param flags Flags specifying how the send call is to be made.
215   *
216   * @returns The number of bytes sent.
217   *
218   * @throws boost::system::system_error Thrown on failure.
219   *
220   * @note The send operation may not transmit all of the data to the peer.
221   * Consider using the @ref write function if you need to ensure that all data
222   * is written before the blocking operation completes.
223   *
224   * @par Example
225   * To send a single data buffer use the @ref buffer function as follows:
226   * @code
227   * socket.send(boost::asio::buffer(data, size), 0);
228   * @endcode
229   * See the @ref buffer documentation for information on sending multiple
230   * buffers in one go, and how to use it with arrays, boost::array or
231   * std::vector.
232   */
233  template <typename ConstBufferSequence>
234  std::size_t send(const ConstBufferSequence& buffers,
235      socket_base::message_flags flags)
236  {
237    boost::system::error_code ec;
238    std::size_t s = this->get_service().send(
239        this->get_implementation(), buffers, flags, ec);
240    boost::asio::detail::throw_error(ec, "send");
241    return s;
242  }
243
244  /// Send some data on the socket.
245  /**
246   * This function is used to send data on the stream socket. The function
247   * call will block until one or more bytes of the data has been sent
248   * successfully, or an until error occurs.
249   *
250   * @param buffers One or more data buffers to be sent on the socket.
251   *
252   * @param flags Flags specifying how the send call is to be made.
253   *
254   * @param ec Set to indicate what error occurred, if any.
255   *
256   * @returns The number of bytes sent. Returns 0 if an error occurred.
257   *
258   * @note The send operation may not transmit all of the data to the peer.
259   * Consider using the @ref write function if you need to ensure that all data
260   * is written before the blocking operation completes.
261   */
262  template <typename ConstBufferSequence>
263  std::size_t send(const ConstBufferSequence& buffers,
264      socket_base::message_flags flags, boost::system::error_code& ec)
265  {
266    return this->get_service().send(
267        this->get_implementation(), buffers, flags, ec);
268  }
269
270  /// Start an asynchronous send.
271  /**
272   * This function is used to asynchronously send data on the stream socket.
273   * The function call always returns immediately.
274   *
275   * @param buffers One or more data buffers to be sent on the socket. Although
276   * the buffers object may be copied as necessary, ownership of the underlying
277   * memory blocks is retained by the caller, which must guarantee that they
278   * remain valid until the handler is called.
279   *
280   * @param handler The handler to be called when the send operation completes.
281   * Copies will be made of the handler as required. The function signature of
282   * the handler must be:
283   * @code void handler(
284   *   const boost::system::error_code& error, // Result of operation.
285   *   std::size_t bytes_transferred           // Number of bytes sent.
286   * ); @endcode
287   * Regardless of whether the asynchronous operation completes immediately or
288   * not, the handler will not be invoked from within this function. Invocation
289   * of the handler will be performed in a manner equivalent to using
290   * boost::asio::io_service::post().
291   *
292   * @note The send operation may not transmit all of the data to the peer.
293   * Consider using the @ref async_write function if you need to ensure that all
294   * data is written before the asynchronous operation completes.
295   *
296   * @par Example
297   * To send a single data buffer use the @ref buffer function as follows:
298   * @code
299   * socket.async_send(boost::asio::buffer(data, size), handler);
300   * @endcode
301   * See the @ref buffer documentation for information on sending multiple
302   * buffers in one go, and how to use it with arrays, boost::array or
303   * std::vector.
304   */
305  template <typename ConstBufferSequence, typename WriteHandler>
306  void async_send(const ConstBufferSequence& buffers,
307      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
308  {
309    // If you get an error on the following line it means that your handler does
310    // not meet the documented type requirements for a WriteHandler.
311    BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
312
313    this->get_service().async_send(this->get_implementation(), buffers, 0,
314        BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
315  }
316
317  /// Start an asynchronous send.
318  /**
319   * This function is used to asynchronously send data on the stream socket.
320   * The function call always returns immediately.
321   *
322   * @param buffers One or more data buffers to be sent on the socket. Although
323   * the buffers object may be copied as necessary, ownership of the underlying
324   * memory blocks is retained by the caller, which must guarantee that they
325   * remain valid until the handler is called.
326   *
327   * @param flags Flags specifying how the send call is to be made.
328   *
329   * @param handler The handler to be called when the send operation completes.
330   * Copies will be made of the handler as required. The function signature of
331   * the handler must be:
332   * @code void handler(
333   *   const boost::system::error_code& error, // Result of operation.
334   *   std::size_t bytes_transferred           // Number of bytes sent.
335   * ); @endcode
336   * Regardless of whether the asynchronous operation completes immediately or
337   * not, the handler will not be invoked from within this function. Invocation
338   * of the handler will be performed in a manner equivalent to using
339   * boost::asio::io_service::post().
340   *
341   * @note The send operation may not transmit all of the data to the peer.
342   * Consider using the @ref async_write function if you need to ensure that all
343   * data is written before the asynchronous operation completes.
344   *
345   * @par Example
346   * To send a single data buffer use the @ref buffer function as follows:
347   * @code
348   * socket.async_send(boost::asio::buffer(data, size), 0, handler);
349   * @endcode
350   * See the @ref buffer documentation for information on sending multiple
351   * buffers in one go, and how to use it with arrays, boost::array or
352   * std::vector.
353   */
354  template <typename ConstBufferSequence, typename WriteHandler>
355  void async_send(const ConstBufferSequence& buffers,
356      socket_base::message_flags flags,
357      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
358  {
359    // If you get an error on the following line it means that your handler does
360    // not meet the documented type requirements for a WriteHandler.
361    BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
362
363    this->get_service().async_send(this->get_implementation(), buffers, flags,
364        BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
365  }
366
367  /// Receive some data on the socket.
368  /**
369   * This function is used to receive data on the stream socket. The function
370   * call will block until one or more bytes of data has been received
371   * successfully, or until an error occurs.
372   *
373   * @param buffers One or more buffers into which the data will be received.
374   *
375   * @returns The number of bytes received.
376   *
377   * @throws boost::system::system_error Thrown on failure. An error code of
378   * boost::asio::error::eof indicates that the connection was closed by the
379   * peer.
380   *
381   * @note The receive operation may not receive all of the requested number of
382   * bytes. Consider using the @ref read function if you need to ensure that the
383   * requested amount of data is read before the blocking operation completes.
384   *
385   * @par Example
386   * To receive into a single data buffer use the @ref buffer function as
387   * follows:
388   * @code
389   * socket.receive(boost::asio::buffer(data, size));
390   * @endcode
391   * See the @ref buffer documentation for information on receiving into
392   * multiple buffers in one go, and how to use it with arrays, boost::array or
393   * std::vector.
394   */
395  template <typename MutableBufferSequence>
396  std::size_t receive(const MutableBufferSequence& buffers)
397  {
398    boost::system::error_code ec;
399    std::size_t s = this->get_service().receive(
400        this->get_implementation(), buffers, 0, ec);
401    boost::asio::detail::throw_error(ec, "receive");
402    return s;
403  }
404
405  /// Receive some data on the socket.
406  /**
407   * This function is used to receive data on the stream socket. The function
408   * call will block until one or more bytes of data has been received
409   * successfully, or until an error occurs.
410   *
411   * @param buffers One or more buffers into which the data will be received.
412   *
413   * @param flags Flags specifying how the receive call is to be made.
414   *
415   * @returns The number of bytes received.
416   *
417   * @throws boost::system::system_error Thrown on failure. An error code of
418   * boost::asio::error::eof indicates that the connection was closed by the
419   * peer.
420   *
421   * @note The receive operation may not receive all of the requested number of
422   * bytes. Consider using the @ref read function if you need to ensure that the
423   * requested amount of data is read before the blocking operation completes.
424   *
425   * @par Example
426   * To receive into a single data buffer use the @ref buffer function as
427   * follows:
428   * @code
429   * socket.receive(boost::asio::buffer(data, size), 0);
430   * @endcode
431   * See the @ref buffer documentation for information on receiving into
432   * multiple buffers in one go, and how to use it with arrays, boost::array or
433   * std::vector.
434   */
435  template <typename MutableBufferSequence>
436  std::size_t receive(const MutableBufferSequence& buffers,
437      socket_base::message_flags flags)
438  {
439    boost::system::error_code ec;
440    std::size_t s = this->get_service().receive(
441        this->get_implementation(), buffers, flags, ec);
442    boost::asio::detail::throw_error(ec, "receive");
443    return s;
444  }
445
446  /// Receive some data on a connected socket.
447  /**
448   * This function is used to receive data on the stream socket. The function
449   * call will block until one or more bytes of data has been received
450   * successfully, or until an error occurs.
451   *
452   * @param buffers One or more buffers into which the data will be received.
453   *
454   * @param flags Flags specifying how the receive call is to be made.
455   *
456   * @param ec Set to indicate what error occurred, if any.
457   *
458   * @returns The number of bytes received. Returns 0 if an error occurred.
459   *
460   * @note The receive operation may not receive all of the requested number of
461   * bytes. Consider using the @ref read function if you need to ensure that the
462   * requested amount of data is read before the blocking operation completes.
463   */
464  template <typename MutableBufferSequence>
465  std::size_t receive(const MutableBufferSequence& buffers,
466      socket_base::message_flags flags, boost::system::error_code& ec)
467  {
468    return this->get_service().receive(
469        this->get_implementation(), buffers, flags, ec);
470  }
471
472  /// Start an asynchronous receive.
473  /**
474   * This function is used to asynchronously receive data from the stream
475   * socket. The function call always returns immediately.
476   *
477   * @param buffers One or more buffers into which the data will be received.
478   * Although the buffers object may be copied as necessary, ownership of the
479   * underlying memory blocks is retained by the caller, which must guarantee
480   * that they remain valid until the handler is called.
481   *
482   * @param handler The handler to be called when the receive operation
483   * completes. Copies will be made of the handler as required. The function
484   * signature of the handler must be:
485   * @code void handler(
486   *   const boost::system::error_code& error, // Result of operation.
487   *   std::size_t bytes_transferred           // Number of bytes received.
488   * ); @endcode
489   * Regardless of whether the asynchronous operation completes immediately or
490   * not, the handler will not be invoked from within this function. Invocation
491   * of the handler will be performed in a manner equivalent to using
492   * boost::asio::io_service::post().
493   *
494   * @note The receive operation may not receive all of the requested number of
495   * bytes. Consider using the @ref async_read function if you need to ensure
496   * that the requested amount of data is received before the asynchronous
497   * operation completes.
498   *
499   * @par Example
500   * To receive into a single data buffer use the @ref buffer function as
501   * follows:
502   * @code
503   * socket.async_receive(boost::asio::buffer(data, size), handler);
504   * @endcode
505   * See the @ref buffer documentation for information on receiving into
506   * multiple buffers in one go, and how to use it with arrays, boost::array or
507   * std::vector.
508   */
509  template <typename MutableBufferSequence, typename ReadHandler>
510  void async_receive(const MutableBufferSequence& buffers,
511      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
512  {
513    // If you get an error on the following line it means that your handler does
514    // not meet the documented type requirements for a ReadHandler.
515    BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
516
517    this->get_service().async_receive(this->get_implementation(),
518        buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
519  }
520
521  /// Start an asynchronous receive.
522  /**
523   * This function is used to asynchronously receive data from the stream
524   * socket. The function call always returns immediately.
525   *
526   * @param buffers One or more buffers into which the data will be received.
527   * Although the buffers object may be copied as necessary, ownership of the
528   * underlying memory blocks is retained by the caller, which must guarantee
529   * that they remain valid until the handler is called.
530   *
531   * @param flags Flags specifying how the receive call is to be made.
532   *
533   * @param handler The handler to be called when the receive operation
534   * completes. Copies will be made of the handler as required. The function
535   * signature of the handler must be:
536   * @code void handler(
537   *   const boost::system::error_code& error, // Result of operation.
538   *   std::size_t bytes_transferred           // Number of bytes received.
539   * ); @endcode
540   * Regardless of whether the asynchronous operation completes immediately or
541   * not, the handler will not be invoked from within this function. Invocation
542   * of the handler will be performed in a manner equivalent to using
543   * boost::asio::io_service::post().
544   *
545   * @note The receive operation may not receive all of the requested number of
546   * bytes. Consider using the @ref async_read function if you need to ensure
547   * that the requested amount of data is received before the asynchronous
548   * operation completes.
549   *
550   * @par Example
551   * To receive into a single data buffer use the @ref buffer function as
552   * follows:
553   * @code
554   * socket.async_receive(boost::asio::buffer(data, size), 0, handler);
555   * @endcode
556   * See the @ref buffer documentation for information on receiving into
557   * multiple buffers in one go, and how to use it with arrays, boost::array or
558   * std::vector.
559   */
560  template <typename MutableBufferSequence, typename ReadHandler>
561  void async_receive(const MutableBufferSequence& buffers,
562      socket_base::message_flags flags,
563      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
564  {
565    // If you get an error on the following line it means that your handler does
566    // not meet the documented type requirements for a ReadHandler.
567    BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
568
569    this->get_service().async_receive(this->get_implementation(),
570        buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
571  }
572
573  /// Write some data to the socket.
574  /**
575   * This function is used to write data to the stream socket. The function call
576   * will block until one or more bytes of the data has been written
577   * successfully, or until an error occurs.
578   *
579   * @param buffers One or more data buffers to be written to the socket.
580   *
581   * @returns The number of bytes written.
582   *
583   * @throws boost::system::system_error Thrown on failure. An error code of
584   * boost::asio::error::eof indicates that the connection was closed by the
585   * peer.
586   *
587   * @note The write_some operation may not transmit all of the data to the
588   * peer. Consider using the @ref write function if you need to ensure that
589   * all data is written before the blocking operation completes.
590   *
591   * @par Example
592   * To write a single data buffer use the @ref buffer function as follows:
593   * @code
594   * socket.write_some(boost::asio::buffer(data, size));
595   * @endcode
596   * See the @ref buffer documentation for information on writing multiple
597   * buffers in one go, and how to use it with arrays, boost::array or
598   * std::vector.
599   */
600  template <typename ConstBufferSequence>
601  std::size_t write_some(const ConstBufferSequence& buffers)
602  {
603    boost::system::error_code ec;
604    std::size_t s = this->get_service().send(
605        this->get_implementation(), buffers, 0, ec);
606    boost::asio::detail::throw_error(ec, "write_some");
607    return s;
608  }
609
610  /// Write some data to the socket.
611  /**
612   * This function is used to write data to the stream socket. The function call
613   * will block until one or more bytes of the data has been written
614   * successfully, or until an error occurs.
615   *
616   * @param buffers One or more data buffers to be written to the socket.
617   *
618   * @param ec Set to indicate what error occurred, if any.
619   *
620   * @returns The number of bytes written. Returns 0 if an error occurred.
621   *
622   * @note The write_some operation may not transmit all of the data to the
623   * peer. Consider using the @ref write function if you need to ensure that
624   * all data is written before the blocking operation completes.
625   */
626  template <typename ConstBufferSequence>
627  std::size_t write_some(const ConstBufferSequence& buffers,
628      boost::system::error_code& ec)
629  {
630    return this->get_service().send(this->get_implementation(), buffers, 0, ec);
631  }
632
633  /// Start an asynchronous write.
634  /**
635   * This function is used to asynchronously write data to the stream socket.
636   * The function call always returns immediately.
637   *
638   * @param buffers One or more data buffers to be written to the socket.
639   * Although the buffers object may be copied as necessary, ownership of the
640   * underlying memory blocks is retained by the caller, which must guarantee
641   * that they remain valid until the handler is called.
642   *
643   * @param handler The handler to be called when the write operation completes.
644   * Copies will be made of the handler as required. The function signature of
645   * the handler must be:
646   * @code void handler(
647   *   const boost::system::error_code& error, // Result of operation.
648   *   std::size_t bytes_transferred           // Number of bytes written.
649   * ); @endcode
650   * Regardless of whether the asynchronous operation completes immediately or
651   * not, the handler will not be invoked from within this function. Invocation
652   * of the handler will be performed in a manner equivalent to using
653   * boost::asio::io_service::post().
654   *
655   * @note The write operation may not transmit all of the data to the peer.
656   * Consider using the @ref async_write function if you need to ensure that all
657   * data is written before the asynchronous operation completes.
658   *
659   * @par Example
660   * To write a single data buffer use the @ref buffer function as follows:
661   * @code
662   * socket.async_write_some(boost::asio::buffer(data, size), handler);
663   * @endcode
664   * See the @ref buffer documentation for information on writing multiple
665   * buffers in one go, and how to use it with arrays, boost::array or
666   * std::vector.
667   */
668  template <typename ConstBufferSequence, typename WriteHandler>
669  void async_write_some(const ConstBufferSequence& buffers,
670      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
671  {
672    // If you get an error on the following line it means that your handler does
673    // not meet the documented type requirements for a WriteHandler.
674    BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
675
676    this->get_service().async_send(this->get_implementation(),
677        buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
678  }
679
680  /// Read some data from the socket.
681  /**
682   * This function is used to read data from the stream socket. The function
683   * call will block until one or more bytes of data has been read successfully,
684   * or until an error occurs.
685   *
686   * @param buffers One or more buffers into which the data will be read.
687   *
688   * @returns The number of bytes read.
689   *
690   * @throws boost::system::system_error Thrown on failure. An error code of
691   * boost::asio::error::eof indicates that the connection was closed by the
692   * peer.
693   *
694   * @note The read_some operation may not read all of the requested number of
695   * bytes. Consider using the @ref read function if you need to ensure that
696   * the requested amount of data is read before the blocking operation
697   * completes.
698   *
699   * @par Example
700   * To read into a single data buffer use the @ref buffer function as follows:
701   * @code
702   * socket.read_some(boost::asio::buffer(data, size));
703   * @endcode
704   * See the @ref buffer documentation for information on reading into multiple
705   * buffers in one go, and how to use it with arrays, boost::array or
706   * std::vector.
707   */
708  template <typename MutableBufferSequence>
709  std::size_t read_some(const MutableBufferSequence& buffers)
710  {
711    boost::system::error_code ec;
712    std::size_t s = this->get_service().receive(
713        this->get_implementation(), buffers, 0, ec);
714    boost::asio::detail::throw_error(ec, "read_some");
715    return s;
716  }
717
718  /// Read some data from the socket.
719  /**
720   * This function is used to read data from the stream socket. The function
721   * call will block until one or more bytes of data has been read successfully,
722   * or until an error occurs.
723   *
724   * @param buffers One or more buffers into which the data will be read.
725   *
726   * @param ec Set to indicate what error occurred, if any.
727   *
728   * @returns The number of bytes read. Returns 0 if an error occurred.
729   *
730   * @note The read_some operation may not read all of the requested number of
731   * bytes. Consider using the @ref read function if you need to ensure that
732   * the requested amount of data is read before the blocking operation
733   * completes.
734   */
735  template <typename MutableBufferSequence>
736  std::size_t read_some(const MutableBufferSequence& buffers,
737      boost::system::error_code& ec)
738  {
739    return this->get_service().receive(
740        this->get_implementation(), buffers, 0, ec);
741  }
742
743  /// Start an asynchronous read.
744  /**
745   * This function is used to asynchronously read data from the stream socket.
746   * The function call always returns immediately.
747   *
748   * @param buffers One or more buffers into which the data will be read.
749   * Although the buffers object may be copied as necessary, ownership of the
750   * underlying memory blocks is retained by the caller, which must guarantee
751   * that they remain valid until the handler is called.
752   *
753   * @param handler The handler to be called when the read operation completes.
754   * Copies will be made of the handler as required. The function signature of
755   * the handler must be:
756   * @code void handler(
757   *   const boost::system::error_code& error, // Result of operation.
758   *   std::size_t bytes_transferred           // Number of bytes read.
759   * ); @endcode
760   * Regardless of whether the asynchronous operation completes immediately or
761   * not, the handler will not be invoked from within this function. Invocation
762   * of the handler will be performed in a manner equivalent to using
763   * boost::asio::io_service::post().
764   *
765   * @note The read operation may not read all of the requested number of bytes.
766   * Consider using the @ref async_read function if you need to ensure that the
767   * requested amount of data is read before the asynchronous operation
768   * completes.
769   *
770   * @par Example
771   * To read into a single data buffer use the @ref buffer function as follows:
772   * @code
773   * socket.async_read_some(boost::asio::buffer(data, size), handler);
774   * @endcode
775   * See the @ref buffer documentation for information on reading into multiple
776   * buffers in one go, and how to use it with arrays, boost::array or
777   * std::vector.
778   */
779  template <typename MutableBufferSequence, typename ReadHandler>
780  void async_read_some(const MutableBufferSequence& buffers,
781      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
782  {
783    // If you get an error on the following line it means that your handler does
784    // not meet the documented type requirements for a ReadHandler.
785    BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
786
787    this->get_service().async_receive(this->get_implementation(),
788        buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
789  }
790};
791
792} // namespace asio
793} // namespace boost
794
795#include <boost/asio/detail/pop_options.hpp>
796
797#endif // BOOST_ASIO_BASIC_STREAM_SOCKET_HPP