change support python version
[platform/upstream/boost.git] / boost / asio / basic_raw_socket.hpp
1 //
2 // basic_raw_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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_RAW_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_RAW_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/non_const_lvalue.hpp>
23 #include <boost/asio/detail/throw_error.hpp>
24 #include <boost/asio/detail/type_traits.hpp>
25 #include <boost/asio/error.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31
32 #if !defined(BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL)
33 #define BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL
34
35 // Forward declaration with defaulted arguments.
36 template <typename Protocol, typename Executor = executor>
37 class basic_raw_socket;
38
39 #endif // !defined(BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL)
40
41 /// Provides raw-oriented socket functionality.
42 /**
43  * The basic_raw_socket class template provides asynchronous and blocking
44  * raw-oriented socket functionality.
45  *
46  * @par Thread Safety
47  * @e Distinct @e objects: Safe.@n
48  * @e Shared @e objects: Unsafe.
49  */
50 template <typename Protocol, typename Executor>
51 class basic_raw_socket
52   : public basic_socket<Protocol, Executor>
53 {
54 public:
55   /// The type of the executor associated with the object.
56   typedef Executor executor_type;
57
58   /// Rebinds the socket type to another executor.
59   template <typename Executor1>
60   struct rebind_executor
61   {
62     /// The socket type when rebound to the specified executor.
63     typedef basic_raw_socket<Protocol, Executor1> other;
64   };
65
66   /// The native representation of a socket.
67 #if defined(GENERATING_DOCUMENTATION)
68   typedef implementation_defined native_handle_type;
69 #else
70   typedef typename basic_socket<Protocol,
71     Executor>::native_handle_type native_handle_type;
72 #endif
73
74   /// The protocol type.
75   typedef Protocol protocol_type;
76
77   /// The endpoint type.
78   typedef typename Protocol::endpoint endpoint_type;
79
80   /// Construct a basic_raw_socket without opening it.
81   /**
82    * This constructor creates a raw socket without opening it. The open()
83    * function must be called before data can be sent or received on the socket.
84    *
85    * @param ex The I/O executor that the socket will use, by default, to
86    * dispatch handlers for any asynchronous operations performed on the socket.
87    */
88   explicit basic_raw_socket(const executor_type& ex)
89     : basic_socket<Protocol, Executor>(ex)
90   {
91   }
92
93   /// Construct a basic_raw_socket without opening it.
94   /**
95    * This constructor creates a raw socket without opening it. The open()
96    * function must be called before data can be sent or received on the socket.
97    *
98    * @param context An execution context which provides the I/O executor that
99    * the socket will use, by default, to dispatch handlers for any asynchronous
100    * operations performed on the socket.
101    */
102   template <typename ExecutionContext>
103   explicit basic_raw_socket(ExecutionContext& context,
104       typename enable_if<
105         is_convertible<ExecutionContext&, execution_context&>::value
106       >::type* = 0)
107     : basic_socket<Protocol, Executor>(context)
108   {
109   }
110
111   /// Construct and open a basic_raw_socket.
112   /**
113    * This constructor creates and opens a raw socket.
114    *
115    * @param ex The I/O executor that the socket will use, by default, to
116    * dispatch handlers for any asynchronous operations performed on the socket.
117    *
118    * @param protocol An object specifying protocol parameters to be used.
119    *
120    * @throws boost::system::system_error Thrown on failure.
121    */
122   basic_raw_socket(const executor_type& ex, const protocol_type& protocol)
123     : basic_socket<Protocol, Executor>(ex, protocol)
124   {
125   }
126
127   /// Construct and open a basic_raw_socket.
128   /**
129    * This constructor creates and opens a raw socket.
130    *
131    * @param context An execution context which provides the I/O executor that
132    * the socket will use, by default, to dispatch handlers for any asynchronous
133    * operations performed on the socket.
134    *
135    * @param protocol An object specifying protocol parameters to be used.
136    *
137    * @throws boost::system::system_error Thrown on failure.
138    */
139   template <typename ExecutionContext>
140   basic_raw_socket(ExecutionContext& context, const protocol_type& protocol,
141       typename enable_if<
142         is_convertible<ExecutionContext&, execution_context&>::value
143       >::type* = 0)
144     : basic_socket<Protocol, Executor>(context, protocol)
145   {
146   }
147
148   /// Construct a basic_raw_socket, opening it and binding it to the given
149   /// local endpoint.
150   /**
151    * This constructor creates a raw socket and automatically opens it bound
152    * to the specified endpoint on the local machine. The protocol used is the
153    * protocol associated with the given endpoint.
154    *
155    * @param ex The I/O executor that the socket will use, by default, to
156    * dispatch handlers for any asynchronous operations performed on the socket.
157    *
158    * @param endpoint An endpoint on the local machine to which the raw
159    * socket will be bound.
160    *
161    * @throws boost::system::system_error Thrown on failure.
162    */
163   basic_raw_socket(const executor_type& ex, const endpoint_type& endpoint)
164     : basic_socket<Protocol, Executor>(ex, endpoint)
165   {
166   }
167
168   /// Construct a basic_raw_socket, opening it and binding it to the given
169   /// local endpoint.
170   /**
171    * This constructor creates a raw socket and automatically opens it bound
172    * to the specified endpoint on the local machine. The protocol used is the
173    * protocol associated with the given endpoint.
174    *
175    * @param context An execution context which provides the I/O executor that
176    * the socket will use, by default, to dispatch handlers for any asynchronous
177    * operations performed on the socket.
178    *
179    * @param endpoint An endpoint on the local machine to which the raw
180    * socket will be bound.
181    *
182    * @throws boost::system::system_error Thrown on failure.
183    */
184   template <typename ExecutionContext>
185   basic_raw_socket(ExecutionContext& context, const endpoint_type& endpoint,
186       typename enable_if<
187         is_convertible<ExecutionContext&, execution_context&>::value
188       >::type* = 0)
189     : basic_socket<Protocol, Executor>(context, endpoint)
190   {
191   }
192
193   /// Construct a basic_raw_socket on an existing native socket.
194   /**
195    * This constructor creates a raw socket object to hold an existing
196    * native socket.
197    *
198    * @param ex The I/O executor that the socket will use, by default, to
199    * dispatch handlers for any asynchronous operations performed on the socket.
200    *
201    * @param protocol An object specifying protocol parameters to be used.
202    *
203    * @param native_socket The new underlying socket implementation.
204    *
205    * @throws boost::system::system_error Thrown on failure.
206    */
207   basic_raw_socket(const executor_type& ex,
208       const protocol_type& protocol, const native_handle_type& native_socket)
209     : basic_socket<Protocol, Executor>(ex, protocol, native_socket)
210   {
211   }
212
213   /// Construct a basic_raw_socket on an existing native socket.
214   /**
215    * This constructor creates a raw socket object to hold an existing
216    * native socket.
217    *
218    * @param context An execution context which provides the I/O executor that
219    * the socket will use, by default, to dispatch handlers for any asynchronous
220    * operations performed on the socket.
221    *
222    * @param protocol An object specifying protocol parameters to be used.
223    *
224    * @param native_socket The new underlying socket implementation.
225    *
226    * @throws boost::system::system_error Thrown on failure.
227    */
228   template <typename ExecutionContext>
229   basic_raw_socket(ExecutionContext& context,
230       const protocol_type& protocol, const native_handle_type& native_socket,
231       typename enable_if<
232         is_convertible<ExecutionContext&, execution_context&>::value
233       >::type* = 0)
234     : basic_socket<Protocol, Executor>(context, protocol, native_socket)
235   {
236   }
237
238 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
239   /// Move-construct a basic_raw_socket from another.
240   /**
241    * This constructor moves a raw socket from one object to another.
242    *
243    * @param other The other basic_raw_socket object from which the move
244    * will occur.
245    *
246    * @note Following the move, the moved-from object is in the same state as if
247    * constructed using the @c basic_raw_socket(const executor_type&)
248    * constructor.
249    */
250   basic_raw_socket(basic_raw_socket&& other)
251     : basic_socket<Protocol, Executor>(std::move(other))
252   {
253   }
254
255   /// Move-assign a basic_raw_socket from another.
256   /**
257    * This assignment operator moves a raw socket from one object to another.
258    *
259    * @param other The other basic_raw_socket object from which the move
260    * will occur.
261    *
262    * @note Following the move, the moved-from object is in the same state as if
263    * constructed using the @c basic_raw_socket(const executor_type&)
264    * constructor.
265    */
266   basic_raw_socket& operator=(basic_raw_socket&& other)
267   {
268     basic_socket<Protocol, Executor>::operator=(std::move(other));
269     return *this;
270   }
271
272   /// Move-construct a basic_raw_socket from a socket of another protocol
273   /// type.
274   /**
275    * This constructor moves a raw socket from one object to another.
276    *
277    * @param other The other basic_raw_socket object from which the move
278    * will occur.
279    *
280    * @note Following the move, the moved-from object is in the same state as if
281    * constructed using the @c basic_raw_socket(const executor_type&)
282    * constructor.
283    */
284   template <typename Protocol1, typename Executor1>
285   basic_raw_socket(basic_raw_socket<Protocol1, Executor1>&& other,
286       typename enable_if<
287         is_convertible<Protocol1, Protocol>::value
288           && is_convertible<Executor1, Executor>::value
289       >::type* = 0)
290     : basic_socket<Protocol, Executor>(std::move(other))
291   {
292   }
293
294   /// Move-assign a basic_raw_socket from a socket of another protocol type.
295   /**
296    * This assignment operator moves a raw socket from one object to another.
297    *
298    * @param other The other basic_raw_socket object from which the move
299    * will occur.
300    *
301    * @note Following the move, the moved-from object is in the same state as if
302    * constructed using the @c basic_raw_socket(const executor_type&)
303    * constructor.
304    */
305   template <typename Protocol1, typename Executor1>
306   typename enable_if<
307     is_convertible<Protocol1, Protocol>::value
308       && is_convertible<Executor1, Executor>::value,
309     basic_raw_socket&
310   >::type operator=(basic_raw_socket<Protocol1, Executor1>&& other)
311   {
312     basic_socket<Protocol, Executor>::operator=(std::move(other));
313     return *this;
314   }
315 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
316
317   /// Destroys the socket.
318   /**
319    * This function destroys the socket, cancelling any outstanding asynchronous
320    * operations associated with the socket as if by calling @c cancel.
321    */
322   ~basic_raw_socket()
323   {
324   }
325
326   /// Send some data on a connected socket.
327   /**
328    * This function is used to send data on the raw socket. The function call
329    * will block until the data has been sent successfully or an error occurs.
330    *
331    * @param buffers One ore more data buffers to be sent on the socket.
332    *
333    * @returns The number of bytes sent.
334    *
335    * @throws boost::system::system_error Thrown on failure.
336    *
337    * @note The send operation can only be used with a connected socket. Use
338    * the send_to function to send data on an unconnected raw socket.
339    *
340    * @par Example
341    * To send a single data buffer use the @ref buffer function as follows:
342    * @code socket.send(boost::asio::buffer(data, size)); @endcode
343    * See the @ref buffer documentation for information on sending multiple
344    * buffers in one go, and how to use it with arrays, boost::array or
345    * std::vector.
346    */
347   template <typename ConstBufferSequence>
348   std::size_t send(const ConstBufferSequence& buffers)
349   {
350     boost::system::error_code ec;
351     std::size_t s = this->impl_.get_service().send(
352         this->impl_.get_implementation(), buffers, 0, ec);
353     boost::asio::detail::throw_error(ec, "send");
354     return s;
355   }
356
357   /// Send some data on a connected socket.
358   /**
359    * This function is used to send data on the raw socket. The function call
360    * will block until the data has been sent successfully or an error occurs.
361    *
362    * @param buffers One ore more data buffers to be sent on the socket.
363    *
364    * @param flags Flags specifying how the send call is to be made.
365    *
366    * @returns The number of bytes sent.
367    *
368    * @throws boost::system::system_error Thrown on failure.
369    *
370    * @note The send operation can only be used with a connected socket. Use
371    * the send_to function to send data on an unconnected raw socket.
372    */
373   template <typename ConstBufferSequence>
374   std::size_t send(const ConstBufferSequence& buffers,
375       socket_base::message_flags flags)
376   {
377     boost::system::error_code ec;
378     std::size_t s = this->impl_.get_service().send(
379         this->impl_.get_implementation(), buffers, flags, ec);
380     boost::asio::detail::throw_error(ec, "send");
381     return s;
382   }
383
384   /// Send some data on a connected socket.
385   /**
386    * This function is used to send data on the raw socket. The function call
387    * will block until the data has been sent successfully or an error occurs.
388    *
389    * @param buffers One or more data buffers to be sent on the socket.
390    *
391    * @param flags Flags specifying how the send call is to be made.
392    *
393    * @param ec Set to indicate what error occurred, if any.
394    *
395    * @returns The number of bytes sent.
396    *
397    * @note The send operation can only be used with a connected socket. Use
398    * the send_to function to send data on an unconnected raw socket.
399    */
400   template <typename ConstBufferSequence>
401   std::size_t send(const ConstBufferSequence& buffers,
402       socket_base::message_flags flags, boost::system::error_code& ec)
403   {
404     return this->impl_.get_service().send(
405         this->impl_.get_implementation(), buffers, flags, ec);
406   }
407
408   /// Start an asynchronous send on a connected socket.
409   /**
410    * This function is used to send data on the raw socket. The function call
411    * will block until the data has been sent successfully or an error occurs.
412    *
413    * @param buffers One or more data buffers to be sent on the socket. Although
414    * the buffers object may be copied as necessary, ownership of the underlying
415    * memory blocks is retained by the caller, which must guarantee that they
416    * remain valid until the handler is called.
417    *
418    * @param handler The handler to be called when the send operation completes.
419    * Copies will be made of the handler as required. The function signature of
420    * the handler must be:
421    * @code void handler(
422    *   const boost::system::error_code& error, // Result of operation.
423    *   std::size_t bytes_transferred           // Number of bytes sent.
424    * ); @endcode
425    * Regardless of whether the asynchronous operation completes immediately or
426    * not, the handler will not be invoked from within this function. On
427    * immediate completion, invocation of the handler will be performed in a
428    * manner equivalent to using boost::asio::post().
429    *
430    * @note The async_send operation can only be used with a connected socket.
431    * Use the async_send_to function to send data on an unconnected raw
432    * socket.
433    *
434    * @par Example
435    * To send a single data buffer use the @ref buffer function as follows:
436    * @code
437    * socket.async_send(boost::asio::buffer(data, size), handler);
438    * @endcode
439    * See the @ref buffer documentation for information on sending multiple
440    * buffers in one go, and how to use it with arrays, boost::array or
441    * std::vector.
442    */
443   template <typename ConstBufferSequence, typename WriteHandler>
444   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
445       void (boost::system::error_code, std::size_t))
446   async_send(const ConstBufferSequence& buffers,
447       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
448   {
449     return async_initiate<WriteHandler,
450       void (boost::system::error_code, std::size_t)>(
451         initiate_async_send(), handler, this,
452         buffers, socket_base::message_flags(0));
453   }
454
455   /// Start an asynchronous send on a connected socket.
456   /**
457    * This function is used to send data on the raw socket. The function call
458    * will block until the data has been sent successfully or an error occurs.
459    *
460    * @param buffers One or more data buffers to be sent on the socket. Although
461    * the buffers object may be copied as necessary, ownership of the underlying
462    * memory blocks is retained by the caller, which must guarantee that they
463    * remain valid until the handler is called.
464    *
465    * @param flags Flags specifying how the send call is to be made.
466    *
467    * @param handler The handler to be called when the send operation completes.
468    * Copies will be made of the handler as required. The function signature of
469    * the handler must be:
470    * @code void handler(
471    *   const boost::system::error_code& error, // Result of operation.
472    *   std::size_t bytes_transferred           // Number of bytes sent.
473    * ); @endcode
474    * Regardless of whether the asynchronous operation completes immediately or
475    * not, the handler will not be invoked from within this function. On
476    * immediate completion, invocation of the handler will be performed in a
477    * manner equivalent to using boost::asio::post().
478    *
479    * @note The async_send operation can only be used with a connected socket.
480    * Use the async_send_to function to send data on an unconnected raw
481    * socket.
482    */
483   template <typename ConstBufferSequence, typename WriteHandler>
484   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
485       void (boost::system::error_code, std::size_t))
486   async_send(const ConstBufferSequence& buffers,
487       socket_base::message_flags flags,
488       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
489   {
490     return async_initiate<WriteHandler,
491       void (boost::system::error_code, std::size_t)>(
492         initiate_async_send(), handler, this, buffers, flags);
493   }
494
495   /// Send raw data to the specified endpoint.
496   /**
497    * This function is used to send raw data to the specified remote endpoint.
498    * The function call will block until the data has been sent successfully or
499    * an error occurs.
500    *
501    * @param buffers One or more data buffers to be sent to the remote endpoint.
502    *
503    * @param destination The remote endpoint to which the data will be sent.
504    *
505    * @returns The number of bytes sent.
506    *
507    * @throws boost::system::system_error Thrown on failure.
508    *
509    * @par Example
510    * To send a single data buffer use the @ref buffer function as follows:
511    * @code
512    * boost::asio::ip::udp::endpoint destination(
513    *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
514    * socket.send_to(boost::asio::buffer(data, size), destination);
515    * @endcode
516    * See the @ref buffer documentation for information on sending multiple
517    * buffers in one go, and how to use it with arrays, boost::array or
518    * std::vector.
519    */
520   template <typename ConstBufferSequence>
521   std::size_t send_to(const ConstBufferSequence& buffers,
522       const endpoint_type& destination)
523   {
524     boost::system::error_code ec;
525     std::size_t s = this->impl_.get_service().send_to(
526         this->impl_.get_implementation(), buffers, destination, 0, ec);
527     boost::asio::detail::throw_error(ec, "send_to");
528     return s;
529   }
530
531   /// Send raw data to the specified endpoint.
532   /**
533    * This function is used to send raw data to the specified remote endpoint.
534    * The function call will block until the data has been sent successfully or
535    * an error occurs.
536    *
537    * @param buffers One or more data buffers to be sent to the remote endpoint.
538    *
539    * @param destination The remote endpoint to which the data will be sent.
540    *
541    * @param flags Flags specifying how the send call is to be made.
542    *
543    * @returns The number of bytes sent.
544    *
545    * @throws boost::system::system_error Thrown on failure.
546    */
547   template <typename ConstBufferSequence>
548   std::size_t send_to(const ConstBufferSequence& buffers,
549       const endpoint_type& destination, socket_base::message_flags flags)
550   {
551     boost::system::error_code ec;
552     std::size_t s = this->impl_.get_service().send_to(
553         this->impl_.get_implementation(), buffers, destination, flags, ec);
554     boost::asio::detail::throw_error(ec, "send_to");
555     return s;
556   }
557
558   /// Send raw data to the specified endpoint.
559   /**
560    * This function is used to send raw data to the specified remote endpoint.
561    * The function call will block until the data has been sent successfully or
562    * an error occurs.
563    *
564    * @param buffers One or more data buffers to be sent to the remote endpoint.
565    *
566    * @param destination The remote endpoint to which the data will be sent.
567    *
568    * @param flags Flags specifying how the send call is to be made.
569    *
570    * @param ec Set to indicate what error occurred, if any.
571    *
572    * @returns The number of bytes sent.
573    */
574   template <typename ConstBufferSequence>
575   std::size_t send_to(const ConstBufferSequence& buffers,
576       const endpoint_type& destination, socket_base::message_flags flags,
577       boost::system::error_code& ec)
578   {
579     return this->impl_.get_service().send_to(this->impl_.get_implementation(),
580         buffers, destination, flags, ec);
581   }
582
583   /// Start an asynchronous send.
584   /**
585    * This function is used to asynchronously send raw data to the specified
586    * remote endpoint. The function call always returns immediately.
587    *
588    * @param buffers One or more data buffers to be sent to the remote endpoint.
589    * Although the buffers object may be copied as necessary, ownership of the
590    * underlying memory blocks is retained by the caller, which must guarantee
591    * that they remain valid until the handler is called.
592    *
593    * @param destination The remote endpoint to which the data will be sent.
594    * Copies will be made of the endpoint as required.
595    *
596    * @param handler The handler to be called when the send operation completes.
597    * Copies will be made of the handler as required. The function signature of
598    * the handler must be:
599    * @code void handler(
600    *   const boost::system::error_code& error, // Result of operation.
601    *   std::size_t bytes_transferred           // Number of bytes sent.
602    * ); @endcode
603    * Regardless of whether the asynchronous operation completes immediately or
604    * not, the handler will not be invoked from within this function. On
605    * immediate completion, invocation of the handler will be performed in a
606    * manner equivalent to using boost::asio::post().
607    *
608    * @par Example
609    * To send a single data buffer use the @ref buffer function as follows:
610    * @code
611    * boost::asio::ip::udp::endpoint destination(
612    *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
613    * socket.async_send_to(
614    *     boost::asio::buffer(data, size), destination, handler);
615    * @endcode
616    * See the @ref buffer documentation for information on sending multiple
617    * buffers in one go, and how to use it with arrays, boost::array or
618    * std::vector.
619    */
620   template <typename ConstBufferSequence, typename WriteHandler>
621   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
622       void (boost::system::error_code, std::size_t))
623   async_send_to(const ConstBufferSequence& buffers,
624       const endpoint_type& destination,
625       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
626   {
627     return async_initiate<WriteHandler,
628       void (boost::system::error_code, std::size_t)>(
629         initiate_async_send_to(), handler, this, buffers,
630         destination, socket_base::message_flags(0));
631   }
632
633   /// Start an asynchronous send.
634   /**
635    * This function is used to asynchronously send raw data to the specified
636    * remote endpoint. The function call always returns immediately.
637    *
638    * @param buffers One or more data buffers to be sent to the remote endpoint.
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 flags Flags specifying how the send call is to be made.
644    *
645    * @param destination The remote endpoint to which the data will be sent.
646    * Copies will be made of the endpoint as required.
647    *
648    * @param handler The handler to be called when the send operation completes.
649    * Copies will be made of the handler as required. The function signature of
650    * the handler must be:
651    * @code void handler(
652    *   const boost::system::error_code& error, // Result of operation.
653    *   std::size_t bytes_transferred           // Number of bytes sent.
654    * ); @endcode
655    * Regardless of whether the asynchronous operation completes immediately or
656    * not, the handler will not be invoked from within this function. On
657    * immediate completion, invocation of the handler will be performed in a
658    * manner equivalent to using boost::asio::post().
659    */
660   template <typename ConstBufferSequence, typename WriteHandler>
661   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
662       void (boost::system::error_code, std::size_t))
663   async_send_to(const ConstBufferSequence& buffers,
664       const endpoint_type& destination, socket_base::message_flags flags,
665       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
666   {
667     return async_initiate<WriteHandler,
668       void (boost::system::error_code, std::size_t)>(
669         initiate_async_send_to(), handler, this, buffers, destination, flags);
670   }
671
672   /// Receive some data on a connected socket.
673   /**
674    * This function is used to receive data on the raw socket. The function
675    * call will block until data has been received successfully or an error
676    * occurs.
677    *
678    * @param buffers One or more buffers into which the data will be received.
679    *
680    * @returns The number of bytes received.
681    *
682    * @throws boost::system::system_error Thrown on failure.
683    *
684    * @note The receive operation can only be used with a connected socket. Use
685    * the receive_from function to receive data on an unconnected raw
686    * socket.
687    *
688    * @par Example
689    * To receive into a single data buffer use the @ref buffer function as
690    * follows:
691    * @code socket.receive(boost::asio::buffer(data, size)); @endcode
692    * See the @ref buffer documentation for information on receiving into
693    * multiple buffers in one go, and how to use it with arrays, boost::array or
694    * std::vector.
695    */
696   template <typename MutableBufferSequence>
697   std::size_t receive(const MutableBufferSequence& buffers)
698   {
699     boost::system::error_code ec;
700     std::size_t s = this->impl_.get_service().receive(
701         this->impl_.get_implementation(), buffers, 0, ec);
702     boost::asio::detail::throw_error(ec, "receive");
703     return s;
704   }
705
706   /// Receive some data on a connected socket.
707   /**
708    * This function is used to receive data on the raw socket. The function
709    * call will block until data has been received successfully or an error
710    * occurs.
711    *
712    * @param buffers One or more buffers into which the data will be received.
713    *
714    * @param flags Flags specifying how the receive call is to be made.
715    *
716    * @returns The number of bytes received.
717    *
718    * @throws boost::system::system_error Thrown on failure.
719    *
720    * @note The receive operation can only be used with a connected socket. Use
721    * the receive_from function to receive data on an unconnected raw
722    * socket.
723    */
724   template <typename MutableBufferSequence>
725   std::size_t receive(const MutableBufferSequence& buffers,
726       socket_base::message_flags flags)
727   {
728     boost::system::error_code ec;
729     std::size_t s = this->impl_.get_service().receive(
730         this->impl_.get_implementation(), buffers, flags, ec);
731     boost::asio::detail::throw_error(ec, "receive");
732     return s;
733   }
734
735   /// Receive some data on a connected socket.
736   /**
737    * This function is used to receive data on the raw socket. The function
738    * call will block until data has been received successfully or an error
739    * occurs.
740    *
741    * @param buffers One or more buffers into which the data will be received.
742    *
743    * @param flags Flags specifying how the receive call is to be made.
744    *
745    * @param ec Set to indicate what error occurred, if any.
746    *
747    * @returns The number of bytes received.
748    *
749    * @note The receive operation can only be used with a connected socket. Use
750    * the receive_from function to receive data on an unconnected raw
751    * socket.
752    */
753   template <typename MutableBufferSequence>
754   std::size_t receive(const MutableBufferSequence& buffers,
755       socket_base::message_flags flags, boost::system::error_code& ec)
756   {
757     return this->impl_.get_service().receive(
758         this->impl_.get_implementation(), buffers, flags, ec);
759   }
760
761   /// Start an asynchronous receive on a connected socket.
762   /**
763    * This function is used to asynchronously receive data from the raw
764    * socket. The function call always returns immediately.
765    *
766    * @param buffers One or more buffers into which the data will be received.
767    * Although the buffers object may be copied as necessary, ownership of the
768    * underlying memory blocks is retained by the caller, which must guarantee
769    * that they remain valid until the handler is called.
770    *
771    * @param handler The handler to be called when the receive operation
772    * completes. Copies will be made of the handler as required. The function
773    * signature of the handler must be:
774    * @code void handler(
775    *   const boost::system::error_code& error, // Result of operation.
776    *   std::size_t bytes_transferred           // Number of bytes received.
777    * ); @endcode
778    * Regardless of whether the asynchronous operation completes immediately or
779    * not, the handler will not be invoked from within this function. On
780    * immediate completion, invocation of the handler will be performed in a
781    * manner equivalent to using boost::asio::post().
782    *
783    * @note The async_receive operation can only be used with a connected socket.
784    * Use the async_receive_from function to receive data on an unconnected
785    * raw socket.
786    *
787    * @par Example
788    * To receive into a single data buffer use the @ref buffer function as
789    * follows:
790    * @code
791    * socket.async_receive(boost::asio::buffer(data, size), handler);
792    * @endcode
793    * See the @ref buffer documentation for information on receiving into
794    * multiple buffers in one go, and how to use it with arrays, boost::array or
795    * std::vector.
796    */
797   template <typename MutableBufferSequence, typename ReadHandler>
798   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
799       void (boost::system::error_code, std::size_t))
800   async_receive(const MutableBufferSequence& buffers,
801       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
802   {
803     return async_initiate<ReadHandler,
804       void (boost::system::error_code, std::size_t)>(
805         initiate_async_receive(), handler, this,
806         buffers, socket_base::message_flags(0));
807   }
808
809   /// Start an asynchronous receive on a connected socket.
810   /**
811    * This function is used to asynchronously receive data from the raw
812    * socket. The function call always returns immediately.
813    *
814    * @param buffers One or more buffers into which the data will be received.
815    * Although the buffers object may be copied as necessary, ownership of the
816    * underlying memory blocks is retained by the caller, which must guarantee
817    * that they remain valid until the handler is called.
818    *
819    * @param flags Flags specifying how the receive call is to be made.
820    *
821    * @param handler The handler to be called when the receive operation
822    * completes. Copies will be made of the handler as required. The function
823    * signature of the handler must be:
824    * @code void handler(
825    *   const boost::system::error_code& error, // Result of operation.
826    *   std::size_t bytes_transferred           // Number of bytes received.
827    * ); @endcode
828    * Regardless of whether the asynchronous operation completes immediately or
829    * not, the handler will not be invoked from within this function. On
830    * immediate completion, invocation of the handler will be performed in a
831    * manner equivalent to using boost::asio::post().
832    *
833    * @note The async_receive operation can only be used with a connected socket.
834    * Use the async_receive_from function to receive data on an unconnected
835    * raw socket.
836    */
837   template <typename MutableBufferSequence, typename ReadHandler>
838   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
839       void (boost::system::error_code, std::size_t))
840   async_receive(const MutableBufferSequence& buffers,
841       socket_base::message_flags flags,
842       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
843   {
844     return async_initiate<ReadHandler,
845       void (boost::system::error_code, std::size_t)>(
846         initiate_async_receive(), handler, this, buffers, flags);
847   }
848
849   /// Receive raw data with the endpoint of the sender.
850   /**
851    * This function is used to receive raw data. The function call will block
852    * until data has been received successfully or an error occurs.
853    *
854    * @param buffers One or more buffers into which the data will be received.
855    *
856    * @param sender_endpoint An endpoint object that receives the endpoint of
857    * the remote sender of the data.
858    *
859    * @returns The number of bytes received.
860    *
861    * @throws boost::system::system_error Thrown on failure.
862    *
863    * @par Example
864    * To receive into a single data buffer use the @ref buffer function as
865    * follows:
866    * @code
867    * boost::asio::ip::udp::endpoint sender_endpoint;
868    * socket.receive_from(
869    *     boost::asio::buffer(data, size), sender_endpoint);
870    * @endcode
871    * See the @ref buffer documentation for information on receiving into
872    * multiple buffers in one go, and how to use it with arrays, boost::array or
873    * std::vector.
874    */
875   template <typename MutableBufferSequence>
876   std::size_t receive_from(const MutableBufferSequence& buffers,
877       endpoint_type& sender_endpoint)
878   {
879     boost::system::error_code ec;
880     std::size_t s = this->impl_.get_service().receive_from(
881         this->impl_.get_implementation(), buffers, sender_endpoint, 0, ec);
882     boost::asio::detail::throw_error(ec, "receive_from");
883     return s;
884   }
885   
886   /// Receive raw data with the endpoint of the sender.
887   /**
888    * This function is used to receive raw data. The function call will block
889    * until data has been received successfully or an error occurs.
890    *
891    * @param buffers One or more buffers into which the data will be received.
892    *
893    * @param sender_endpoint An endpoint object that receives the endpoint of
894    * the remote sender of the data.
895    *
896    * @param flags Flags specifying how the receive call is to be made.
897    *
898    * @returns The number of bytes received.
899    *
900    * @throws boost::system::system_error Thrown on failure.
901    */
902   template <typename MutableBufferSequence>
903   std::size_t receive_from(const MutableBufferSequence& buffers,
904       endpoint_type& sender_endpoint, socket_base::message_flags flags)
905   {
906     boost::system::error_code ec;
907     std::size_t s = this->impl_.get_service().receive_from(
908         this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
909     boost::asio::detail::throw_error(ec, "receive_from");
910     return s;
911   }
912   
913   /// Receive raw data with the endpoint of the sender.
914   /**
915    * This function is used to receive raw data. The function call will block
916    * until data has been received successfully or an error occurs.
917    *
918    * @param buffers One or more buffers into which the data will be received.
919    *
920    * @param sender_endpoint An endpoint object that receives the endpoint of
921    * the remote sender of the data.
922    *
923    * @param flags Flags specifying how the receive call is to be made.
924    *
925    * @param ec Set to indicate what error occurred, if any.
926    *
927    * @returns The number of bytes received.
928    */
929   template <typename MutableBufferSequence>
930   std::size_t receive_from(const MutableBufferSequence& buffers,
931       endpoint_type& sender_endpoint, socket_base::message_flags flags,
932       boost::system::error_code& ec)
933   {
934     return this->impl_.get_service().receive_from(
935         this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
936   }
937
938   /// Start an asynchronous receive.
939   /**
940    * This function is used to asynchronously receive raw data. The function
941    * call always returns immediately.
942    *
943    * @param buffers One or more buffers into which the data will be received.
944    * Although the buffers object may be copied as necessary, ownership of the
945    * underlying memory blocks is retained by the caller, which must guarantee
946    * that they remain valid until the handler is called.
947    *
948    * @param sender_endpoint An endpoint object that receives the endpoint of
949    * the remote sender of the data. Ownership of the sender_endpoint object
950    * is retained by the caller, which must guarantee that it is valid until the
951    * handler is called.
952    *
953    * @param handler The handler to be called when the receive operation
954    * completes. Copies will be made of the handler as required. The function
955    * signature of the handler must be:
956    * @code void handler(
957    *   const boost::system::error_code& error, // Result of operation.
958    *   std::size_t bytes_transferred           // Number of bytes received.
959    * ); @endcode
960    * Regardless of whether the asynchronous operation completes immediately or
961    * not, the handler will not be invoked from within this function. On
962    * immediate completion, invocation of the handler will be performed in a
963    * manner equivalent to using boost::asio::post().
964    *
965    * @par Example
966    * To receive into a single data buffer use the @ref buffer function as
967    * follows:
968    * @code socket.async_receive_from(
969    *     boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
970    * See the @ref buffer documentation for information on receiving into
971    * multiple buffers in one go, and how to use it with arrays, boost::array or
972    * std::vector.
973    */
974   template <typename MutableBufferSequence, typename ReadHandler>
975   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
976       void (boost::system::error_code, std::size_t))
977   async_receive_from(const MutableBufferSequence& buffers,
978       endpoint_type& sender_endpoint,
979       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
980   {
981     return async_initiate<ReadHandler,
982       void (boost::system::error_code, std::size_t)>(
983         initiate_async_receive_from(), handler, this, buffers,
984         &sender_endpoint, socket_base::message_flags(0));
985   }
986
987   /// Start an asynchronous receive.
988   /**
989    * This function is used to asynchronously receive raw data. The function
990    * call always returns immediately.
991    *
992    * @param buffers One or more buffers into which the data will be received.
993    * Although the buffers object may be copied as necessary, ownership of the
994    * underlying memory blocks is retained by the caller, which must guarantee
995    * that they remain valid until the handler is called.
996    *
997    * @param sender_endpoint An endpoint object that receives the endpoint of
998    * the remote sender of the data. Ownership of the sender_endpoint object
999    * is retained by the caller, which must guarantee that it is valid until the
1000    * handler is called.
1001    *
1002    * @param flags Flags specifying how the receive call is to be made.
1003    *
1004    * @param handler The handler to be called when the receive operation
1005    * completes. Copies will be made of the handler as required. The function
1006    * signature of the handler must be:
1007    * @code void handler(
1008    *   const boost::system::error_code& error, // Result of operation.
1009    *   std::size_t bytes_transferred           // Number of bytes received.
1010    * ); @endcode
1011    * Regardless of whether the asynchronous operation completes immediately or
1012    * not, the handler will not be invoked from within this function. On
1013    * immediate completion, invocation of the handler will be performed in a
1014    * manner equivalent to using boost::asio::post().
1015    */
1016   template <typename MutableBufferSequence, typename ReadHandler>
1017   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
1018       void (boost::system::error_code, std::size_t))
1019   async_receive_from(const MutableBufferSequence& buffers,
1020       endpoint_type& sender_endpoint, socket_base::message_flags flags,
1021       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
1022   {
1023     return async_initiate<ReadHandler,
1024       void (boost::system::error_code, std::size_t)>(
1025         initiate_async_receive_from(), handler,
1026         this, buffers, &sender_endpoint, flags);
1027   }
1028
1029 private:
1030   struct initiate_async_send
1031   {
1032     template <typename WriteHandler, typename ConstBufferSequence>
1033     void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1034         basic_raw_socket* self, const ConstBufferSequence& buffers,
1035         socket_base::message_flags flags) const
1036     {
1037       // If you get an error on the following line it means that your handler
1038       // does not meet the documented type requirements for a WriteHandler.
1039       BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1040
1041       detail::non_const_lvalue<WriteHandler> handler2(handler);
1042       self->impl_.get_service().async_send(
1043           self->impl_.get_implementation(), buffers, flags,
1044           handler2.value, self->impl_.get_implementation_executor());
1045     }
1046   };
1047
1048   struct initiate_async_send_to
1049   {
1050     template <typename WriteHandler, typename ConstBufferSequence>
1051     void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1052         basic_raw_socket* self, const ConstBufferSequence& buffers,
1053         const endpoint_type& destination,
1054         socket_base::message_flags flags) const
1055     {
1056       // If you get an error on the following line it means that your handler
1057       // does not meet the documented type requirements for a WriteHandler.
1058       BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1059
1060       detail::non_const_lvalue<WriteHandler> handler2(handler);
1061       self->impl_.get_service().async_send_to(
1062           self->impl_.get_implementation(), buffers, destination, flags,
1063           handler2.value, self->impl_.get_implementation_executor());
1064     }
1065   };
1066
1067   struct initiate_async_receive
1068   {
1069     template <typename ReadHandler, typename MutableBufferSequence>
1070     void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1071         basic_raw_socket* self, const MutableBufferSequence& buffers,
1072         socket_base::message_flags flags) const
1073     {
1074       // If you get an error on the following line it means that your handler
1075       // does not meet the documented type requirements for a ReadHandler.
1076       BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1077
1078       detail::non_const_lvalue<ReadHandler> handler2(handler);
1079       self->impl_.get_service().async_receive(
1080           self->impl_.get_implementation(), buffers, flags,
1081           handler2.value, self->impl_.get_implementation_executor());
1082     }
1083   };
1084
1085   struct initiate_async_receive_from
1086   {
1087     template <typename ReadHandler, typename MutableBufferSequence>
1088     void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1089         basic_raw_socket* self, const MutableBufferSequence& buffers,
1090         endpoint_type* sender_endpoint, socket_base::message_flags flags) const
1091     {
1092       // If you get an error on the following line it means that your handler
1093       // does not meet the documented type requirements for a ReadHandler.
1094       BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1095
1096       detail::non_const_lvalue<ReadHandler> handler2(handler);
1097       self->impl_.get_service().async_receive_from(
1098           self->impl_.get_implementation(), buffers, *sender_endpoint, flags,
1099           handler2.value, self->impl_.get_implementation_executor());
1100     }
1101   };
1102 };
1103
1104 } // namespace asio
1105 } // namespace boost
1106
1107 #include <boost/asio/detail/pop_options.hpp>
1108
1109 #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP