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) BOOST_ASIO_NOEXCEPT
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,
444 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
445 std::size_t)) WriteHandler
446 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
447 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
448 void (boost::system::error_code, std::size_t))
449 async_send(const ConstBufferSequence& buffers,
450 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
451 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
453 return async_initiate<WriteHandler,
454 void (boost::system::error_code, std::size_t)>(
455 initiate_async_send(this), handler,
456 buffers, socket_base::message_flags(0));
459 /// Start an asynchronous send on a connected socket.
461 * This function is used to send data on the raw socket. The function call
462 * will block until the data has been sent successfully or an error occurs.
464 * @param buffers One or more data buffers to be sent on the socket. Although
465 * the buffers object may be copied as necessary, ownership of the underlying
466 * memory blocks is retained by the caller, which must guarantee that they
467 * remain valid until the handler is called.
469 * @param flags Flags specifying how the send call is to be made.
471 * @param handler The handler to be called when the send operation completes.
472 * Copies will be made of the handler as required. The function signature of
473 * the handler must be:
474 * @code void handler(
475 * const boost::system::error_code& error, // Result of operation.
476 * std::size_t bytes_transferred // Number of bytes sent.
478 * Regardless of whether the asynchronous operation completes immediately or
479 * not, the handler will not be invoked from within this function. On
480 * immediate completion, invocation of the handler will be performed in a
481 * manner equivalent to using boost::asio::post().
483 * @note The async_send operation can only be used with a connected socket.
484 * Use the async_send_to function to send data on an unconnected raw
487 template <typename ConstBufferSequence,
488 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
489 std::size_t)) WriteHandler
490 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
491 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
492 void (boost::system::error_code, std::size_t))
493 async_send(const ConstBufferSequence& buffers,
494 socket_base::message_flags flags,
495 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
496 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
498 return async_initiate<WriteHandler,
499 void (boost::system::error_code, std::size_t)>(
500 initiate_async_send(this), handler, buffers, flags);
503 /// Send raw data to the specified endpoint.
505 * This function is used to send raw data to the specified remote endpoint.
506 * The function call will block until the data has been sent successfully or
509 * @param buffers One or more data buffers to be sent to the remote endpoint.
511 * @param destination The remote endpoint to which the data will be sent.
513 * @returns The number of bytes sent.
515 * @throws boost::system::system_error Thrown on failure.
518 * To send a single data buffer use the @ref buffer function as follows:
520 * boost::asio::ip::udp::endpoint destination(
521 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
522 * socket.send_to(boost::asio::buffer(data, size), destination);
524 * See the @ref buffer documentation for information on sending multiple
525 * buffers in one go, and how to use it with arrays, boost::array or
528 template <typename ConstBufferSequence>
529 std::size_t send_to(const ConstBufferSequence& buffers,
530 const endpoint_type& destination)
532 boost::system::error_code ec;
533 std::size_t s = this->impl_.get_service().send_to(
534 this->impl_.get_implementation(), buffers, destination, 0, ec);
535 boost::asio::detail::throw_error(ec, "send_to");
539 /// Send raw data to the specified endpoint.
541 * This function is used to send raw data to the specified remote endpoint.
542 * The function call will block until the data has been sent successfully or
545 * @param buffers One or more data buffers to be sent to the remote endpoint.
547 * @param destination The remote endpoint to which the data will be sent.
549 * @param flags Flags specifying how the send call is to be made.
551 * @returns The number of bytes sent.
553 * @throws boost::system::system_error Thrown on failure.
555 template <typename ConstBufferSequence>
556 std::size_t send_to(const ConstBufferSequence& buffers,
557 const endpoint_type& destination, socket_base::message_flags flags)
559 boost::system::error_code ec;
560 std::size_t s = this->impl_.get_service().send_to(
561 this->impl_.get_implementation(), buffers, destination, flags, ec);
562 boost::asio::detail::throw_error(ec, "send_to");
566 /// Send raw data to the specified endpoint.
568 * This function is used to send raw data to the specified remote endpoint.
569 * The function call will block until the data has been sent successfully or
572 * @param buffers One or more data buffers to be sent to the remote endpoint.
574 * @param destination The remote endpoint to which the data will be sent.
576 * @param flags Flags specifying how the send call is to be made.
578 * @param ec Set to indicate what error occurred, if any.
580 * @returns The number of bytes sent.
582 template <typename ConstBufferSequence>
583 std::size_t send_to(const ConstBufferSequence& buffers,
584 const endpoint_type& destination, socket_base::message_flags flags,
585 boost::system::error_code& ec)
587 return this->impl_.get_service().send_to(this->impl_.get_implementation(),
588 buffers, destination, flags, ec);
591 /// Start an asynchronous send.
593 * This function is used to asynchronously send raw data to the specified
594 * remote endpoint. The function call always returns immediately.
596 * @param buffers One or more data buffers to be sent to the remote endpoint.
597 * Although the buffers object may be copied as necessary, ownership of the
598 * underlying memory blocks is retained by the caller, which must guarantee
599 * that they remain valid until the handler is called.
601 * @param destination The remote endpoint to which the data will be sent.
602 * Copies will be made of the endpoint as required.
604 * @param handler The handler to be called when the send operation completes.
605 * Copies will be made of the handler as required. The function signature of
606 * the handler must be:
607 * @code void handler(
608 * const boost::system::error_code& error, // Result of operation.
609 * std::size_t bytes_transferred // Number of bytes sent.
611 * Regardless of whether the asynchronous operation completes immediately or
612 * not, the handler will not be invoked from within this function. On
613 * immediate completion, invocation of the handler will be performed in a
614 * manner equivalent to using boost::asio::post().
617 * To send a single data buffer use the @ref buffer function as follows:
619 * boost::asio::ip::udp::endpoint destination(
620 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
621 * socket.async_send_to(
622 * boost::asio::buffer(data, size), destination, handler);
624 * See the @ref buffer documentation for information on sending multiple
625 * buffers in one go, and how to use it with arrays, boost::array or
628 template <typename ConstBufferSequence,
629 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
630 std::size_t)) WriteHandler
631 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
632 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
633 void (boost::system::error_code, std::size_t))
634 async_send_to(const ConstBufferSequence& buffers,
635 const endpoint_type& destination,
636 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
637 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
639 return async_initiate<WriteHandler,
640 void (boost::system::error_code, std::size_t)>(
641 initiate_async_send_to(this), handler, buffers,
642 destination, socket_base::message_flags(0));
645 /// Start an asynchronous send.
647 * This function is used to asynchronously send raw data to the specified
648 * remote endpoint. The function call always returns immediately.
650 * @param buffers One or more data buffers to be sent to the remote endpoint.
651 * Although the buffers object may be copied as necessary, ownership of the
652 * underlying memory blocks is retained by the caller, which must guarantee
653 * that they remain valid until the handler is called.
655 * @param flags Flags specifying how the send call is to be made.
657 * @param destination The remote endpoint to which the data will be sent.
658 * Copies will be made of the endpoint as required.
660 * @param handler The handler to be called when the send operation completes.
661 * Copies will be made of the handler as required. The function signature of
662 * the handler must be:
663 * @code void handler(
664 * const boost::system::error_code& error, // Result of operation.
665 * std::size_t bytes_transferred // Number of bytes sent.
667 * Regardless of whether the asynchronous operation completes immediately or
668 * not, the handler will not be invoked from within this function. On
669 * immediate completion, invocation of the handler will be performed in a
670 * manner equivalent to using boost::asio::post().
672 template <typename ConstBufferSequence,
673 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
674 std::size_t)) WriteHandler
675 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
676 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
677 void (boost::system::error_code, std::size_t))
678 async_send_to(const ConstBufferSequence& buffers,
679 const endpoint_type& destination, socket_base::message_flags flags,
680 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
681 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
683 return async_initiate<WriteHandler,
684 void (boost::system::error_code, std::size_t)>(
685 initiate_async_send_to(this), handler, buffers, destination, flags);
688 /// Receive some data on a connected socket.
690 * This function is used to receive data on the raw socket. The function
691 * call will block until data has been received successfully or an error
694 * @param buffers One or more buffers into which the data will be received.
696 * @returns The number of bytes received.
698 * @throws boost::system::system_error Thrown on failure.
700 * @note The receive operation can only be used with a connected socket. Use
701 * the receive_from function to receive data on an unconnected raw
705 * To receive into a single data buffer use the @ref buffer function as
707 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
708 * See the @ref buffer documentation for information on receiving into
709 * multiple buffers in one go, and how to use it with arrays, boost::array or
712 template <typename MutableBufferSequence>
713 std::size_t receive(const MutableBufferSequence& buffers)
715 boost::system::error_code ec;
716 std::size_t s = this->impl_.get_service().receive(
717 this->impl_.get_implementation(), buffers, 0, ec);
718 boost::asio::detail::throw_error(ec, "receive");
722 /// Receive some data on a connected socket.
724 * This function is used to receive data on the raw socket. The function
725 * call will block until data has been received successfully or an error
728 * @param buffers One or more buffers into which the data will be received.
730 * @param flags Flags specifying how the receive call is to be made.
732 * @returns The number of bytes received.
734 * @throws boost::system::system_error Thrown on failure.
736 * @note The receive operation can only be used with a connected socket. Use
737 * the receive_from function to receive data on an unconnected raw
740 template <typename MutableBufferSequence>
741 std::size_t receive(const MutableBufferSequence& buffers,
742 socket_base::message_flags flags)
744 boost::system::error_code ec;
745 std::size_t s = this->impl_.get_service().receive(
746 this->impl_.get_implementation(), buffers, flags, ec);
747 boost::asio::detail::throw_error(ec, "receive");
751 /// Receive some data on a connected socket.
753 * This function is used to receive data on the raw socket. The function
754 * call will block until data has been received successfully or an error
757 * @param buffers One or more buffers into which the data will be received.
759 * @param flags Flags specifying how the receive call is to be made.
761 * @param ec Set to indicate what error occurred, if any.
763 * @returns The number of bytes received.
765 * @note The receive operation can only be used with a connected socket. Use
766 * the receive_from function to receive data on an unconnected raw
769 template <typename MutableBufferSequence>
770 std::size_t receive(const MutableBufferSequence& buffers,
771 socket_base::message_flags flags, boost::system::error_code& ec)
773 return this->impl_.get_service().receive(
774 this->impl_.get_implementation(), buffers, flags, ec);
777 /// Start an asynchronous receive on a connected socket.
779 * This function is used to asynchronously receive data from the raw
780 * socket. The function call always returns immediately.
782 * @param buffers One or more buffers into which the data will be received.
783 * Although the buffers object may be copied as necessary, ownership of the
784 * underlying memory blocks is retained by the caller, which must guarantee
785 * that they remain valid until the handler is called.
787 * @param handler The handler to be called when the receive operation
788 * completes. Copies will be made of the handler as required. The function
789 * signature of the handler must be:
790 * @code void handler(
791 * const boost::system::error_code& error, // Result of operation.
792 * std::size_t bytes_transferred // Number of bytes received.
794 * Regardless of whether the asynchronous operation completes immediately or
795 * not, the handler will not be invoked from within this function. On
796 * immediate completion, invocation of the handler will be performed in a
797 * manner equivalent to using boost::asio::post().
799 * @note The async_receive operation can only be used with a connected socket.
800 * Use the async_receive_from function to receive data on an unconnected
804 * To receive into a single data buffer use the @ref buffer function as
807 * socket.async_receive(boost::asio::buffer(data, size), handler);
809 * See the @ref buffer documentation for information on receiving into
810 * multiple buffers in one go, and how to use it with arrays, boost::array or
813 template <typename MutableBufferSequence,
814 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
815 std::size_t)) ReadHandler
816 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
817 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
818 void (boost::system::error_code, std::size_t))
819 async_receive(const MutableBufferSequence& buffers,
820 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
821 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
823 return async_initiate<ReadHandler,
824 void (boost::system::error_code, std::size_t)>(
825 initiate_async_receive(this), handler,
826 buffers, socket_base::message_flags(0));
829 /// Start an asynchronous receive on a connected socket.
831 * This function is used to asynchronously receive data from the raw
832 * socket. The function call always returns immediately.
834 * @param buffers One or more buffers into which the data will be received.
835 * Although the buffers object may be copied as necessary, ownership of the
836 * underlying memory blocks is retained by the caller, which must guarantee
837 * that they remain valid until the handler is called.
839 * @param flags Flags specifying how the receive call is to be made.
841 * @param handler The handler to be called when the receive operation
842 * completes. Copies will be made of the handler as required. The function
843 * signature of the handler must be:
844 * @code void handler(
845 * const boost::system::error_code& error, // Result of operation.
846 * std::size_t bytes_transferred // Number of bytes received.
848 * Regardless of whether the asynchronous operation completes immediately or
849 * not, the handler will not be invoked from within this function. On
850 * immediate completion, invocation of the handler will be performed in a
851 * manner equivalent to using boost::asio::post().
853 * @note The async_receive operation can only be used with a connected socket.
854 * Use the async_receive_from function to receive data on an unconnected
857 template <typename MutableBufferSequence,
858 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
859 std::size_t)) ReadHandler
860 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
861 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
862 void (boost::system::error_code, std::size_t))
863 async_receive(const MutableBufferSequence& buffers,
864 socket_base::message_flags flags,
865 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
866 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
868 return async_initiate<ReadHandler,
869 void (boost::system::error_code, std::size_t)>(
870 initiate_async_receive(this), handler, buffers, flags);
873 /// Receive raw data with the endpoint of the sender.
875 * This function is used to receive raw data. The function call will block
876 * until data has been received successfully or an error occurs.
878 * @param buffers One or more buffers into which the data will be received.
880 * @param sender_endpoint An endpoint object that receives the endpoint of
881 * the remote sender of the data.
883 * @returns The number of bytes received.
885 * @throws boost::system::system_error Thrown on failure.
888 * To receive into a single data buffer use the @ref buffer function as
891 * boost::asio::ip::udp::endpoint sender_endpoint;
892 * socket.receive_from(
893 * boost::asio::buffer(data, size), sender_endpoint);
895 * See the @ref buffer documentation for information on receiving into
896 * multiple buffers in one go, and how to use it with arrays, boost::array or
899 template <typename MutableBufferSequence>
900 std::size_t receive_from(const MutableBufferSequence& buffers,
901 endpoint_type& sender_endpoint)
903 boost::system::error_code ec;
904 std::size_t s = this->impl_.get_service().receive_from(
905 this->impl_.get_implementation(), buffers, sender_endpoint, 0, ec);
906 boost::asio::detail::throw_error(ec, "receive_from");
910 /// Receive raw data with the endpoint of the sender.
912 * This function is used to receive raw data. The function call will block
913 * until data has been received successfully or an error occurs.
915 * @param buffers One or more buffers into which the data will be received.
917 * @param sender_endpoint An endpoint object that receives the endpoint of
918 * the remote sender of the data.
920 * @param flags Flags specifying how the receive call is to be made.
922 * @returns The number of bytes received.
924 * @throws boost::system::system_error Thrown on failure.
926 template <typename MutableBufferSequence>
927 std::size_t receive_from(const MutableBufferSequence& buffers,
928 endpoint_type& sender_endpoint, socket_base::message_flags flags)
930 boost::system::error_code ec;
931 std::size_t s = this->impl_.get_service().receive_from(
932 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
933 boost::asio::detail::throw_error(ec, "receive_from");
937 /// Receive raw data with the endpoint of the sender.
939 * This function is used to receive raw data. The function call will block
940 * until data has been received successfully or an error occurs.
942 * @param buffers One or more buffers into which the data will be received.
944 * @param sender_endpoint An endpoint object that receives the endpoint of
945 * the remote sender of the data.
947 * @param flags Flags specifying how the receive call is to be made.
949 * @param ec Set to indicate what error occurred, if any.
951 * @returns The number of bytes received.
953 template <typename MutableBufferSequence>
954 std::size_t receive_from(const MutableBufferSequence& buffers,
955 endpoint_type& sender_endpoint, socket_base::message_flags flags,
956 boost::system::error_code& ec)
958 return this->impl_.get_service().receive_from(
959 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
962 /// Start an asynchronous receive.
964 * This function is used to asynchronously receive raw data. The function
965 * call always returns immediately.
967 * @param buffers One or more buffers into which the data will be received.
968 * Although the buffers object may be copied as necessary, ownership of the
969 * underlying memory blocks is retained by the caller, which must guarantee
970 * that they remain valid until the handler is called.
972 * @param sender_endpoint An endpoint object that receives the endpoint of
973 * the remote sender of the data. Ownership of the sender_endpoint object
974 * is retained by the caller, which must guarantee that it is valid until the
977 * @param handler The handler to be called when the receive operation
978 * completes. Copies will be made of the handler as required. The function
979 * signature of the handler must be:
980 * @code void handler(
981 * const boost::system::error_code& error, // Result of operation.
982 * std::size_t bytes_transferred // Number of bytes received.
984 * Regardless of whether the asynchronous operation completes immediately or
985 * not, the handler will not be invoked from within this function. On
986 * immediate completion, invocation of the handler will be performed in a
987 * manner equivalent to using boost::asio::post().
990 * To receive into a single data buffer use the @ref buffer function as
992 * @code socket.async_receive_from(
993 * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
994 * See the @ref buffer documentation for information on receiving into
995 * multiple buffers in one go, and how to use it with arrays, boost::array or
998 template <typename MutableBufferSequence,
999 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1000 std::size_t)) ReadHandler
1001 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1002 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
1003 void (boost::system::error_code, std::size_t))
1004 async_receive_from(const MutableBufferSequence& buffers,
1005 endpoint_type& sender_endpoint,
1006 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
1007 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1009 return async_initiate<ReadHandler,
1010 void (boost::system::error_code, std::size_t)>(
1011 initiate_async_receive_from(this), handler, buffers,
1012 &sender_endpoint, socket_base::message_flags(0));
1015 /// Start an asynchronous receive.
1017 * This function is used to asynchronously receive raw data. The function
1018 * call always returns immediately.
1020 * @param buffers One or more buffers into which the data will be received.
1021 * Although the buffers object may be copied as necessary, ownership of the
1022 * underlying memory blocks is retained by the caller, which must guarantee
1023 * that they remain valid until the handler is called.
1025 * @param sender_endpoint An endpoint object that receives the endpoint of
1026 * the remote sender of the data. Ownership of the sender_endpoint object
1027 * is retained by the caller, which must guarantee that it is valid until the
1028 * handler is called.
1030 * @param flags Flags specifying how the receive call is to be made.
1032 * @param handler The handler to be called when the receive operation
1033 * completes. Copies will be made of the handler as required. The function
1034 * signature of the handler must be:
1035 * @code void handler(
1036 * const boost::system::error_code& error, // Result of operation.
1037 * std::size_t bytes_transferred // Number of bytes received.
1039 * Regardless of whether the asynchronous operation completes immediately or
1040 * not, the handler will not be invoked from within this function. On
1041 * immediate completion, invocation of the handler will be performed in a
1042 * manner equivalent to using boost::asio::post().
1044 template <typename MutableBufferSequence,
1045 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1046 std::size_t)) ReadHandler
1047 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1048 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
1049 void (boost::system::error_code, std::size_t))
1050 async_receive_from(const MutableBufferSequence& buffers,
1051 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1052 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
1053 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1055 return async_initiate<ReadHandler,
1056 void (boost::system::error_code, std::size_t)>(
1057 initiate_async_receive_from(this), handler,
1058 buffers, &sender_endpoint, flags);
1062 class initiate_async_send
1065 typedef Executor executor_type;
1067 explicit initiate_async_send(basic_raw_socket* self)
1072 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1074 return self_->get_executor();
1077 template <typename WriteHandler, typename ConstBufferSequence>
1078 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1079 const ConstBufferSequence& buffers,
1080 socket_base::message_flags flags) const
1082 // If you get an error on the following line it means that your handler
1083 // does not meet the documented type requirements for a WriteHandler.
1084 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1086 detail::non_const_lvalue<WriteHandler> handler2(handler);
1087 self_->impl_.get_service().async_send(
1088 self_->impl_.get_implementation(), buffers, flags,
1089 handler2.value, self_->impl_.get_implementation_executor());
1093 basic_raw_socket* self_;
1096 class initiate_async_send_to
1099 typedef Executor executor_type;
1101 explicit initiate_async_send_to(basic_raw_socket* self)
1106 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1108 return self_->get_executor();
1111 template <typename WriteHandler, typename ConstBufferSequence>
1112 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1113 const ConstBufferSequence& buffers, const endpoint_type& destination,
1114 socket_base::message_flags flags) const
1116 // If you get an error on the following line it means that your handler
1117 // does not meet the documented type requirements for a WriteHandler.
1118 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1120 detail::non_const_lvalue<WriteHandler> handler2(handler);
1121 self_->impl_.get_service().async_send_to(
1122 self_->impl_.get_implementation(), buffers, destination, flags,
1123 handler2.value, self_->impl_.get_implementation_executor());
1127 basic_raw_socket* self_;
1130 class initiate_async_receive
1133 typedef Executor executor_type;
1135 explicit initiate_async_receive(basic_raw_socket* self)
1140 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1142 return self_->get_executor();
1145 template <typename ReadHandler, typename MutableBufferSequence>
1146 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1147 const MutableBufferSequence& buffers,
1148 socket_base::message_flags flags) const
1150 // If you get an error on the following line it means that your handler
1151 // does not meet the documented type requirements for a ReadHandler.
1152 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1154 detail::non_const_lvalue<ReadHandler> handler2(handler);
1155 self_->impl_.get_service().async_receive(
1156 self_->impl_.get_implementation(), buffers, flags,
1157 handler2.value, self_->impl_.get_implementation_executor());
1161 basic_raw_socket* self_;
1164 class initiate_async_receive_from
1167 typedef Executor executor_type;
1169 explicit initiate_async_receive_from(basic_raw_socket* self)
1174 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1176 return self_->get_executor();
1179 template <typename ReadHandler, typename MutableBufferSequence>
1180 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1181 const MutableBufferSequence& buffers, endpoint_type* sender_endpoint,
1182 socket_base::message_flags flags) const
1184 // If you get an error on the following line it means that your handler
1185 // does not meet the documented type requirements for a ReadHandler.
1186 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1188 detail::non_const_lvalue<ReadHandler> handler2(handler);
1189 self_->impl_.get_service().async_receive_from(
1190 self_->impl_.get_implementation(), buffers, *sender_endpoint, flags,
1191 handler2.value, self_->impl_.get_implementation_executor());
1195 basic_raw_socket* self_;
1200 } // namespace boost
1202 #include <boost/asio/detail/pop_options.hpp>
1204 #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP