2 // basic_raw_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_BASIC_RAW_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_RAW_SOCKET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
27 #include <boost/asio/detail/push_options.hpp>
32 #if !defined(BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL)
33 #define BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL
35 // Forward declaration with defaulted arguments.
36 template <typename Protocol, typename Executor = executor>
37 class basic_raw_socket;
39 #endif // !defined(BOOST_ASIO_BASIC_RAW_SOCKET_FWD_DECL)
41 /// Provides raw-oriented socket functionality.
43 * The basic_raw_socket class template provides asynchronous and blocking
44 * raw-oriented socket functionality.
47 * @e Distinct @e objects: Safe.@n
48 * @e Shared @e objects: Unsafe.
50 template <typename Protocol, typename Executor>
51 class basic_raw_socket
52 : public basic_socket<Protocol, Executor>
55 /// The type of the executor associated with the object.
56 typedef Executor executor_type;
58 /// Rebinds the socket type to another executor.
59 template <typename Executor1>
60 struct rebind_executor
62 /// The socket type when rebound to the specified executor.
63 typedef basic_raw_socket<Protocol, Executor1> other;
66 /// The native representation of a socket.
67 #if defined(GENERATING_DOCUMENTATION)
68 typedef implementation_defined native_handle_type;
70 typedef typename basic_socket<Protocol,
71 Executor>::native_handle_type native_handle_type;
74 /// The protocol type.
75 typedef Protocol protocol_type;
77 /// The endpoint type.
78 typedef typename Protocol::endpoint endpoint_type;
80 /// Construct a basic_raw_socket without opening it.
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.
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.
88 explicit basic_raw_socket(const executor_type& ex)
89 : basic_socket<Protocol, Executor>(ex)
93 /// Construct a basic_raw_socket without opening it.
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.
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.
102 template <typename ExecutionContext>
103 explicit basic_raw_socket(ExecutionContext& context,
105 is_convertible<ExecutionContext&, execution_context&>::value
107 : basic_socket<Protocol, Executor>(context)
111 /// Construct and open a basic_raw_socket.
113 * This constructor creates and opens a raw socket.
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.
118 * @param protocol An object specifying protocol parameters to be used.
120 * @throws boost::system::system_error Thrown on failure.
122 basic_raw_socket(const executor_type& ex, const protocol_type& protocol)
123 : basic_socket<Protocol, Executor>(ex, protocol)
127 /// Construct and open a basic_raw_socket.
129 * This constructor creates and opens a raw socket.
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.
135 * @param protocol An object specifying protocol parameters to be used.
137 * @throws boost::system::system_error Thrown on failure.
139 template <typename ExecutionContext>
140 basic_raw_socket(ExecutionContext& context, const protocol_type& protocol,
142 is_convertible<ExecutionContext&, execution_context&>::value
144 : basic_socket<Protocol, Executor>(context, protocol)
148 /// Construct a basic_raw_socket, opening it and binding it to the given
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.
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.
158 * @param endpoint An endpoint on the local machine to which the raw
159 * socket will be bound.
161 * @throws boost::system::system_error Thrown on failure.
163 basic_raw_socket(const executor_type& ex, const endpoint_type& endpoint)
164 : basic_socket<Protocol, Executor>(ex, endpoint)
168 /// Construct a basic_raw_socket, opening it and binding it to the given
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.
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.
179 * @param endpoint An endpoint on the local machine to which the raw
180 * socket will be bound.
182 * @throws boost::system::system_error Thrown on failure.
184 template <typename ExecutionContext>
185 basic_raw_socket(ExecutionContext& context, const endpoint_type& endpoint,
187 is_convertible<ExecutionContext&, execution_context&>::value
189 : basic_socket<Protocol, Executor>(context, endpoint)
193 /// Construct a basic_raw_socket on an existing native socket.
195 * This constructor creates a raw socket object to hold an existing
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.
201 * @param protocol An object specifying protocol parameters to be used.
203 * @param native_socket The new underlying socket implementation.
205 * @throws boost::system::system_error Thrown on failure.
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)
213 /// Construct a basic_raw_socket on an existing native socket.
215 * This constructor creates a raw socket object to hold an existing
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.
222 * @param protocol An object specifying protocol parameters to be used.
224 * @param native_socket The new underlying socket implementation.
226 * @throws boost::system::system_error Thrown on failure.
228 template <typename ExecutionContext>
229 basic_raw_socket(ExecutionContext& context,
230 const protocol_type& protocol, const native_handle_type& native_socket,
232 is_convertible<ExecutionContext&, execution_context&>::value
234 : basic_socket<Protocol, Executor>(context, protocol, native_socket)
238 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
239 /// Move-construct a basic_raw_socket from another.
241 * This constructor moves a raw socket from one object to another.
243 * @param other The other basic_raw_socket object from which the move
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&)
250 basic_raw_socket(basic_raw_socket&& other)
251 : basic_socket<Protocol, Executor>(std::move(other))
255 /// Move-assign a basic_raw_socket from another.
257 * This assignment operator moves a raw socket from one object to another.
259 * @param other The other basic_raw_socket object from which the move
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&)
266 basic_raw_socket& operator=(basic_raw_socket&& other)
268 basic_socket<Protocol, Executor>::operator=(std::move(other));
272 /// Move-construct a basic_raw_socket from a socket of another protocol
275 * This constructor moves a raw socket from one object to another.
277 * @param other The other basic_raw_socket object from which the move
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&)
284 template <typename Protocol1, typename Executor1>
285 basic_raw_socket(basic_raw_socket<Protocol1, Executor1>&& other,
287 is_convertible<Protocol1, Protocol>::value
288 && is_convertible<Executor1, Executor>::value
290 : basic_socket<Protocol, Executor>(std::move(other))
294 /// Move-assign a basic_raw_socket from a socket of another protocol type.
296 * This assignment operator moves a raw socket from one object to another.
298 * @param other The other basic_raw_socket object from which the move
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&)
305 template <typename Protocol1, typename Executor1>
307 is_convertible<Protocol1, Protocol>::value
308 && is_convertible<Executor1, Executor>::value,
310 >::type operator=(basic_raw_socket<Protocol1, Executor1>&& other)
312 basic_socket<Protocol, Executor>::operator=(std::move(other));
315 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
317 /// Destroys the socket.
319 * This function destroys the socket, cancelling any outstanding asynchronous
320 * operations associated with the socket as if by calling @c cancel.
326 /// Send some data on a connected socket.
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.
331 * @param buffers One ore more data buffers to be sent on the socket.
333 * @returns The number of bytes sent.
335 * @throws boost::system::system_error Thrown on failure.
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.
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
347 template <typename ConstBufferSequence>
348 std::size_t send(const ConstBufferSequence& buffers)
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");
357 /// Send some data on a connected socket.
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.
362 * @param buffers One ore more data buffers to be sent on the socket.
364 * @param flags Flags specifying how the send call is to be made.
366 * @returns The number of bytes sent.
368 * @throws boost::system::system_error Thrown on failure.
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.
373 template <typename ConstBufferSequence>
374 std::size_t send(const ConstBufferSequence& buffers,
375 socket_base::message_flags flags)
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");
384 /// Send some data on a connected socket.
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.
389 * @param buffers One or more data buffers to be sent on the socket.
391 * @param flags Flags specifying how the send call is to be made.
393 * @param ec Set to indicate what error occurred, if any.
395 * @returns The number of bytes sent.
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.
400 template <typename ConstBufferSequence>
401 std::size_t send(const ConstBufferSequence& buffers,
402 socket_base::message_flags flags, boost::system::error_code& ec)
404 return this->impl_.get_service().send(
405 this->impl_.get_implementation(), buffers, flags, ec);
408 /// Start an asynchronous send on a connected socket.
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.
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.
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.
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().
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
435 * To send a single data buffer use the @ref buffer function as follows:
437 * socket.async_send(boost::asio::buffer(data, size), handler);
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
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)
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));
455 /// Start an asynchronous send on a connected socket.
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.
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.
465 * @param flags Flags specifying how the send call is to be made.
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.
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().
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
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)
490 return async_initiate<WriteHandler,
491 void (boost::system::error_code, std::size_t)>(
492 initiate_async_send(), handler, this, buffers, flags);
495 /// Send raw data to the specified endpoint.
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
501 * @param buffers One or more data buffers to be sent to the remote endpoint.
503 * @param destination The remote endpoint to which the data will be sent.
505 * @returns The number of bytes sent.
507 * @throws boost::system::system_error Thrown on failure.
510 * To send a single data buffer use the @ref buffer function as follows:
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);
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
520 template <typename ConstBufferSequence>
521 std::size_t send_to(const ConstBufferSequence& buffers,
522 const endpoint_type& destination)
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");
531 /// Send raw data to the specified endpoint.
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
537 * @param buffers One or more data buffers to be sent to the remote endpoint.
539 * @param destination The remote endpoint to which the data will be sent.
541 * @param flags Flags specifying how the send call is to be made.
543 * @returns The number of bytes sent.
545 * @throws boost::system::system_error Thrown on failure.
547 template <typename ConstBufferSequence>
548 std::size_t send_to(const ConstBufferSequence& buffers,
549 const endpoint_type& destination, socket_base::message_flags flags)
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");
558 /// Send raw data to the specified endpoint.
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
564 * @param buffers One or more data buffers to be sent to the remote endpoint.
566 * @param destination The remote endpoint to which the data will be sent.
568 * @param flags Flags specifying how the send call is to be made.
570 * @param ec Set to indicate what error occurred, if any.
572 * @returns The number of bytes sent.
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)
579 return this->impl_.get_service().send_to(this->impl_.get_implementation(),
580 buffers, destination, flags, ec);
583 /// Start an asynchronous send.
585 * This function is used to asynchronously send raw data to the specified
586 * remote endpoint. The function call always returns immediately.
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.
593 * @param destination The remote endpoint to which the data will be sent.
594 * Copies will be made of the endpoint as required.
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.
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().
609 * To send a single data buffer use the @ref buffer function as follows:
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);
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
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)
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));
633 /// Start an asynchronous send.
635 * This function is used to asynchronously send raw data to the specified
636 * remote endpoint. The function call always returns immediately.
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.
643 * @param flags Flags specifying how the send call is to be made.
645 * @param destination The remote endpoint to which the data will be sent.
646 * Copies will be made of the endpoint as required.
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.
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().
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)
667 return async_initiate<WriteHandler,
668 void (boost::system::error_code, std::size_t)>(
669 initiate_async_send_to(), handler, this, buffers, destination, flags);
672 /// Receive some data on a connected socket.
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
678 * @param buffers One or more buffers into which the data will be received.
680 * @returns The number of bytes received.
682 * @throws boost::system::system_error Thrown on failure.
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
689 * To receive into a single data buffer use the @ref buffer function as
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
696 template <typename MutableBufferSequence>
697 std::size_t receive(const MutableBufferSequence& buffers)
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");
706 /// Receive some data on a connected socket.
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
712 * @param buffers One or more buffers into which the data will be received.
714 * @param flags Flags specifying how the receive call is to be made.
716 * @returns The number of bytes received.
718 * @throws boost::system::system_error Thrown on failure.
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
724 template <typename MutableBufferSequence>
725 std::size_t receive(const MutableBufferSequence& buffers,
726 socket_base::message_flags flags)
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");
735 /// Receive some data on a connected socket.
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
741 * @param buffers One or more buffers into which the data will be received.
743 * @param flags Flags specifying how the receive call is to be made.
745 * @param ec Set to indicate what error occurred, if any.
747 * @returns The number of bytes received.
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
753 template <typename MutableBufferSequence>
754 std::size_t receive(const MutableBufferSequence& buffers,
755 socket_base::message_flags flags, boost::system::error_code& ec)
757 return this->impl_.get_service().receive(
758 this->impl_.get_implementation(), buffers, flags, ec);
761 /// Start an asynchronous receive on a connected socket.
763 * This function is used to asynchronously receive data from the raw
764 * socket. The function call always returns immediately.
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.
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.
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().
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
788 * To receive into a single data buffer use the @ref buffer function as
791 * socket.async_receive(boost::asio::buffer(data, size), handler);
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
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)
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));
809 /// Start an asynchronous receive on a connected socket.
811 * This function is used to asynchronously receive data from the raw
812 * socket. The function call always returns immediately.
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.
819 * @param flags Flags specifying how the receive call is to be made.
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.
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().
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
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)
844 return async_initiate<ReadHandler,
845 void (boost::system::error_code, std::size_t)>(
846 initiate_async_receive(), handler, this, buffers, flags);
849 /// Receive raw data with the endpoint of the sender.
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.
854 * @param buffers One or more buffers into which the data will be received.
856 * @param sender_endpoint An endpoint object that receives the endpoint of
857 * the remote sender of the data.
859 * @returns The number of bytes received.
861 * @throws boost::system::system_error Thrown on failure.
864 * To receive into a single data buffer use the @ref buffer function as
867 * boost::asio::ip::udp::endpoint sender_endpoint;
868 * socket.receive_from(
869 * boost::asio::buffer(data, size), sender_endpoint);
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
875 template <typename MutableBufferSequence>
876 std::size_t receive_from(const MutableBufferSequence& buffers,
877 endpoint_type& sender_endpoint)
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");
886 /// Receive raw data with the endpoint of the sender.
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.
891 * @param buffers One or more buffers into which the data will be received.
893 * @param sender_endpoint An endpoint object that receives the endpoint of
894 * the remote sender of the data.
896 * @param flags Flags specifying how the receive call is to be made.
898 * @returns The number of bytes received.
900 * @throws boost::system::system_error Thrown on failure.
902 template <typename MutableBufferSequence>
903 std::size_t receive_from(const MutableBufferSequence& buffers,
904 endpoint_type& sender_endpoint, socket_base::message_flags flags)
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");
913 /// Receive raw data with the endpoint of the sender.
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.
918 * @param buffers One or more buffers into which the data will be received.
920 * @param sender_endpoint An endpoint object that receives the endpoint of
921 * the remote sender of the data.
923 * @param flags Flags specifying how the receive call is to be made.
925 * @param ec Set to indicate what error occurred, if any.
927 * @returns The number of bytes received.
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)
934 return this->impl_.get_service().receive_from(
935 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
938 /// Start an asynchronous receive.
940 * This function is used to asynchronously receive raw data. The function
941 * call always returns immediately.
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.
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
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.
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().
966 * To receive into a single data buffer use the @ref buffer function as
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
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)
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));
987 /// Start an asynchronous receive.
989 * This function is used to asynchronously receive raw data. The function
990 * call always returns immediately.
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.
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.
1002 * @param flags Flags specifying how the receive call is to be made.
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.
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().
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)
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);
1030 struct initiate_async_send
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
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;
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());
1048 struct initiate_async_send_to
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
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;
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());
1067 struct initiate_async_receive
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
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;
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());
1085 struct initiate_async_receive_from
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
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;
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());
1105 } // namespace boost
1107 #include <boost/asio/detail/pop_options.hpp>
1109 #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP