Chris@16: // Chris@16: // basic_raw_socket.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_BASIC_RAW_SOCKET_HPP Chris@16: #define BOOST_ASIO_BASIC_RAW_SOCKET_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Provides raw-oriented socket functionality. Chris@16: /** Chris@16: * The basic_raw_socket class template provides asynchronous and blocking Chris@16: * raw-oriented socket functionality. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: */ Chris@16: template > Chris@16: class basic_raw_socket Chris@16: : public basic_socket Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of a Chris@16: /// socket. Chris@16: typedef typename RawSocketService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of a socket. Chris@16: typedef typename RawSocketService::native_handle_type native_handle_type; Chris@16: Chris@16: /// The protocol type. Chris@16: typedef Protocol protocol_type; Chris@16: Chris@16: /// The endpoint type. Chris@16: typedef typename Protocol::endpoint endpoint_type; Chris@16: Chris@16: /// Construct a basic_raw_socket without opening it. Chris@16: /** Chris@16: * This constructor creates a raw socket without opening it. The open() Chris@16: * function must be called before data can be sent or received on the socket. Chris@16: * Chris@16: * @param io_service The io_service object that the raw socket will use Chris@16: * to dispatch handlers for any asynchronous operations performed on the Chris@16: * socket. Chris@16: */ Chris@16: explicit basic_raw_socket(boost::asio::io_service& io_service) Chris@16: : basic_socket(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct and open a basic_raw_socket. Chris@16: /** Chris@16: * This constructor creates and opens a raw socket. Chris@16: * Chris@16: * @param io_service The io_service object that the raw socket will use Chris@16: * to dispatch handlers for any asynchronous operations performed on the Chris@16: * socket. Chris@16: * Chris@16: * @param protocol An object specifying protocol parameters to be used. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_raw_socket(boost::asio::io_service& io_service, Chris@16: const protocol_type& protocol) Chris@16: : basic_socket(io_service, protocol) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a basic_raw_socket, opening it and binding it to the given Chris@16: /// local endpoint. Chris@16: /** Chris@16: * This constructor creates a raw socket and automatically opens it bound Chris@16: * to the specified endpoint on the local machine. The protocol used is the Chris@16: * protocol associated with the given endpoint. Chris@16: * Chris@16: * @param io_service The io_service object that the raw socket will use Chris@16: * to dispatch handlers for any asynchronous operations performed on the Chris@16: * socket. Chris@16: * Chris@16: * @param endpoint An endpoint on the local machine to which the raw Chris@16: * socket will be bound. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_raw_socket(boost::asio::io_service& io_service, Chris@16: const endpoint_type& endpoint) Chris@16: : basic_socket(io_service, endpoint) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a basic_raw_socket on an existing native socket. Chris@16: /** Chris@16: * This constructor creates a raw socket object to hold an existing Chris@16: * native socket. Chris@16: * Chris@16: * @param io_service The io_service object that the raw socket will use Chris@16: * to dispatch handlers for any asynchronous operations performed on the Chris@16: * socket. Chris@16: * Chris@16: * @param protocol An object specifying protocol parameters to be used. Chris@16: * Chris@16: * @param native_socket The new underlying socket implementation. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_raw_socket(boost::asio::io_service& io_service, Chris@16: const protocol_type& protocol, const native_handle_type& native_socket) Chris@16: : basic_socket( Chris@16: io_service, protocol, native_socket) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_raw_socket from another. Chris@16: /** Chris@16: * This constructor moves a raw socket from one object to another. Chris@16: * Chris@16: * @param other The other basic_raw_socket object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_raw_socket(io_service&) constructor. Chris@16: */ Chris@16: basic_raw_socket(basic_raw_socket&& other) Chris@16: : basic_socket( Chris@16: BOOST_ASIO_MOVE_CAST(basic_raw_socket)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_raw_socket from another. Chris@16: /** Chris@16: * This assignment operator moves a raw socket from one object to another. Chris@16: * Chris@16: * @param other The other basic_raw_socket object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_raw_socket(io_service&) constructor. Chris@16: */ Chris@16: basic_raw_socket& operator=(basic_raw_socket&& other) Chris@16: { Chris@16: basic_socket::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_raw_socket)(other)); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Move-construct a basic_raw_socket from a socket of another protocol type. Chris@16: /** Chris@16: * This constructor moves a raw socket from one object to another. Chris@16: * Chris@16: * @param other The other basic_raw_socket object from which the move will Chris@16: * occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_raw_socket(io_service&) constructor. Chris@16: */ Chris@16: template Chris@16: basic_raw_socket(basic_raw_socket&& other, Chris@16: typename enable_if::value>::type* = 0) Chris@16: : basic_socket( Chris@16: BOOST_ASIO_MOVE_CAST2(basic_raw_socket< Chris@16: Protocol1, RawSocketService1>)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_raw_socket from a socket of another protocol type. Chris@16: /** Chris@16: * This assignment operator moves a raw socket from one object to another. Chris@16: * Chris@16: * @param other The other basic_raw_socket object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_raw_socket(io_service&) constructor. Chris@16: */ Chris@16: template Chris@16: typename enable_if::value, Chris@16: basic_raw_socket>::type& operator=( Chris@16: basic_raw_socket&& other) Chris@16: { Chris@16: basic_socket::operator=( Chris@16: BOOST_ASIO_MOVE_CAST2(basic_raw_socket< Chris@16: Protocol1, RawSocketService1>)(other)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Send some data on a connected socket. Chris@16: /** Chris@16: * This function is used to send data on the raw socket. The function call Chris@16: * will block until the data has been sent successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One ore more data buffers to be sent on the socket. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note The send operation can only be used with a connected socket. Use Chris@16: * the send_to function to send data on an unconnected raw socket. Chris@16: * Chris@16: * @par Example Chris@16: * To send a single data buffer use the @ref buffer function as follows: Chris@16: * @code socket.send(boost::asio::buffer(data, size)); @endcode Chris@16: * See the @ref buffer documentation for information on sending multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t send(const ConstBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().send( Chris@16: this->get_implementation(), buffers, 0, ec); Chris@16: boost::asio::detail::throw_error(ec, "send"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Send some data on a connected socket. Chris@16: /** Chris@16: * This function is used to send data on the raw socket. The function call Chris@16: * will block until the data has been sent successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One ore more data buffers to be sent on the socket. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note The send operation can only be used with a connected socket. Use Chris@16: * the send_to function to send data on an unconnected raw socket. Chris@16: */ Chris@16: template Chris@16: std::size_t send(const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().send( Chris@16: this->get_implementation(), buffers, flags, ec); Chris@16: boost::asio::detail::throw_error(ec, "send"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Send some data on a connected socket. Chris@16: /** Chris@16: * This function is used to send data on the raw socket. The function call Chris@16: * will block until the data has been sent successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent on the socket. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: * Chris@16: * @note The send operation can only be used with a connected socket. Use Chris@16: * the send_to function to send data on an unconnected raw socket. Chris@16: */ Chris@16: template Chris@16: std::size_t send(const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().send( Chris@16: this->get_implementation(), buffers, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous send on a connected socket. Chris@16: /** Chris@16: * This function is used to send data on the raw socket. The function call Chris@16: * will block until the data has been sent successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent on the socket. Although Chris@16: * the buffers object may be copied as necessary, ownership of the underlying Chris@16: * memory blocks is retained by the caller, which must guarantee that they Chris@16: * remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the send operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes sent. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The async_send operation can only be used with a connected socket. Chris@16: * Use the async_send_to function to send data on an unconnected raw Chris@16: * socket. Chris@16: * Chris@16: * @par Example Chris@16: * To send a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * socket.async_send(boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on sending multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_send(const ConstBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_send(this->get_implementation(), Chris@16: buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous send on a connected socket. Chris@16: /** Chris@16: * This function is used to send data on the raw socket. The function call Chris@16: * will block until the data has been sent successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent on the socket. Although Chris@16: * the buffers object may be copied as necessary, ownership of the underlying Chris@16: * memory blocks is retained by the caller, which must guarantee that they Chris@16: * remain valid until the handler is called. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @param handler The handler to be called when the send operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes sent. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The async_send operation can only be used with a connected socket. Chris@16: * Use the async_send_to function to send data on an unconnected raw Chris@16: * socket. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_send(const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_send(this->get_implementation(), Chris@16: buffers, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Send raw data to the specified endpoint. Chris@16: /** Chris@16: * This function is used to send raw data to the specified remote endpoint. Chris@16: * The function call will block until the data has been sent successfully or Chris@16: * an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent to the remote endpoint. Chris@16: * Chris@16: * @param destination The remote endpoint to which the data will be sent. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * To send a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * boost::asio::ip::udp::endpoint destination( Chris@16: * boost::asio::ip::address::from_string("1.2.3.4"), 12345); Chris@16: * socket.send_to(boost::asio::buffer(data, size), destination); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on sending multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t send_to(const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().send_to( Chris@16: this->get_implementation(), buffers, destination, 0, ec); Chris@16: boost::asio::detail::throw_error(ec, "send_to"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Send raw data to the specified endpoint. Chris@16: /** Chris@16: * This function is used to send raw data to the specified remote endpoint. Chris@16: * The function call will block until the data has been sent successfully or Chris@16: * an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent to the remote endpoint. Chris@16: * Chris@16: * @param destination The remote endpoint to which the data will be sent. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: template Chris@16: std::size_t send_to(const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination, socket_base::message_flags flags) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().send_to( Chris@16: this->get_implementation(), buffers, destination, flags, ec); Chris@16: boost::asio::detail::throw_error(ec, "send_to"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Send raw data to the specified endpoint. Chris@16: /** Chris@16: * This function is used to send raw data to the specified remote endpoint. Chris@16: * The function call will block until the data has been sent successfully or Chris@16: * an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent to the remote endpoint. Chris@16: * Chris@16: * @param destination The remote endpoint to which the data will be sent. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes sent. Chris@16: */ Chris@16: template Chris@16: std::size_t send_to(const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination, socket_base::message_flags flags, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().send_to(this->get_implementation(), Chris@16: buffers, destination, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous send. Chris@16: /** Chris@16: * This function is used to asynchronously send raw data to the specified Chris@16: * remote endpoint. The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent to the remote endpoint. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param destination The remote endpoint to which the data will be sent. Chris@16: * Copies will be made of the endpoint as required. Chris@16: * Chris@16: * @param handler The handler to be called when the send operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes sent. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @par Example Chris@16: * To send a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * boost::asio::ip::udp::endpoint destination( Chris@16: * boost::asio::ip::address::from_string("1.2.3.4"), 12345); Chris@16: * socket.async_send_to( Chris@16: * boost::asio::buffer(data, size), destination, handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on sending multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_send_to(const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_send_to(this->get_implementation(), Chris@16: buffers, destination, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous send. Chris@16: /** Chris@16: * This function is used to asynchronously send raw data to the specified Chris@16: * remote endpoint. The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more data buffers to be sent to the remote endpoint. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param flags Flags specifying how the send call is to be made. Chris@16: * Chris@16: * @param destination The remote endpoint to which the data will be sent. Chris@16: * Copies will be made of the endpoint as required. Chris@16: * Chris@16: * @param handler The handler to be called when the send operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes sent. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_send_to(const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination, socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_send_to( Chris@16: this->get_implementation(), buffers, destination, flags, Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Receive some data on a connected socket. Chris@16: /** Chris@16: * This function is used to receive data on the raw socket. The function Chris@16: * call will block until data has been received successfully or an error Chris@16: * occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note The receive operation can only be used with a connected socket. Use Chris@16: * the receive_from function to receive data on an unconnected raw Chris@16: * socket. Chris@16: * Chris@16: * @par Example Chris@16: * To receive into a single data buffer use the @ref buffer function as Chris@16: * follows: Chris@16: * @code socket.receive(boost::asio::buffer(data, size)); @endcode Chris@16: * See the @ref buffer documentation for information on receiving into Chris@16: * multiple buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t receive(const MutableBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().receive( Chris@16: this->get_implementation(), buffers, 0, ec); Chris@16: boost::asio::detail::throw_error(ec, "receive"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Receive some data on a connected socket. Chris@16: /** Chris@16: * This function is used to receive data on the raw socket. The function Chris@16: * call will block until data has been received successfully or an error Chris@16: * occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note The receive operation can only be used with a connected socket. Use Chris@16: * the receive_from function to receive data on an unconnected raw Chris@16: * socket. Chris@16: */ Chris@16: template Chris@16: std::size_t receive(const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().receive( Chris@16: this->get_implementation(), buffers, flags, ec); Chris@16: boost::asio::detail::throw_error(ec, "receive"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Receive some data on a connected socket. Chris@16: /** Chris@16: * This function is used to receive data on the raw socket. The function Chris@16: * call will block until data has been received successfully or an error Chris@16: * occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: * Chris@16: * @note The receive operation can only be used with a connected socket. Use Chris@16: * the receive_from function to receive data on an unconnected raw Chris@16: * socket. Chris@16: */ Chris@16: template Chris@16: std::size_t receive(const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().receive( Chris@16: this->get_implementation(), buffers, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous receive on a connected socket. Chris@16: /** Chris@16: * This function is used to asynchronously receive data from the raw Chris@16: * socket. The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the receive operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes received. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The async_receive operation can only be used with a connected socket. Chris@16: * Use the async_receive_from function to receive data on an unconnected Chris@16: * raw socket. Chris@16: * Chris@16: * @par Example Chris@16: * To receive into a single data buffer use the @ref buffer function as Chris@16: * follows: Chris@16: * @code Chris@16: * socket.async_receive(boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on receiving into Chris@16: * multiple buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_receive(const MutableBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_receive(this->get_implementation(), Chris@16: buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous receive on a connected socket. Chris@16: /** Chris@16: * This function is used to asynchronously receive data from the raw Chris@16: * socket. The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @param handler The handler to be called when the receive operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes received. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The async_receive operation can only be used with a connected socket. Chris@16: * Use the async_receive_from function to receive data on an unconnected Chris@16: * raw socket. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_receive(const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_receive(this->get_implementation(), Chris@16: buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Receive raw data with the endpoint of the sender. Chris@16: /** Chris@16: * This function is used to receive raw data. The function call will block Chris@16: * until data has been received successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @param sender_endpoint An endpoint object that receives the endpoint of Chris@16: * the remote sender of the data. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * To receive into a single data buffer use the @ref buffer function as Chris@16: * follows: Chris@16: * @code Chris@16: * boost::asio::ip::udp::endpoint sender_endpoint; Chris@16: * socket.receive_from( Chris@16: * boost::asio::buffer(data, size), sender_endpoint); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on receiving into Chris@16: * multiple buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t receive_from(const MutableBufferSequence& buffers, Chris@16: endpoint_type& sender_endpoint) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().receive_from( Chris@16: this->get_implementation(), buffers, sender_endpoint, 0, ec); Chris@16: boost::asio::detail::throw_error(ec, "receive_from"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Receive raw data with the endpoint of the sender. Chris@16: /** Chris@16: * This function is used to receive raw data. The function call will block Chris@16: * until data has been received successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @param sender_endpoint An endpoint object that receives the endpoint of Chris@16: * the remote sender of the data. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: template Chris@16: std::size_t receive_from(const MutableBufferSequence& buffers, Chris@16: endpoint_type& sender_endpoint, socket_base::message_flags flags) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().receive_from( Chris@16: this->get_implementation(), buffers, sender_endpoint, flags, ec); Chris@16: boost::asio::detail::throw_error(ec, "receive_from"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Receive raw data with the endpoint of the sender. Chris@16: /** Chris@16: * This function is used to receive raw data. The function call will block Chris@16: * until data has been received successfully or an error occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Chris@16: * @param sender_endpoint An endpoint object that receives the endpoint of Chris@16: * the remote sender of the data. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes received. Chris@16: */ Chris@16: template Chris@16: std::size_t receive_from(const MutableBufferSequence& buffers, Chris@16: endpoint_type& sender_endpoint, socket_base::message_flags flags, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().receive_from(this->get_implementation(), Chris@16: buffers, sender_endpoint, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous receive. Chris@16: /** Chris@16: * This function is used to asynchronously receive raw data. The function Chris@16: * call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param sender_endpoint An endpoint object that receives the endpoint of Chris@16: * the remote sender of the data. Ownership of the sender_endpoint object Chris@16: * is retained by the caller, which must guarantee that it is valid until the Chris@16: * handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the receive operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes received. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @par Example Chris@16: * To receive into a single data buffer use the @ref buffer function as Chris@16: * follows: Chris@16: * @code socket.async_receive_from( Chris@16: * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode Chris@16: * See the @ref buffer documentation for information on receiving into Chris@16: * multiple buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_receive_from(const MutableBufferSequence& buffers, Chris@16: endpoint_type& sender_endpoint, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_receive_from( Chris@16: this->get_implementation(), buffers, sender_endpoint, 0, Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous receive. Chris@16: /** Chris@16: * This function is used to asynchronously receive raw data. The function Chris@16: * call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be received. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param sender_endpoint An endpoint object that receives the endpoint of Chris@16: * the remote sender of the data. Ownership of the sender_endpoint object Chris@16: * is retained by the caller, which must guarantee that it is valid until the Chris@16: * handler is called. Chris@16: * Chris@16: * @param flags Flags specifying how the receive call is to be made. Chris@16: * Chris@16: * @param handler The handler to be called when the receive operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes received. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_receive_from(const MutableBufferSequence& buffers, Chris@16: endpoint_type& sender_endpoint, socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_receive_from( Chris@16: this->get_implementation(), buffers, sender_endpoint, flags, Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP