2 // basic_raw_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_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 = any_io_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 * 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_raw_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_raw_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_raw_socket without opening it.
89 * This constructor creates a raw 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_raw_socket(const executor_type& ex)
96 : basic_socket<Protocol, Executor>(ex)
100 /// Construct a basic_raw_socket without opening it.
102 * This constructor creates a raw 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_raw_socket(ExecutionContext& context,
112 is_convertible<ExecutionContext&, execution_context&>::value
114 : basic_socket<Protocol, Executor>(context)
118 /// Construct and open a basic_raw_socket.
120 * This constructor creates and opens a raw 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_raw_socket(const executor_type& ex, const protocol_type& protocol)
130 : basic_socket<Protocol, Executor>(ex, protocol)
134 /// Construct and open a basic_raw_socket.
136 * This constructor creates and opens a raw 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_raw_socket(ExecutionContext& context, const protocol_type& protocol,
149 is_convertible<ExecutionContext&, execution_context&>::value,
151 >::type = defaulted_constraint())
152 : basic_socket<Protocol, Executor>(context, protocol)
156 /// Construct a basic_raw_socket, opening it and binding it to the given
159 * This constructor creates a raw socket and automatically opens it bound
160 * to the specified endpoint on the local machine. The protocol used is the
161 * protocol associated with the given endpoint.
163 * @param ex The I/O executor that the socket will use, by default, to
164 * dispatch handlers for any asynchronous operations performed on the socket.
166 * @param endpoint An endpoint on the local machine to which the raw
167 * socket will be bound.
169 * @throws boost::system::system_error Thrown on failure.
171 basic_raw_socket(const executor_type& ex, const endpoint_type& endpoint)
172 : basic_socket<Protocol, Executor>(ex, endpoint)
176 /// Construct a basic_raw_socket, opening it and binding it to the given
179 * This constructor creates a raw socket and automatically opens it bound
180 * to the specified endpoint on the local machine. The protocol used is the
181 * protocol associated with the given endpoint.
183 * @param context An execution context which provides the I/O executor that
184 * the socket will use, by default, to dispatch handlers for any asynchronous
185 * operations performed on the socket.
187 * @param endpoint An endpoint on the local machine to which the raw
188 * socket will be bound.
190 * @throws boost::system::system_error Thrown on failure.
192 template <typename ExecutionContext>
193 basic_raw_socket(ExecutionContext& context, const endpoint_type& endpoint,
195 is_convertible<ExecutionContext&, execution_context&>::value
197 : basic_socket<Protocol, Executor>(context, endpoint)
201 /// Construct a basic_raw_socket on an existing native socket.
203 * This constructor creates a raw socket object to hold an existing
206 * @param ex The I/O executor that the socket will use, by default, to
207 * dispatch handlers for any asynchronous operations performed on the socket.
209 * @param protocol An object specifying protocol parameters to be used.
211 * @param native_socket The new underlying socket implementation.
213 * @throws boost::system::system_error Thrown on failure.
215 basic_raw_socket(const executor_type& ex,
216 const protocol_type& protocol, const native_handle_type& native_socket)
217 : basic_socket<Protocol, Executor>(ex, protocol, native_socket)
221 /// Construct a basic_raw_socket on an existing native socket.
223 * This constructor creates a raw socket object to hold an existing
226 * @param context An execution context which provides the I/O executor that
227 * the socket will use, by default, to dispatch handlers for any asynchronous
228 * operations performed on the socket.
230 * @param protocol An object specifying protocol parameters to be used.
232 * @param native_socket The new underlying socket implementation.
234 * @throws boost::system::system_error Thrown on failure.
236 template <typename ExecutionContext>
237 basic_raw_socket(ExecutionContext& context,
238 const protocol_type& protocol, const native_handle_type& native_socket,
240 is_convertible<ExecutionContext&, execution_context&>::value
242 : basic_socket<Protocol, Executor>(context, protocol, native_socket)
246 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
247 /// Move-construct a basic_raw_socket from another.
249 * This constructor moves a raw socket from one object to another.
251 * @param other The other basic_raw_socket object from which the move
254 * @note Following the move, the moved-from object is in the same state as if
255 * constructed using the @c basic_raw_socket(const executor_type&)
258 basic_raw_socket(basic_raw_socket&& other) BOOST_ASIO_NOEXCEPT
259 : basic_socket<Protocol, Executor>(std::move(other))
263 /// Move-assign a basic_raw_socket from another.
265 * This assignment operator moves a raw socket from one object to another.
267 * @param other The other basic_raw_socket object from which the move
270 * @note Following the move, the moved-from object is in the same state as if
271 * constructed using the @c basic_raw_socket(const executor_type&)
274 basic_raw_socket& operator=(basic_raw_socket&& other)
276 basic_socket<Protocol, Executor>::operator=(std::move(other));
280 /// Move-construct a basic_raw_socket from a socket of another protocol
283 * This constructor moves a raw socket from one object to another.
285 * @param other The other basic_raw_socket object from which the move
288 * @note Following the move, the moved-from object is in the same state as if
289 * constructed using the @c basic_raw_socket(const executor_type&)
292 template <typename Protocol1, typename Executor1>
293 basic_raw_socket(basic_raw_socket<Protocol1, Executor1>&& other,
295 is_convertible<Protocol1, Protocol>::value
296 && is_convertible<Executor1, Executor>::value
298 : basic_socket<Protocol, Executor>(std::move(other))
302 /// Move-assign a basic_raw_socket from a socket of another protocol type.
304 * This assignment operator moves a raw socket from one object to another.
306 * @param other The other basic_raw_socket object from which the move
309 * @note Following the move, the moved-from object is in the same state as if
310 * constructed using the @c basic_raw_socket(const executor_type&)
313 template <typename Protocol1, typename Executor1>
315 is_convertible<Protocol1, Protocol>::value
316 && is_convertible<Executor1, Executor>::value,
318 >::type operator=(basic_raw_socket<Protocol1, Executor1>&& other)
320 basic_socket<Protocol, Executor>::operator=(std::move(other));
323 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
325 /// Destroys the socket.
327 * This function destroys the socket, cancelling any outstanding asynchronous
328 * operations associated with the socket as if by calling @c cancel.
334 /// Send some data on a connected socket.
336 * This function is used to send data on the raw socket. The function call
337 * will block until the data has been sent successfully or an error occurs.
339 * @param buffers One ore more data buffers to be sent on the socket.
341 * @returns The number of bytes sent.
343 * @throws boost::system::system_error Thrown on failure.
345 * @note The send operation can only be used with a connected socket. Use
346 * the send_to function to send data on an unconnected raw socket.
349 * To send a single data buffer use the @ref buffer function as follows:
350 * @code socket.send(boost::asio::buffer(data, size)); @endcode
351 * See the @ref buffer documentation for information on sending multiple
352 * buffers in one go, and how to use it with arrays, boost::array or
355 template <typename ConstBufferSequence>
356 std::size_t send(const ConstBufferSequence& buffers)
358 boost::system::error_code ec;
359 std::size_t s = this->impl_.get_service().send(
360 this->impl_.get_implementation(), buffers, 0, ec);
361 boost::asio::detail::throw_error(ec, "send");
365 /// Send some data on a connected socket.
367 * This function is used to send data on the raw socket. The function call
368 * will block until the data has been sent successfully or an error occurs.
370 * @param buffers One ore more data buffers to be sent on the socket.
372 * @param flags Flags specifying how the send call is to be made.
374 * @returns The number of bytes sent.
376 * @throws boost::system::system_error Thrown on failure.
378 * @note The send operation can only be used with a connected socket. Use
379 * the send_to function to send data on an unconnected raw socket.
381 template <typename ConstBufferSequence>
382 std::size_t send(const ConstBufferSequence& buffers,
383 socket_base::message_flags flags)
385 boost::system::error_code ec;
386 std::size_t s = this->impl_.get_service().send(
387 this->impl_.get_implementation(), buffers, flags, ec);
388 boost::asio::detail::throw_error(ec, "send");
392 /// Send some data on a connected socket.
394 * This function is used to send data on the raw socket. The function call
395 * will block until the data has been sent successfully or an error occurs.
397 * @param buffers One or more data buffers to be sent on the socket.
399 * @param flags Flags specifying how the send call is to be made.
401 * @param ec Set to indicate what error occurred, if any.
403 * @returns The number of bytes sent.
405 * @note The send operation can only be used with a connected socket. Use
406 * the send_to function to send data on an unconnected raw socket.
408 template <typename ConstBufferSequence>
409 std::size_t send(const ConstBufferSequence& buffers,
410 socket_base::message_flags flags, boost::system::error_code& ec)
412 return this->impl_.get_service().send(
413 this->impl_.get_implementation(), buffers, flags, ec);
416 /// Start an asynchronous send on a connected socket.
418 * This function is used to asynchronously send data on the raw socket. It is
419 * an initiating function for an @ref asynchronous_operation, and always
420 * returns immediately.
422 * @param buffers One or more data buffers to be sent on the socket. Although
423 * the buffers object may be copied as necessary, ownership of the underlying
424 * memory blocks is retained by the caller, which must guarantee that they
425 * remain valid until the completion handler is called.
427 * @param token The @ref completion_token that will be used to produce a
428 * completion handler, which will be called when the send completes.
429 * Potential completion tokens include @ref use_future, @ref use_awaitable,
430 * @ref yield_context, or a function object with the correct completion
431 * signature. The function signature of the completion handler must be:
432 * @code void handler(
433 * const boost::system::error_code& error, // Result of operation.
434 * std::size_t bytes_transferred // Number of bytes sent.
436 * Regardless of whether the asynchronous operation completes immediately or
437 * not, the completion handler will not be invoked from within this function.
438 * On immediate completion, invocation of the handler will be performed in a
439 * manner equivalent to using boost::asio::post().
441 * @par Completion Signature
442 * @code void(boost::system::error_code, std::size_t) @endcode
444 * @note The async_send operation can only be used with a connected socket.
445 * Use the async_send_to function to send data on an unconnected raw
449 * To send a single data buffer use the @ref buffer function as follows:
451 * socket.async_send(boost::asio::buffer(data, size), handler);
453 * See the @ref buffer documentation for information on sending multiple
454 * buffers in one go, and how to use it with arrays, boost::array or
457 * @par Per-Operation Cancellation
458 * On POSIX or Windows operating systems, this asynchronous operation supports
459 * cancellation for the following boost::asio::cancellation_type values:
461 * @li @c cancellation_type::terminal
463 * @li @c cancellation_type::partial
465 * @li @c cancellation_type::total
467 template <typename ConstBufferSequence,
468 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
469 std::size_t)) WriteToken
470 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
471 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
472 void (boost::system::error_code, std::size_t))
473 async_send(const ConstBufferSequence& buffers,
474 BOOST_ASIO_MOVE_ARG(WriteToken) token
475 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
477 return async_initiate<WriteToken,
478 void (boost::system::error_code, std::size_t)>(
479 initiate_async_send(this), token,
480 buffers, socket_base::message_flags(0));
483 /// Start an asynchronous send on a connected socket.
485 * This function is used to asynchronously send data on the raw socket. It is
486 * an initiating function for an @ref asynchronous_operation, and always
487 * returns immediately.
489 * @param buffers One or more data buffers to be sent on the socket. Although
490 * the buffers object may be copied as necessary, ownership of the underlying
491 * memory blocks is retained by the caller, which must guarantee that they
492 * remain valid until the completion handler is called.
494 * @param flags Flags specifying how the send call is to be made.
496 * @param token The @ref completion_token that will be used to produce a
497 * completion handler, which will be called when the send completes.
498 * Potential completion tokens include @ref use_future, @ref use_awaitable,
499 * @ref yield_context, or a function object with the correct completion
500 * signature. The function signature of the completion handler must be:
501 * @code void handler(
502 * const boost::system::error_code& error, // Result of operation.
503 * std::size_t bytes_transferred // Number of bytes sent.
505 * Regardless of whether the asynchronous operation completes immediately or
506 * not, the completion handler will not be invoked from within this function.
507 * On immediate completion, invocation of the handler will be performed in a
508 * manner equivalent to using boost::asio::post().
510 * @par Completion Signature
511 * @code void(boost::system::error_code, std::size_t) @endcode
513 * @note The async_send operation can only be used with a connected socket.
514 * Use the async_send_to function to send data on an unconnected raw
517 * @par Per-Operation Cancellation
518 * On POSIX or Windows operating systems, this asynchronous operation supports
519 * cancellation for the following boost::asio::cancellation_type values:
521 * @li @c cancellation_type::terminal
523 * @li @c cancellation_type::partial
525 * @li @c cancellation_type::total
527 template <typename ConstBufferSequence,
528 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
529 std::size_t)) WriteToken
530 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
531 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
532 void (boost::system::error_code, std::size_t))
533 async_send(const ConstBufferSequence& buffers,
534 socket_base::message_flags flags,
535 BOOST_ASIO_MOVE_ARG(WriteToken) token
536 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
538 return async_initiate<WriteToken,
539 void (boost::system::error_code, std::size_t)>(
540 initiate_async_send(this), token, buffers, flags);
543 /// Send raw data to the specified endpoint.
545 * This function is used to send raw data to the specified remote endpoint.
546 * The function call will block until the data has been sent successfully or
549 * @param buffers One or more data buffers to be sent to the remote endpoint.
551 * @param destination The remote endpoint to which the data will be sent.
553 * @returns The number of bytes sent.
555 * @throws boost::system::system_error Thrown on failure.
558 * To send a single data buffer use the @ref buffer function as follows:
560 * boost::asio::ip::udp::endpoint destination(
561 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
562 * socket.send_to(boost::asio::buffer(data, size), destination);
564 * See the @ref buffer documentation for information on sending multiple
565 * buffers in one go, and how to use it with arrays, boost::array or
568 template <typename ConstBufferSequence>
569 std::size_t send_to(const ConstBufferSequence& buffers,
570 const endpoint_type& destination)
572 boost::system::error_code ec;
573 std::size_t s = this->impl_.get_service().send_to(
574 this->impl_.get_implementation(), buffers, destination, 0, ec);
575 boost::asio::detail::throw_error(ec, "send_to");
579 /// Send raw data to the specified endpoint.
581 * This function is used to send raw data to the specified remote endpoint.
582 * The function call will block until the data has been sent successfully or
585 * @param buffers One or more data buffers to be sent to the remote endpoint.
587 * @param destination The remote endpoint to which the data will be sent.
589 * @param flags Flags specifying how the send call is to be made.
591 * @returns The number of bytes sent.
593 * @throws boost::system::system_error Thrown on failure.
595 template <typename ConstBufferSequence>
596 std::size_t send_to(const ConstBufferSequence& buffers,
597 const endpoint_type& destination, socket_base::message_flags flags)
599 boost::system::error_code ec;
600 std::size_t s = this->impl_.get_service().send_to(
601 this->impl_.get_implementation(), buffers, destination, flags, ec);
602 boost::asio::detail::throw_error(ec, "send_to");
606 /// Send raw data to the specified endpoint.
608 * This function is used to send raw data to the specified remote endpoint.
609 * The function call will block until the data has been sent successfully or
612 * @param buffers One or more data buffers to be sent to the remote endpoint.
614 * @param destination The remote endpoint to which the data will be sent.
616 * @param flags Flags specifying how the send call is to be made.
618 * @param ec Set to indicate what error occurred, if any.
620 * @returns The number of bytes sent.
622 template <typename ConstBufferSequence>
623 std::size_t send_to(const ConstBufferSequence& buffers,
624 const endpoint_type& destination, socket_base::message_flags flags,
625 boost::system::error_code& ec)
627 return this->impl_.get_service().send_to(this->impl_.get_implementation(),
628 buffers, destination, flags, ec);
631 /// Start an asynchronous send.
633 * This function is used to asynchronously send raw data to the specified
634 * remote endpoint. It is an initiating function for an @ref
635 * asynchronous_operation, and always returns immediately.
637 * @param buffers One or more data buffers to be sent to the remote endpoint.
638 * Although the buffers object may be copied as necessary, ownership of the
639 * underlying memory blocks is retained by the caller, which must guarantee
640 * that they remain valid until the completion handler is called.
642 * @param destination The remote endpoint to which the data will be sent.
643 * Copies will be made of the endpoint as required.
645 * @param token The @ref completion_token that will be used to produce a
646 * completion handler, which will be called when the send completes.
647 * Potential completion tokens include @ref use_future, @ref use_awaitable,
648 * @ref yield_context, or a function object with the correct completion
649 * signature. The function signature of the completion handler must be:
650 * @code void handler(
651 * const boost::system::error_code& error, // Result of operation.
652 * std::size_t bytes_transferred // Number of bytes sent.
654 * Regardless of whether the asynchronous operation completes immediately or
655 * not, the completion handler will not be invoked from within this function.
656 * On immediate completion, invocation of the handler will be performed in a
657 * manner equivalent to using boost::asio::post().
659 * @par Completion Signature
660 * @code void(boost::system::error_code, std::size_t) @endcode
663 * To send a single data buffer use the @ref buffer function as follows:
665 * boost::asio::ip::udp::endpoint destination(
666 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
667 * socket.async_send_to(
668 * boost::asio::buffer(data, size), destination, handler);
670 * See the @ref buffer documentation for information on sending multiple
671 * buffers in one go, and how to use it with arrays, boost::array or
674 * @par Per-Operation Cancellation
675 * On POSIX or Windows operating systems, this asynchronous operation supports
676 * cancellation for the following boost::asio::cancellation_type values:
678 * @li @c cancellation_type::terminal
680 * @li @c cancellation_type::partial
682 * @li @c cancellation_type::total
684 template <typename ConstBufferSequence,
685 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
686 std::size_t)) WriteToken
687 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
688 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
689 void (boost::system::error_code, std::size_t))
690 async_send_to(const ConstBufferSequence& buffers,
691 const endpoint_type& destination,
692 BOOST_ASIO_MOVE_ARG(WriteToken) token
693 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
695 return async_initiate<WriteToken,
696 void (boost::system::error_code, std::size_t)>(
697 initiate_async_send_to(this), token, buffers,
698 destination, socket_base::message_flags(0));
701 /// Start an asynchronous send.
703 * This function is used to asynchronously send raw data to the specified
704 * remote endpoint. It is an initiating function for an @ref
705 * asynchronous_operation, and always returns immediately.
707 * @param buffers One or more data buffers to be sent to the remote endpoint.
708 * Although the buffers object may be copied as necessary, ownership of the
709 * underlying memory blocks is retained by the caller, which must guarantee
710 * that they remain valid until the completion handler is called.
712 * @param flags Flags specifying how the send call is to be made.
714 * @param destination The remote endpoint to which the data will be sent.
715 * Copies will be made of the endpoint as required.
717 * @param token The @ref completion_token that will be used to produce a
718 * completion handler, which will be called when the send completes.
719 * Potential completion tokens include @ref use_future, @ref use_awaitable,
720 * @ref yield_context, or a function object with the correct completion
721 * signature. The function signature of the completion handler must be:
722 * @code void handler(
723 * const boost::system::error_code& error, // Result of operation.
724 * std::size_t bytes_transferred // Number of bytes sent.
726 * Regardless of whether the asynchronous operation completes immediately or
727 * not, the completion handler will not be invoked from within this function.
728 * On immediate completion, invocation of the handler will be performed in a
729 * manner equivalent to using boost::asio::post().
731 * @par Completion Signature
732 * @code void(boost::system::error_code, std::size_t) @endcode
734 * @par Per-Operation Cancellation
735 * On POSIX or Windows operating systems, this asynchronous operation supports
736 * cancellation for the following boost::asio::cancellation_type values:
738 * @li @c cancellation_type::terminal
740 * @li @c cancellation_type::partial
742 * @li @c cancellation_type::total
744 template <typename ConstBufferSequence,
745 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
746 std::size_t)) WriteToken
747 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
748 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
749 void (boost::system::error_code, std::size_t))
750 async_send_to(const ConstBufferSequence& buffers,
751 const endpoint_type& destination, socket_base::message_flags flags,
752 BOOST_ASIO_MOVE_ARG(WriteToken) token
753 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
755 return async_initiate<WriteToken,
756 void (boost::system::error_code, std::size_t)>(
757 initiate_async_send_to(this), token, buffers, destination, flags);
760 /// Receive some data on a connected socket.
762 * This function is used to receive data on the raw socket. The function
763 * call will block until data has been received successfully or an error
766 * @param buffers One or more buffers into which the data will be received.
768 * @returns The number of bytes received.
770 * @throws boost::system::system_error Thrown on failure.
772 * @note The receive operation can only be used with a connected socket. Use
773 * the receive_from function to receive data on an unconnected raw
777 * To receive into a single data buffer use the @ref buffer function as
779 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
780 * See the @ref buffer documentation for information on receiving into
781 * multiple buffers in one go, and how to use it with arrays, boost::array or
784 template <typename MutableBufferSequence>
785 std::size_t receive(const MutableBufferSequence& buffers)
787 boost::system::error_code ec;
788 std::size_t s = this->impl_.get_service().receive(
789 this->impl_.get_implementation(), buffers, 0, ec);
790 boost::asio::detail::throw_error(ec, "receive");
794 /// Receive some data on a connected socket.
796 * This function is used to receive data on the raw socket. The function
797 * call will block until data has been received successfully or an error
800 * @param buffers One or more buffers into which the data will be received.
802 * @param flags Flags specifying how the receive call is to be made.
804 * @returns The number of bytes received.
806 * @throws boost::system::system_error Thrown on failure.
808 * @note The receive operation can only be used with a connected socket. Use
809 * the receive_from function to receive data on an unconnected raw
812 template <typename MutableBufferSequence>
813 std::size_t receive(const MutableBufferSequence& buffers,
814 socket_base::message_flags flags)
816 boost::system::error_code ec;
817 std::size_t s = this->impl_.get_service().receive(
818 this->impl_.get_implementation(), buffers, flags, ec);
819 boost::asio::detail::throw_error(ec, "receive");
823 /// Receive some data on a connected socket.
825 * This function is used to receive data on the raw socket. The function
826 * call will block until data has been received successfully or an error
829 * @param buffers One or more buffers into which the data will be received.
831 * @param flags Flags specifying how the receive call is to be made.
833 * @param ec Set to indicate what error occurred, if any.
835 * @returns The number of bytes received.
837 * @note The receive operation can only be used with a connected socket. Use
838 * the receive_from function to receive data on an unconnected raw
841 template <typename MutableBufferSequence>
842 std::size_t receive(const MutableBufferSequence& buffers,
843 socket_base::message_flags flags, boost::system::error_code& ec)
845 return this->impl_.get_service().receive(
846 this->impl_.get_implementation(), buffers, flags, ec);
849 /// Start an asynchronous receive on a connected socket.
851 * This function is used to asynchronously receive data from the raw
852 * socket. It is an initiating function for an @ref asynchronous_operation,
853 * and always returns immediately.
855 * @param buffers One or more buffers into which the data will be received.
856 * Although the buffers object may be copied as necessary, ownership of the
857 * underlying memory blocks is retained by the caller, which must guarantee
858 * that they remain valid until the completion handler is called.
860 * @param token The @ref completion_token that will be used to produce a
861 * completion handler, which will be called when the receive completes.
862 * Potential completion tokens include @ref use_future, @ref use_awaitable,
863 * @ref yield_context, or a function object with the correct completion
864 * signature. The function signature of the completion handler must be:
865 * @code void handler(
866 * const boost::system::error_code& error, // Result of operation.
867 * std::size_t bytes_transferred // Number of bytes received.
869 * Regardless of whether the asynchronous operation completes immediately or
870 * not, the completion handler will not be invoked from within this function.
871 * On immediate completion, invocation of the handler will be performed in a
872 * manner equivalent to using boost::asio::post().
874 * @par Completion Signature
875 * @code void(boost::system::error_code, std::size_t) @endcode
877 * @note The async_receive operation can only be used with a connected socket.
878 * Use the async_receive_from function to receive data on an unconnected
882 * To receive into a single data buffer use the @ref buffer function as
885 * socket.async_receive(boost::asio::buffer(data, size), handler);
887 * See the @ref buffer documentation for information on receiving into
888 * multiple buffers in one go, and how to use it with arrays, boost::array or
891 * @par Per-Operation Cancellation
892 * On POSIX or Windows operating systems, this asynchronous operation supports
893 * cancellation for the following boost::asio::cancellation_type values:
895 * @li @c cancellation_type::terminal
897 * @li @c cancellation_type::partial
899 * @li @c cancellation_type::total
901 template <typename MutableBufferSequence,
902 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
903 std::size_t)) ReadToken
904 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
905 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
906 void (boost::system::error_code, std::size_t))
907 async_receive(const MutableBufferSequence& buffers,
908 BOOST_ASIO_MOVE_ARG(ReadToken) token
909 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
911 return async_initiate<ReadToken,
912 void (boost::system::error_code, std::size_t)>(
913 initiate_async_receive(this), token,
914 buffers, socket_base::message_flags(0));
917 /// Start an asynchronous receive on a connected socket.
919 * This function is used to asynchronously receive data from the raw
920 * socket. It is an initiating function for an @ref asynchronous_operation,
921 * and always returns immediately.
923 * @param buffers One or more buffers into which the data will be received.
924 * Although the buffers object may be copied as necessary, ownership of the
925 * underlying memory blocks is retained by the caller, which must guarantee
926 * that they remain valid until the completion handler is called.
928 * @param flags Flags specifying how the receive call is to be made.
930 * @param token The @ref completion_token that will be used to produce a
931 * completion handler, which will be called when the receive completes.
932 * Potential completion tokens include @ref use_future, @ref use_awaitable,
933 * @ref yield_context, or a function object with the correct completion
934 * signature. The function signature of the completion handler must be:
935 * @code void handler(
936 * const boost::system::error_code& error, // Result of operation.
937 * std::size_t bytes_transferred // Number of bytes received.
939 * Regardless of whether the asynchronous operation completes immediately or
940 * not, the completion handler will not be invoked from within this function.
941 * On immediate completion, invocation of the handler will be performed in a
942 * manner equivalent to using boost::asio::post().
944 * @par Completion Signature
945 * @code void(boost::system::error_code, std::size_t) @endcode
947 * @note The async_receive operation can only be used with a connected socket.
948 * Use the async_receive_from function to receive data on an unconnected
951 * @par Per-Operation Cancellation
952 * On POSIX or Windows operating systems, this asynchronous operation supports
953 * cancellation for the following boost::asio::cancellation_type values:
955 * @li @c cancellation_type::terminal
957 * @li @c cancellation_type::partial
959 * @li @c cancellation_type::total
961 template <typename MutableBufferSequence,
962 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
963 std::size_t)) ReadToken
964 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
965 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
966 void (boost::system::error_code, std::size_t))
967 async_receive(const MutableBufferSequence& buffers,
968 socket_base::message_flags flags,
969 BOOST_ASIO_MOVE_ARG(ReadToken) token
970 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
972 return async_initiate<ReadToken,
973 void (boost::system::error_code, std::size_t)>(
974 initiate_async_receive(this), token, buffers, flags);
977 /// Receive raw data with the endpoint of the sender.
979 * This function is used to receive raw data. The function call will block
980 * until data has been received successfully or an error occurs.
982 * @param buffers One or more buffers into which the data will be received.
984 * @param sender_endpoint An endpoint object that receives the endpoint of
985 * the remote sender of the data.
987 * @returns The number of bytes received.
989 * @throws boost::system::system_error Thrown on failure.
992 * To receive into a single data buffer use the @ref buffer function as
995 * boost::asio::ip::udp::endpoint sender_endpoint;
996 * socket.receive_from(
997 * boost::asio::buffer(data, size), sender_endpoint);
999 * See the @ref buffer documentation for information on receiving into
1000 * multiple buffers in one go, and how to use it with arrays, boost::array or
1003 template <typename MutableBufferSequence>
1004 std::size_t receive_from(const MutableBufferSequence& buffers,
1005 endpoint_type& sender_endpoint)
1007 boost::system::error_code ec;
1008 std::size_t s = this->impl_.get_service().receive_from(
1009 this->impl_.get_implementation(), buffers, sender_endpoint, 0, ec);
1010 boost::asio::detail::throw_error(ec, "receive_from");
1014 /// Receive raw data with the endpoint of the sender.
1016 * This function is used to receive raw data. The function call will block
1017 * until data has been received successfully or an error occurs.
1019 * @param buffers One or more buffers into which the data will be received.
1021 * @param sender_endpoint An endpoint object that receives the endpoint of
1022 * the remote sender of the data.
1024 * @param flags Flags specifying how the receive call is to be made.
1026 * @returns The number of bytes received.
1028 * @throws boost::system::system_error Thrown on failure.
1030 template <typename MutableBufferSequence>
1031 std::size_t receive_from(const MutableBufferSequence& buffers,
1032 endpoint_type& sender_endpoint, socket_base::message_flags flags)
1034 boost::system::error_code ec;
1035 std::size_t s = this->impl_.get_service().receive_from(
1036 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
1037 boost::asio::detail::throw_error(ec, "receive_from");
1041 /// Receive raw data with the endpoint of the sender.
1043 * This function is used to receive raw data. The function call will block
1044 * until data has been received successfully or an error occurs.
1046 * @param buffers One or more buffers into which the data will be received.
1048 * @param sender_endpoint An endpoint object that receives the endpoint of
1049 * the remote sender of the data.
1051 * @param flags Flags specifying how the receive call is to be made.
1053 * @param ec Set to indicate what error occurred, if any.
1055 * @returns The number of bytes received.
1057 template <typename MutableBufferSequence>
1058 std::size_t receive_from(const MutableBufferSequence& buffers,
1059 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1060 boost::system::error_code& ec)
1062 return this->impl_.get_service().receive_from(
1063 this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
1066 /// Start an asynchronous receive.
1068 * This function is used to asynchronously receive raw data. It is an
1069 * initiating function for an @ref asynchronous_operation, and always returns
1072 * @param buffers One or more buffers into which the data will be received.
1073 * Although the buffers object may be copied as necessary, ownership of the
1074 * underlying memory blocks is retained by the caller, which must guarantee
1075 * that they remain valid until the completion handler is called.
1077 * @param sender_endpoint An endpoint object that receives the endpoint of
1078 * the remote sender of the data. Ownership of the sender_endpoint object
1079 * is retained by the caller, which must guarantee that it is valid until the
1080 * completion handler is called.
1082 * @param token The @ref completion_token that will be used to produce a
1083 * completion handler, which will be called when the receive completes.
1084 * Potential completion tokens include @ref use_future, @ref use_awaitable,
1085 * @ref yield_context, or a function object with the correct completion
1086 * signature. The function signature of the completion handler must be:
1087 * @code void handler(
1088 * const boost::system::error_code& error, // Result of operation.
1089 * std::size_t bytes_transferred // Number of bytes received.
1091 * Regardless of whether the asynchronous operation completes immediately or
1092 * not, the completion handler will not be invoked from within this function.
1093 * On immediate completion, invocation of the handler will be performed in a
1094 * manner equivalent to using boost::asio::post().
1096 * @par Completion Signature
1097 * @code void(boost::system::error_code, std::size_t) @endcode
1100 * To receive into a single data buffer use the @ref buffer function as
1102 * @code socket.async_receive_from(
1103 * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
1104 * See the @ref buffer documentation for information on receiving into
1105 * multiple buffers in one go, and how to use it with arrays, boost::array or
1108 * @par Per-Operation Cancellation
1109 * On POSIX or Windows operating systems, this asynchronous operation supports
1110 * cancellation for the following boost::asio::cancellation_type values:
1112 * @li @c cancellation_type::terminal
1114 * @li @c cancellation_type::partial
1116 * @li @c cancellation_type::total
1118 template <typename MutableBufferSequence,
1119 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1120 std::size_t)) ReadToken
1121 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1122 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
1123 void (boost::system::error_code, std::size_t))
1124 async_receive_from(const MutableBufferSequence& buffers,
1125 endpoint_type& sender_endpoint,
1126 BOOST_ASIO_MOVE_ARG(ReadToken) token
1127 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1129 return async_initiate<ReadToken,
1130 void (boost::system::error_code, std::size_t)>(
1131 initiate_async_receive_from(this), token, buffers,
1132 &sender_endpoint, socket_base::message_flags(0));
1135 /// Start an asynchronous receive.
1137 * This function is used to asynchronously receive raw data. It is an
1138 * initiating function for an @ref asynchronous_operation, and always returns
1141 * @param buffers One or more buffers into which the data will be received.
1142 * Although the buffers object may be copied as necessary, ownership of the
1143 * underlying memory blocks is retained by the caller, which must guarantee
1144 * that they remain valid until the completion handler is called.
1146 * @param sender_endpoint An endpoint object that receives the endpoint of
1147 * the remote sender of the data. Ownership of the sender_endpoint object
1148 * is retained by the caller, which must guarantee that it is valid until the
1149 * completion handler is called.
1151 * @param flags Flags specifying how the receive call is to be made.
1153 * @param token The @ref completion_token that will be used to produce a
1154 * completion handler, which will be called when the receive completes.
1155 * Potential completion tokens include @ref use_future, @ref use_awaitable,
1156 * @ref yield_context, or a function object with the correct completion
1157 * signature. The function signature of the completion handler must be:
1158 * @code void handler(
1159 * const boost::system::error_code& error, // Result of operation.
1160 * std::size_t bytes_transferred // Number of bytes received.
1162 * Regardless of whether the asynchronous operation completes immediately or
1163 * not, the completion handler will not be invoked from within this function.
1164 * On immediate completion, invocation of the handler will be performed in a
1165 * manner equivalent to using boost::asio::post().
1167 * @par Completion Signature
1168 * @code void(boost::system::error_code, std::size_t) @endcode
1170 * @par Per-Operation Cancellation
1171 * On POSIX or Windows operating systems, this asynchronous operation supports
1172 * cancellation for the following boost::asio::cancellation_type values:
1174 * @li @c cancellation_type::terminal
1176 * @li @c cancellation_type::partial
1178 * @li @c cancellation_type::total
1180 template <typename MutableBufferSequence,
1181 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1182 std::size_t)) ReadToken
1183 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1184 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
1185 void (boost::system::error_code, std::size_t))
1186 async_receive_from(const MutableBufferSequence& buffers,
1187 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1188 BOOST_ASIO_MOVE_ARG(ReadToken) token
1189 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1191 return async_initiate<ReadToken,
1192 void (boost::system::error_code, std::size_t)>(
1193 initiate_async_receive_from(this), token,
1194 buffers, &sender_endpoint, flags);
1198 // Disallow copying and assignment.
1199 basic_raw_socket(const basic_raw_socket&) BOOST_ASIO_DELETED;
1200 basic_raw_socket& operator=(const basic_raw_socket&) BOOST_ASIO_DELETED;
1202 class initiate_async_send
1205 typedef Executor executor_type;
1207 explicit initiate_async_send(basic_raw_socket* self)
1212 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1214 return self_->get_executor();
1217 template <typename WriteHandler, typename ConstBufferSequence>
1218 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1219 const ConstBufferSequence& buffers,
1220 socket_base::message_flags flags) const
1222 // If you get an error on the following line it means that your handler
1223 // does not meet the documented type requirements for a WriteHandler.
1224 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1226 detail::non_const_lvalue<WriteHandler> handler2(handler);
1227 self_->impl_.get_service().async_send(
1228 self_->impl_.get_implementation(), buffers, flags,
1229 handler2.value, self_->impl_.get_executor());
1233 basic_raw_socket* self_;
1236 class initiate_async_send_to
1239 typedef Executor executor_type;
1241 explicit initiate_async_send_to(basic_raw_socket* self)
1246 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1248 return self_->get_executor();
1251 template <typename WriteHandler, typename ConstBufferSequence>
1252 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
1253 const ConstBufferSequence& buffers, const endpoint_type& destination,
1254 socket_base::message_flags flags) const
1256 // If you get an error on the following line it means that your handler
1257 // does not meet the documented type requirements for a WriteHandler.
1258 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
1260 detail::non_const_lvalue<WriteHandler> handler2(handler);
1261 self_->impl_.get_service().async_send_to(
1262 self_->impl_.get_implementation(), buffers, destination,
1263 flags, handler2.value, self_->impl_.get_executor());
1267 basic_raw_socket* self_;
1270 class initiate_async_receive
1273 typedef Executor executor_type;
1275 explicit initiate_async_receive(basic_raw_socket* self)
1280 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1282 return self_->get_executor();
1285 template <typename ReadHandler, typename MutableBufferSequence>
1286 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1287 const MutableBufferSequence& buffers,
1288 socket_base::message_flags flags) const
1290 // If you get an error on the following line it means that your handler
1291 // does not meet the documented type requirements for a ReadHandler.
1292 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1294 detail::non_const_lvalue<ReadHandler> handler2(handler);
1295 self_->impl_.get_service().async_receive(
1296 self_->impl_.get_implementation(), buffers, flags,
1297 handler2.value, self_->impl_.get_executor());
1301 basic_raw_socket* self_;
1304 class initiate_async_receive_from
1307 typedef Executor executor_type;
1309 explicit initiate_async_receive_from(basic_raw_socket* self)
1314 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1316 return self_->get_executor();
1319 template <typename ReadHandler, typename MutableBufferSequence>
1320 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
1321 const MutableBufferSequence& buffers, endpoint_type* sender_endpoint,
1322 socket_base::message_flags flags) const
1324 // If you get an error on the following line it means that your handler
1325 // does not meet the documented type requirements for a ReadHandler.
1326 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1328 detail::non_const_lvalue<ReadHandler> handler2(handler);
1329 self_->impl_.get_service().async_receive_from(
1330 self_->impl_.get_implementation(), buffers, *sender_endpoint,
1331 flags, handler2.value, self_->impl_.get_executor());
1335 basic_raw_socket* self_;
1340 } // namespace boost
1342 #include <boost/asio/detail/pop_options.hpp>
1344 #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP