2 // basic_datagram_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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_DATAGRAM_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_DATAGRAM_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_DATAGRAM_SOCKET_FWD_DECL)
33 #define BOOST_ASIO_BASIC_DATAGRAM_SOCKET_FWD_DECL
35 // Forward declaration with defaulted arguments.
36 template <typename Protocol, typename Executor = any_io_executor>
37 class basic_datagram_socket;
39 #endif // !defined(BOOST_ASIO_BASIC_DATAGRAM_SOCKET_FWD_DECL)
41 /// Provides datagram-oriented socket functionality.
43 * The basic_datagram_socket class template provides asynchronous and blocking
44 * datagram-oriented socket functionality.
47 * @e Distinct @e objects: Safe.@n
48 * @e Shared @e objects: Unsafe.
50 * Synchronous @c send, @c send_to, @c receive, @c receive_from, and @c connect
51 * operations are thread safe with respect to each other, if the underlying
52 * operating system calls are also thread safe. This means that it is permitted
53 * to perform concurrent calls to these synchronous operations on a single
54 * socket object. Other synchronous operations, such as @c open or @c close, are
57 template <typename Protocol, typename Executor>
58 class basic_datagram_socket
59 : public basic_socket<Protocol, Executor>
62 /// The type of the executor associated with the object.
63 typedef Executor executor_type;
65 /// Rebinds the socket type to another executor.
66 template <typename Executor1>
67 struct rebind_executor
69 /// The socket type when rebound to the specified executor.
70 typedef basic_datagram_socket<Protocol, Executor1> other;
73 /// The native representation of a socket.
74 #if defined(GENERATING_DOCUMENTATION)
75 typedef implementation_defined native_handle_type;
77 typedef typename basic_socket<Protocol,
78 Executor>::native_handle_type native_handle_type;
81 /// The protocol type.
82 typedef Protocol protocol_type;
84 /// The endpoint type.
85 typedef typename Protocol::endpoint endpoint_type;
87 /// Construct a basic_datagram_socket without opening it.
89 * This constructor creates a datagram socket without opening it. The open()
90 * function must be called before data can be sent or received on the socket.
92 * @param ex The I/O executor that the socket will use, by default, to
93 * dispatch handlers for any asynchronous operations performed on the socket.
95 explicit basic_datagram_socket(const executor_type& ex)
96 : basic_socket<Protocol, Executor>(ex)
100 /// Construct a basic_datagram_socket without opening it.
102 * This constructor creates a datagram socket without opening it. The open()
103 * function must be called before data can be sent or received on the socket.
105 * @param context An execution context which provides the I/O executor that
106 * the socket will use, by default, to dispatch handlers for any asynchronous
107 * operations performed on the socket.
109 template <typename ExecutionContext>
110 explicit basic_datagram_socket(ExecutionContext& context,
112 is_convertible<ExecutionContext&, execution_context&>::value
114 : basic_socket<Protocol, Executor>(context)
118 /// Construct and open a basic_datagram_socket.
120 * This constructor creates and opens a datagram socket.
122 * @param ex The I/O executor that the socket will use, by default, to
123 * dispatch handlers for any asynchronous operations performed on the socket.
125 * @param protocol An object specifying protocol parameters to be used.
127 * @throws boost::system::system_error Thrown on failure.
129 basic_datagram_socket(const executor_type& ex, const protocol_type& protocol)
130 : basic_socket<Protocol, Executor>(ex, protocol)
134 /// Construct and open a basic_datagram_socket.
136 * This constructor creates and opens a datagram socket.
138 * @param context An execution context which provides the I/O executor that
139 * the socket will use, by default, to dispatch handlers for any asynchronous
140 * operations performed on the socket.
142 * @param protocol An object specifying protocol parameters to be used.
144 * @throws boost::system::system_error Thrown on failure.
146 template <typename ExecutionContext>
147 basic_datagram_socket(ExecutionContext& context,
148 const protocol_type& protocol,
150 is_convertible<ExecutionContext&, execution_context&>::value,
152 >::type = defaulted_constraint())
153 : basic_socket<Protocol, Executor>(context, protocol)
157 /// Construct a basic_datagram_socket, opening it and binding it to the given
160 * This constructor creates a datagram socket and automatically opens it bound
161 * to the specified endpoint on the local machine. The protocol used is the
162 * protocol associated with the given endpoint.
164 * @param ex The I/O executor that the socket will use, by default, to
165 * dispatch handlers for any asynchronous operations performed on the socket.
167 * @param endpoint An endpoint on the local machine to which the datagram
168 * socket will be bound.
170 * @throws boost::system::system_error Thrown on failure.
172 basic_datagram_socket(const executor_type& ex, const endpoint_type& endpoint)
173 : basic_socket<Protocol, Executor>(ex, endpoint)
177 /// Construct a basic_datagram_socket, opening it and binding it to the given
180 * This constructor creates a datagram socket and automatically opens it bound
181 * to the specified endpoint on the local machine. The protocol used is the
182 * protocol associated with the given endpoint.
184 * @param context An execution context which provides the I/O executor that
185 * the socket will use, by default, to dispatch handlers for any asynchronous
186 * operations performed on the socket.
188 * @param endpoint An endpoint on the local machine to which the datagram
189 * socket will be bound.
191 * @throws boost::system::system_error Thrown on failure.
193 template <typename ExecutionContext>
194 basic_datagram_socket(ExecutionContext& context,
195 const endpoint_type& endpoint,
197 is_convertible<ExecutionContext&, execution_context&>::value
199 : basic_socket<Protocol, Executor>(context, endpoint)
203 /// Construct a basic_datagram_socket on an existing native socket.
205 * This constructor creates a datagram socket object to hold an existing
208 * @param ex The I/O executor that the socket will use, by default, to
209 * dispatch handlers for any asynchronous operations performed on the socket.
211 * @param protocol An object specifying protocol parameters to be used.
213 * @param native_socket The new underlying socket implementation.
215 * @throws boost::system::system_error Thrown on failure.
217 basic_datagram_socket(const executor_type& ex,
218 const protocol_type& protocol, const native_handle_type& native_socket)
219 : basic_socket<Protocol, Executor>(ex, protocol, native_socket)
223 /// Construct a basic_datagram_socket on an existing native socket.
225 * This constructor creates a datagram socket object to hold an existing
228 * @param context An execution context which provides the I/O executor that
229 * the socket will use, by default, to dispatch handlers for any asynchronous
230 * operations performed on the socket.
232 * @param protocol An object specifying protocol parameters to be used.
234 * @param native_socket The new underlying socket implementation.
236 * @throws boost::system::system_error Thrown on failure.
238 template <typename ExecutionContext>
239 basic_datagram_socket(ExecutionContext& context,
240 const protocol_type& protocol, const native_handle_type& native_socket,
242 is_convertible<ExecutionContext&, execution_context&>::value
244 : basic_socket<Protocol, Executor>(context, protocol, native_socket)
248 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
249 /// Move-construct a basic_datagram_socket from another.
251 * This constructor moves a datagram socket from one object to another.
253 * @param other The other basic_datagram_socket object from which the move
256 * @note Following the move, the moved-from object is in the same state as if
257 * constructed using the @c basic_datagram_socket(const executor_type&)
260 basic_datagram_socket(basic_datagram_socket&& other) BOOST_ASIO_NOEXCEPT
261 : basic_socket<Protocol, Executor>(std::move(other))
265 /// Move-assign a basic_datagram_socket from another.
267 * This assignment operator moves a datagram socket from one object to
270 * @param other The other basic_datagram_socket object from which the move
273 * @note Following the move, the moved-from object is in the same state as if
274 * constructed using the @c basic_datagram_socket(const executor_type&)
277 basic_datagram_socket& operator=(basic_datagram_socket&& other)
279 basic_socket<Protocol, Executor>::operator=(std::move(other));
283 /// Move-construct a basic_datagram_socket from a socket of another protocol
286 * This constructor moves a datagram socket from one object to another.
288 * @param other The other basic_datagram_socket object from which the move
291 * @note Following the move, the moved-from object is in the same state as if
292 * constructed using the @c basic_datagram_socket(const executor_type&)
295 template <typename Protocol1, typename Executor1>
296 basic_datagram_socket(basic_datagram_socket<Protocol1, Executor1>&& other,
298 is_convertible<Protocol1, Protocol>::value
299 && is_convertible<Executor1, Executor>::value
301 : basic_socket<Protocol, Executor>(std::move(other))
305 /// Move-assign a basic_datagram_socket from a socket of another protocol
308 * This assignment operator moves a datagram socket from one object to
311 * @param other The other basic_datagram_socket object from which the move
314 * @note Following the move, the moved-from object is in the same state as if
315 * constructed using the @c basic_datagram_socket(const executor_type&)
318 template <typename Protocol1, typename Executor1>
320 is_convertible<Protocol1, Protocol>::value
321 && is_convertible<Executor1, Executor>::value,
322 basic_datagram_socket&
323 >::type operator=(basic_datagram_socket<Protocol1, Executor1>&& other)
325 basic_socket<Protocol, Executor>::operator=(std::move(other));
328 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
330 /// Destroys the socket.
332 * This function destroys the socket, cancelling any outstanding asynchronous
333 * operations associated with the socket as if by calling @c cancel.
335 ~basic_datagram_socket()
339 /// Send some data on a connected socket.
341 * This function is used to send data on the datagram socket. The function
342 * call will block until the data has been sent successfully or an error
345 * @param buffers One ore more data buffers to be sent on the socket.
347 * @returns The number of bytes sent.
349 * @throws boost::system::system_error Thrown on failure.
351 * @note The send operation can only be used with a connected socket. Use
352 * the send_to function to send data on an unconnected datagram socket.
355 * To send a single data buffer use the @ref buffer function as follows:
356 * @code socket.send(boost::asio::buffer(data, size)); @endcode
357 * See the @ref buffer documentation for information on sending multiple
358 * buffers in one go, and how to use it with arrays, boost::array or
361 template <typename ConstBufferSequence>
362 std::size_t send(const ConstBufferSequence& buffers)
364 boost::system::error_code ec;
365 std::size_t s = this->impl_.get_service().send(
366 this->impl_.get_implementation(), buffers, 0, ec);
367 boost::asio::detail::throw_error(ec, "send");
371 /// Send some data on a connected socket.
373 * This function is used to send data on the datagram socket. The function
374 * call will block until the data has been sent successfully or an error
377 * @param buffers One ore more data buffers to be sent on the socket.
379 * @param flags Flags specifying how the send call is to be made.
381 * @returns The number of bytes sent.
383 * @throws boost::system::system_error Thrown on failure.
385 * @note The send operation can only be used with a connected socket. Use
386 * the send_to function to send data on an unconnected datagram socket.
388 template <typename ConstBufferSequence>
389 std::size_t send(const ConstBufferSequence& buffers,
390 socket_base::message_flags flags)
392 boost::system::error_code ec;
393 std::size_t s = this->impl_.get_service().send(
394 this->impl_.get_implementation(), buffers, flags, ec);
395 boost::asio::detail::throw_error(ec, "send");
399 /// Send some data on a connected socket.
401 * This function is used to send data on the datagram socket. The function
402 * call will block until the data has been sent successfully or an error
405 * @param buffers One or more data buffers to be sent on the socket.
407 * @param flags Flags specifying how the send call is to be made.
409 * @param ec Set to indicate what error occurred, if any.
411 * @returns The number of bytes sent.
413 * @note The send operation can only be used with a connected socket. Use
414 * the send_to function to send data on an unconnected datagram socket.
416 template <typename ConstBufferSequence>
417 std::size_t send(const ConstBufferSequence& buffers,
418 socket_base::message_flags flags, boost::system::error_code& ec)
420 return this->impl_.get_service().send(
421 this->impl_.get_implementation(), buffers, flags, ec);
424 /// Start an asynchronous send on a connected socket.
426 * This function is used to asynchronously send data on the datagram socket.
427 * It is an initiating function for an @ref asynchronous_operation, and always
428 * returns immediately.
430 * @param buffers One or more data buffers to be sent on the socket. Although
431 * the buffers object may be copied as necessary, ownership of the underlying
432 * memory blocks is retained by the caller, which must guarantee that they
433 * remain valid until the completion handler is called.
435 * @param token The @ref completion_token that will be used to produce a
436 * completion handler, which will be called when the send completes. Potential
437 * completion tokens include @ref use_future, @ref use_awaitable, @ref
438 * yield_context, or a function object with the correct completion signature.
439 * The function signature of the completion handler must be:
440 * @code void handler(
441 * const boost::system::error_code& error, // Result of operation.
442 * std::size_t bytes_transferred // Number of bytes sent.
444 * Regardless of whether the asynchronous operation completes immediately or
445 * not, the completion handler will not be invoked from within this function.
446 * On immediate completion, invocation of the handler will be performed in a
447 * manner equivalent to using boost::asio::post().
449 * @par Completion Signature
450 * @code void(boost::system::error_code, std::size_t) @endcode
452 * @note The async_send operation can only be used with a connected socket.
453 * Use the async_send_to function to send data on an unconnected datagram
457 * To send a single data buffer use the @ref buffer function as follows:
459 * socket.async_send(boost::asio::buffer(data, size), handler);
461 * See the @ref buffer documentation for information on sending multiple
462 * buffers in one go, and how to use it with arrays, boost::array or
465 * @par Per-Operation Cancellation
466 * On POSIX or Windows operating systems, this asynchronous operation supports
467 * cancellation for the following boost::asio::cancellation_type values:
469 * @li @c cancellation_type::terminal
471 * @li @c cancellation_type::partial
473 * @li @c cancellation_type::total
475 template <typename ConstBufferSequence,
476 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
477 std::size_t)) WriteToken
478 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
479 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
480 void (boost::system::error_code, std::size_t))
481 async_send(const ConstBufferSequence& buffers,
482 BOOST_ASIO_MOVE_ARG(WriteToken) token
483 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
485 return async_initiate<WriteToken,
486 void (boost::system::error_code, std::size_t)>(
487 initiate_async_send(this), token,
488 buffers, socket_base::message_flags(0));
491 /// Start an asynchronous send on a connected socket.
493 * This function is used to asynchronously send data on the datagram socket.
494 * It is an initiating function for an @ref asynchronous_operation, and always
495 * returns immediately.
497 * @param buffers One or more data buffers to be sent on the socket. Although
498 * the buffers object may be copied as necessary, ownership of the underlying
499 * memory blocks is retained by the caller, which must guarantee that they
500 * remain valid until the completion handler is called.
502 * @param flags Flags specifying how the send call is to be made.
504 * @param token The @ref completion_token that will be used to produce a
505 * completion handler, which will be called when the send completes. Potential
506 * completion tokens include @ref use_future, @ref use_awaitable, @ref
507 * yield_context, or a function object with the correct completion signature.
508 * The function signature of the completion handler must be:
509 * @code void handler(
510 * const boost::system::error_code& error, // Result of operation.
511 * std::size_t bytes_transferred // Number of bytes sent.
513 * Regardless of whether the asynchronous operation completes immediately or
514 * not, the completion handler will not be invoked from within this function.
515 * On immediate completion, invocation of the handler will be performed in a
516 * manner equivalent to using boost::asio::post().
518 * @par Completion Signature
519 * @code void(boost::system::error_code, std::size_t) @endcode
521 * @note The async_send operation can only be used with a connected socket.
522 * Use the async_send_to function to send data on an unconnected datagram
525 * @par Per-Operation Cancellation
526 * On POSIX or Windows operating systems, this asynchronous operation supports
527 * cancellation for the following boost::asio::cancellation_type values:
529 * @li @c cancellation_type::terminal
531 * @li @c cancellation_type::partial
533 * @li @c cancellation_type::total
535 template <typename ConstBufferSequence,
536 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
537 std::size_t)) WriteToken
538 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
539 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
540 void (boost::system::error_code, std::size_t))
541 async_send(const ConstBufferSequence& buffers,
542 socket_base::message_flags flags,
543 BOOST_ASIO_MOVE_ARG(WriteToken) token
544 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
546 return async_initiate<WriteToken,
547 void (boost::system::error_code, std::size_t)>(
548 initiate_async_send(this), token, buffers, flags);
551 /// Send a datagram to the specified endpoint.
553 * This function is used to send a datagram to the specified remote endpoint.
554 * The function call will block until the data has been sent successfully or
557 * @param buffers One or more data buffers to be sent to the remote endpoint.
559 * @param destination The remote endpoint to which the data will be sent.
561 * @returns The number of bytes sent.
563 * @throws boost::system::system_error Thrown on failure.
566 * To send a single data buffer use the @ref buffer function as follows:
568 * boost::asio::ip::udp::endpoint destination(
569 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
570 * socket.send_to(boost::asio::buffer(data, size), destination);
572 * See the @ref buffer documentation for information on sending multiple
573 * buffers in one go, and how to use it with arrays, boost::array or
576 template <typename ConstBufferSequence>
577 std::size_t send_to(const ConstBufferSequence& buffers,
578 const endpoint_type& destination)
580 boost::system::error_code ec;
581 std::size_t s = this->impl_.get_service().send_to(
582 this->impl_.get_implementation(), buffers, destination, 0, ec);
583 boost::asio::detail::throw_error(ec, "send_to");
587 /// Send a datagram to the specified endpoint.
589 * This function is used to send a datagram to the specified remote endpoint.
590 * The function call will block until the data has been sent successfully or
593 * @param buffers One or more data buffers to be sent to the remote endpoint.
595 * @param destination The remote endpoint to which the data will be sent.
597 * @param flags Flags specifying how the send call is to be made.
599 * @returns The number of bytes sent.
601 * @throws boost::system::system_error Thrown on failure.
603 template <typename ConstBufferSequence>
604 std::size_t send_to(const ConstBufferSequence& buffers,
605 const endpoint_type& destination, socket_base::message_flags flags)
607 boost::system::error_code ec;
608 std::size_t s = this->impl_.get_service().send_to(
609 this->impl_.get_implementation(), buffers, destination, flags, ec);
610 boost::asio::detail::throw_error(ec, "send_to");
614 /// Send a datagram to the specified endpoint.
616 * This function is used to send a datagram to the specified remote endpoint.
617 * The function call will block until the data has been sent successfully or
620 * @param buffers One or more data buffers to be sent to the remote endpoint.
622 * @param destination The remote endpoint to which the data will be sent.
624 * @param flags Flags specifying how the send call is to be made.
626 * @param ec Set to indicate what error occurred, if any.
628 * @returns The number of bytes sent.
630 template <typename ConstBufferSequence>
631 std::size_t send_to(const ConstBufferSequence& buffers,
632 const endpoint_type& destination, socket_base::message_flags flags,
633 boost::system::error_code& ec)
635 return this->impl_.get_service().send_to(this->impl_.get_implementation(),
636 buffers, destination, flags, ec);
639 /// Start an asynchronous send.
641 * This function is used to asynchronously send a datagram to the specified
642 * remote endpoint. It is an initiating function for an @ref
643 * asynchronous_operation, and always returns immediately.
645 * @param buffers One or more data buffers to be sent to the remote endpoint.
646 * Although the buffers object may be copied as necessary, ownership of the
647 * underlying memory blocks is retained by the caller, which must guarantee
648 * that they remain valid until the completion handler is called.
650 * @param destination The remote endpoint to which the data will be sent.
651 * Copies will be made of the endpoint as required.
653 * @param token The @ref completion_token that will be used to produce a
654 * completion handler, which will be called when the send completes. Potential
655 * completion tokens include @ref use_future, @ref use_awaitable, @ref
656 * yield_context, or a function object with the correct completion signature.
657 * The function signature of the completion handler must be:
658 * @code void handler(
659 * const boost::system::error_code& error, // Result of operation.
660 * std::size_t bytes_transferred // Number of bytes sent.
662 * Regardless of whether the asynchronous operation completes immediately or
663 * not, the completion handler will not be invoked from within this function.
664 * On immediate completion, invocation of the handler will be performed in a
665 * manner equivalent to using boost::asio::post().
667 * @par Completion Signature
668 * @code void(boost::system::error_code, std::size_t) @endcode
671 * To send a single data buffer use the @ref buffer function as follows:
673 * boost::asio::ip::udp::endpoint destination(
674 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
675 * socket.async_send_to(
676 * boost::asio::buffer(data, size), destination, handler);
678 * See the @ref buffer documentation for information on sending multiple
679 * buffers in one go, and how to use it with arrays, boost::array or
682 * @par Per-Operation Cancellation
683 * On POSIX or Windows operating systems, this asynchronous operation supports
684 * cancellation for the following boost::asio::cancellation_type values:
686 * @li @c cancellation_type::terminal
688 * @li @c cancellation_type::partial
690 * @li @c cancellation_type::total
692 template <typename ConstBufferSequence,
693 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
694 std::size_t)) WriteToken
695 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
696 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
697 void (boost::system::error_code, std::size_t))
698 async_send_to(const ConstBufferSequence& buffers,
699 const endpoint_type& destination,
700 BOOST_ASIO_MOVE_ARG(WriteToken) token
701 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
703 return async_initiate<WriteToken,
704 void (boost::system::error_code, std::size_t)>(
705 initiate_async_send_to(this), token, buffers,
706 destination, socket_base::message_flags(0));
709 /// Start an asynchronous send.
711 * This function is used to asynchronously send a datagram to the specified
712 * remote endpoint. It is an initiating function for an @ref
713 * asynchronous_operation, and always returns immediately.
715 * @param buffers One or more data buffers to be sent to the remote endpoint.
716 * Although the buffers object may be copied as necessary, ownership of the
717 * underlying memory blocks is retained by the caller, which must guarantee
718 * that they remain valid until the completion handler is called.
720 * @param flags Flags specifying how the send call is to be made.
722 * @param destination The remote endpoint to which the data will be sent.
723 * Copies will be made of the endpoint as required.
725 * @param token The @ref completion_token that will be used to produce a
726 * completion handler, which will be called when the send completes. Potential
727 * completion tokens include @ref use_future, @ref use_awaitable, @ref
728 * yield_context, or a function object with the correct completion signature.
729 * The function signature of the completion handler must be:
730 * @code void handler(
731 * const boost::system::error_code& error, // Result of operation.
732 * std::size_t bytes_transferred // Number of bytes sent.
734 * Regardless of whether the asynchronous operation completes immediately or
735 * not, the completion handler will not be invoked from within this function.
736 * On immediate completion, invocation of the handler will be performed in a
737 * manner equivalent to using boost::asio::post().
739 * @par Completion Signature
740 * @code void(boost::system::error_code, std::size_t) @endcode
742 * @par Per-Operation Cancellation
743 * On POSIX or Windows operating systems, this asynchronous operation supports
744 * cancellation for the following boost::asio::cancellation_type values:
746 * @li @c cancellation_type::terminal
748 * @li @c cancellation_type::partial
750 * @li @c cancellation_type::total
752 template <typename ConstBufferSequence,
753 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
754 std::size_t)) WriteToken
755 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
756 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
757 void (boost::system::error_code, std::size_t))
758 async_send_to(const ConstBufferSequence& buffers,
759 const endpoint_type& destination, socket_base::message_flags flags,
760 BOOST_ASIO_MOVE_ARG(WriteToken) token
761 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
763 return async_initiate<WriteToken,
764 void (boost::system::error_code, std::size_t)>(
765 initiate_async_send_to(this), token, buffers, destination, flags);
768 /// Receive some data on a connected socket.
770 * This function is used to receive data on the datagram socket. The function
771 * call will block until data has been received successfully or an error
774 * @param buffers One or more buffers into which the data will be received.
776 * @returns The number of bytes received.
778 * @throws boost::system::system_error Thrown on failure.
780 * @note The receive operation can only be used with a connected socket. Use
781 * the receive_from function to receive data on an unconnected datagram
785 * To receive into a single data buffer use the @ref buffer function as
787 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
788 * See the @ref buffer documentation for information on receiving into
789 * multiple buffers in one go, and how to use it with arrays, boost::array or
792 template <typename MutableBufferSequence>
793 std::size_t receive(const MutableBufferSequence& buffers)
795 boost::system::error_code ec;
796 std::size_t s = this->impl_.get_service().receive(
797 this->impl_.get_implementation(), buffers, 0, ec);
798 boost::asio::detail::throw_error(ec, "receive");
802 /// Receive some data on a connected socket.
804 * This function is used to receive data on the datagram socket. The function
805 * call will block until data has been received successfully or an error
808 * @param buffers One or more buffers into which the data will be received.
810 * @param flags Flags specifying how the receive call is to be made.
812 * @returns The number of bytes received.
814 * @throws boost::system::system_error Thrown on failure.
816 * @note The receive operation can only be used with a connected socket. Use
817 * the receive_from function to receive data on an unconnected datagram
820 template <typename MutableBufferSequence>
821 std::size_t receive(const MutableBufferSequence& buffers,
822 socket_base::message_flags flags)
824 boost::system::error_code ec;
825 std::size_t s = this->impl_.get_service().receive(
826 this->impl_.get_implementation(), buffers, flags, ec);
827 boost::asio::detail::throw_error(ec, "receive");
831 /// Receive some data on a connected socket.
833 * This function is used to receive data on the datagram socket. The function
834 * call will block until data has been received successfully or an error
837 * @param buffers One or more buffers into which the data will be received.
839 * @param flags Flags specifying how the receive call is to be made.
841 * @param ec Set to indicate what error occurred, if any.
843 * @returns The number of bytes received.
845 * @note The receive operation can only be used with a connected socket. Use
846 * the receive_from function to receive data on an unconnected datagram
849 template <typename MutableBufferSequence>
850 std::size_t receive(const MutableBufferSequence& buffers,
851 socket_base::message_flags flags, boost::system::error_code& ec)
853 return this->impl_.get_service().receive(
854 this->impl_.get_implementation(), buffers, flags, ec);
857 /// Start an asynchronous receive on a connected socket.
859 * This function is used to asynchronously receive data from the datagram
860 * socket. It is an initiating function for an @ref asynchronous_operation,
861 * and always returns immediately.
863 * @param buffers One or more buffers into which the data will be received.
864 * Although the buffers object may be copied as necessary, ownership of the
865 * underlying memory blocks is retained by the caller, which must guarantee
866 * that they remain valid until the completion handler is called.
868 * @param token The @ref completion_token that will be used to produce a
869 * completion handler, which will be called when the receive completes.
870 * Potential completion tokens include @ref use_future, @ref use_awaitable,
871 * @ref yield_context, or a function object with the correct completion
872 * signature. The function signature of the completion handler must be:
873 * @code void handler(
874 * const boost::system::error_code& error, // Result of operation.
875 * std::size_t bytes_transferred // Number of bytes received.
877 * Regardless of whether the asynchronous operation completes immediately or
878 * not, the completion handler will not be invoked from within this function.
879 * On immediate completion, invocation of the handler will be performed in a
880 * manner equivalent to using boost::asio::post().
882 * @par Completion Signature
883 * @code void(boost::system::error_code, std::size_t) @endcode
885 * @note The async_receive operation can only be used with a connected socket.
886 * Use the async_receive_from function to receive data on an unconnected
890 * To receive into a single data buffer use the @ref buffer function as
893 * socket.async_receive(boost::asio::buffer(data, size), handler);
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 * @par Per-Operation Cancellation
900 * On POSIX or Windows operating systems, this asynchronous operation supports
901 * cancellation for the following boost::asio::cancellation_type values:
903 * @li @c cancellation_type::terminal
905 * @li @c cancellation_type::partial
907 * @li @c cancellation_type::total
909 template <typename MutableBufferSequence,
910 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
911 std::size_t)) ReadToken
912 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
913 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
914 void (boost::system::error_code, std::size_t))
915 async_receive(const MutableBufferSequence& buffers,
916 BOOST_ASIO_MOVE_ARG(ReadToken) token
917 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
919 return async_initiate<ReadToken,
920 void (boost::system::error_code, std::size_t)>(
921 initiate_async_receive(this), token,
922 buffers, socket_base::message_flags(0));
925 /// Start an asynchronous receive on a connected socket.
927 * This function is used to asynchronously receive data from the datagram
928 * socket. It is an initiating function for an @ref asynchronous_operation,
929 * and always returns immediately.
931 * @param buffers One or more buffers into which the data will be received.
932 * Although the buffers object may be copied as necessary, ownership of the
933 * underlying memory blocks is retained by the caller, which must guarantee
934 * that they remain valid until the completion handler is called.
936 * @param flags Flags specifying how the receive call is to be made.
938 * @param token The @ref completion_token that will be used to produce a
939 * completion handler, which will be called when the receive completes.
940 * Potential completion tokens include @ref use_future, @ref use_awaitable,
941 * @ref yield_context, or a function object with the correct completion
942 * signature. The function signature of the completion handler must be:
943 * @code void handler(
944 * const boost::system::error_code& error, // Result of operation.
945 * std::size_t bytes_transferred // Number of bytes received.
947 * Regardless of whether the asynchronous operation completes immediately or
948 * not, the completion handler will not be invoked from within this function.
949 * On immediate completion, invocation of the handler will be performed in a
950 * manner equivalent to using boost::asio::post().
952 * @par Completion Signature
953 * @code void(boost::system::error_code, std::size_t) @endcode
955 * @note The async_receive operation can only be used with a connected socket.
956 * Use the async_receive_from function to receive data on an unconnected
959 * @par Per-Operation Cancellation
960 * On POSIX or Windows operating systems, this asynchronous operation supports
961 * cancellation for the following boost::asio::cancellation_type values:
963 * @li @c cancellation_type::terminal
965 * @li @c cancellation_type::partial
967 * @li @c cancellation_type::total
969 template <typename MutableBufferSequence,
970 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
971 std::size_t)) ReadToken
972 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
973 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
974 void (boost::system::error_code, std::size_t))
975 async_receive(const MutableBufferSequence& buffers,
976 socket_base::message_flags flags,
977 BOOST_ASIO_MOVE_ARG(ReadToken) token
978 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
980 return async_initiate<ReadToken,
981 void (boost::system::error_code, std::size_t)>(
982 initiate_async_receive(this), token, buffers, flags);
985 /// Receive a datagram with the endpoint of the sender.
987 * This function is used to receive a datagram. The function call will block
988 * until data has been received successfully or an error occurs.
990 * @param buffers One or more buffers into which the data will be received.
992 * @param sender_endpoint An endpoint object that receives the endpoint of
993 * the remote sender of the datagram.
995 * @returns The number of bytes received.
997 * @throws boost::system::system_error Thrown on failure.
1000 * To receive into a single data buffer use the @ref buffer function as
1003 * boost::asio::ip::udp::endpoint sender_endpoint;
1004 * socket.receive_from(
1005 * boost::asio::buffer(data, size), sender_endpoint);
1007 * See the @ref buffer documentation for information on receiving into
1008 * multiple buffers in one go, and how to use it with arrays, boost::array or
1011 template <typename MutableBufferSequence>
1012 std::size_t receive_from(const MutableBufferSequence& buffers,
1013 endpoint_type& sender_endpoint)
1015 boost::system::error_code ec;
1016 std::size_t s = this->impl_.get_service().receive_from(
1017 this->impl_.get_implementation(), buffers, sender_endpoint, 0, ec);
1018 boost::asio::detail::throw_error(ec, "receive_from");
1022 /// Receive a datagram with the endpoint of the sender.
1024 * This function is used to receive a datagram. The function call will block
1025 * until data has been received successfully or an error occurs.
1027 * @param buffers One or more buffers into which the data will be received.
1029 * @param sender_endpoint An endpoint object that receives the endpoint of
1030 * the remote sender of the datagram.
1032 * @param flags Flags specifying how the receive call is to be made.
1034 * @returns The number of bytes received.
1036 * @throws boost::system::system_error Thrown on failure.
1038 template <typename MutableBufferSequence>
1039 std::size_t receive_from(const MutableBufferSequence& buffers,
1040 endpoint_type& sender_endpoint, socket_base::message_flags flags)
1042 boost::system::error_code ec;
1043 std::size_t s = this->impl_.get_service().receive_from(
1044 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
1045 boost::asio::detail::throw_error(ec, "receive_from");
1049 /// Receive a datagram with the endpoint of the sender.
1051 * This function is used to receive a datagram. The function call will block
1052 * until data has been received successfully or an error occurs.
1054 * @param buffers One or more buffers into which the data will be received.
1056 * @param sender_endpoint An endpoint object that receives the endpoint of
1057 * the remote sender of the datagram.
1059 * @param flags Flags specifying how the receive call is to be made.
1061 * @param ec Set to indicate what error occurred, if any.
1063 * @returns The number of bytes received.
1065 template <typename MutableBufferSequence>
1066 std::size_t receive_from(const MutableBufferSequence& buffers,
1067 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1068 boost::system::error_code& ec)
1070 return this->impl_.get_service().receive_from(
1071 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
1074 /// Start an asynchronous receive.
1076 * This function is used to asynchronously receive a datagram. It is an
1077 * initiating function for an @ref asynchronous_operation, and always returns
1080 * @param buffers One or more buffers into which the data will be received.
1081 * Although the buffers object may be copied as necessary, ownership of the
1082 * underlying memory blocks is retained by the caller, which must guarantee
1083 * that they remain valid until the completion handler is called.
1085 * @param sender_endpoint An endpoint object that receives the endpoint of
1086 * the remote sender of the datagram. Ownership of the sender_endpoint object
1087 * is retained by the caller, which must guarantee that it is valid until the
1088 * completion handler is called.
1090 * @param token The @ref completion_token that will be used to produce a
1091 * completion handler, which will be called when the receive completes.
1092 * Potential completion tokens include @ref use_future, @ref use_awaitable,
1093 * @ref yield_context, or a function object with the correct completion
1094 * signature. The function signature of the completion handler must be:
1095 * @code void handler(
1096 * const boost::system::error_code& error, // Result of operation.
1097 * std::size_t bytes_transferred // Number of bytes received.
1099 * Regardless of whether the asynchronous operation completes immediately or
1100 * not, the completion handler will not be invoked from within this function.
1101 * On immediate completion, invocation of the handler will be performed in a
1102 * manner equivalent to using boost::asio::post().
1104 * @par Completion Signature
1105 * @code void(boost::system::error_code, std::size_t) @endcode
1108 * To receive into a single data buffer use the @ref buffer function as
1110 * @code socket.async_receive_from(
1111 * boost::asio::buffer(data, size), sender_endpoint, handler); @endcode
1112 * See the @ref buffer documentation for information on receiving into
1113 * multiple buffers in one go, and how to use it with arrays, boost::array or
1116 * @par Per-Operation Cancellation
1117 * On POSIX or Windows operating systems, this asynchronous operation supports
1118 * cancellation for the following boost::asio::cancellation_type values:
1120 * @li @c cancellation_type::terminal
1122 * @li @c cancellation_type::partial
1124 * @li @c cancellation_type::total
1126 template <typename MutableBufferSequence,
1127 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1128 std::size_t)) ReadToken
1129 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1130 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
1131 void (boost::system::error_code, std::size_t))
1132 async_receive_from(const MutableBufferSequence& buffers,
1133 endpoint_type& sender_endpoint,
1134 BOOST_ASIO_MOVE_ARG(ReadToken) token
1135 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1137 return async_initiate<ReadToken,
1138 void (boost::system::error_code, std::size_t)>(
1139 initiate_async_receive_from(this), token, buffers,
1140 &sender_endpoint, socket_base::message_flags(0));
1143 /// Start an asynchronous receive.
1145 * This function is used to asynchronously receive a datagram. It is an
1146 * initiating function for an @ref asynchronous_operation, and always returns
1149 * @param buffers One or more buffers into which the data will be received.
1150 * Although the buffers object may be copied as necessary, ownership of the
1151 * underlying memory blocks is retained by the caller, which must guarantee
1152 * that they remain valid until the completion handler is called.
1154 * @param sender_endpoint An endpoint object that receives the endpoint of
1155 * the remote sender of the datagram. Ownership of the sender_endpoint object
1156 * is retained by the caller, which must guarantee that it is valid until the
1157 * completion handler is called.
1159 * @param flags Flags specifying how the receive call is to be made.
1161 * @param token The @ref completion_token that will be used to produce a
1162 * completion handler, which will be called when the receive completes.
1163 * Potential completion tokens include @ref use_future, @ref use_awaitable,
1164 * @ref yield_context, or a function object with the correct completion
1165 * signature. The function signature of the completion handler must be:
1166 * @code void handler(
1167 * const boost::system::error_code& error, // Result of operation.
1168 * std::size_t bytes_transferred // Number of bytes received.
1170 * Regardless of whether the asynchronous operation completes immediately or
1171 * not, the completion handler will not be invoked from within this function.
1172 * On immediate completion, invocation of the handler will be performed in a
1173 * manner equivalent to using boost::asio::post().
1175 * @par Completion Signature
1176 * @code void(boost::system::error_code, std::size_t) @endcode
1178 * @par Per-Operation Cancellation
1179 * On POSIX or Windows operating systems, this asynchronous operation supports
1180 * cancellation for the following boost::asio::cancellation_type values:
1182 * @li @c cancellation_type::terminal
1184 * @li @c cancellation_type::partial
1186 * @li @c cancellation_type::total
1188 template <typename MutableBufferSequence,
1189 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1190 std::size_t)) ReadToken
1191 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1192 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
1193 void (boost::system::error_code, std::size_t))
1194 async_receive_from(const MutableBufferSequence& buffers,
1195 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1196 BOOST_ASIO_MOVE_ARG(ReadToken) token
1197 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1199 return async_initiate<ReadToken,
1200 void (boost::system::error_code, std::size_t)>(
1201 initiate_async_receive_from(this), token,
1202 buffers, &sender_endpoint, flags);
1206 // Disallow copying and assignment.
1207 basic_datagram_socket(const basic_datagram_socket&) BOOST_ASIO_DELETED;
1208 basic_datagram_socket& operator=(
1209 const basic_datagram_socket&) BOOST_ASIO_DELETED;
1211 class initiate_async_send
1214 typedef Executor executor_type;
1216 explicit initiate_async_send(basic_datagram_socket* self)
1221 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1223 return self_->get_executor();
1226 template <typename WriteHandler, typename ConstBufferSequence>
1227 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1228 const ConstBufferSequence& buffers,
1229 socket_base::message_flags flags) const
1231 // If you get an error on the following line it means that your handler
1232 // does not meet the documented type requirements for a WriteHandler.
1233 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1235 detail::non_const_lvalue<WriteHandler> handler2(handler);
1236 self_->impl_.get_service().async_send(
1237 self_->impl_.get_implementation(), buffers, flags,
1238 handler2.value, self_->impl_.get_executor());
1242 basic_datagram_socket* self_;
1245 class initiate_async_send_to
1248 typedef Executor executor_type;
1250 explicit initiate_async_send_to(basic_datagram_socket* self)
1255 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1257 return self_->get_executor();
1260 template <typename WriteHandler, typename ConstBufferSequence>
1261 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1262 const ConstBufferSequence& buffers, const endpoint_type& destination,
1263 socket_base::message_flags flags) const
1265 // If you get an error on the following line it means that your handler
1266 // does not meet the documented type requirements for a WriteHandler.
1267 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1269 detail::non_const_lvalue<WriteHandler> handler2(handler);
1270 self_->impl_.get_service().async_send_to(
1271 self_->impl_.get_implementation(), buffers, destination,
1272 flags, handler2.value, self_->impl_.get_executor());
1276 basic_datagram_socket* self_;
1279 class initiate_async_receive
1282 typedef Executor executor_type;
1284 explicit initiate_async_receive(basic_datagram_socket* self)
1289 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1291 return self_->get_executor();
1294 template <typename ReadHandler, typename MutableBufferSequence>
1295 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1296 const MutableBufferSequence& buffers,
1297 socket_base::message_flags flags) const
1299 // If you get an error on the following line it means that your handler
1300 // does not meet the documented type requirements for a ReadHandler.
1301 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1303 detail::non_const_lvalue<ReadHandler> handler2(handler);
1304 self_->impl_.get_service().async_receive(
1305 self_->impl_.get_implementation(), buffers, flags,
1306 handler2.value, self_->impl_.get_executor());
1310 basic_datagram_socket* self_;
1313 class initiate_async_receive_from
1316 typedef Executor executor_type;
1318 explicit initiate_async_receive_from(basic_datagram_socket* self)
1323 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1325 return self_->get_executor();
1328 template <typename ReadHandler, typename MutableBufferSequence>
1329 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1330 const MutableBufferSequence& buffers, endpoint_type* sender_endpoint,
1331 socket_base::message_flags flags) const
1333 // If you get an error on the following line it means that your handler
1334 // does not meet the documented type requirements for a ReadHandler.
1335 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1337 detail::non_const_lvalue<ReadHandler> handler2(handler);
1338 self_->impl_.get_service().async_receive_from(
1339 self_->impl_.get_implementation(), buffers, *sender_endpoint,
1340 flags, handler2.value, self_->impl_.get_executor());
1344 basic_datagram_socket* self_;
1349 } // namespace boost
1351 #include <boost/asio/detail/pop_options.hpp>
1353 #endif // BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP