Chris@16: // Chris@16: // socket_base.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_SOCKET_BASE_HPP Chris@16: #define BOOST_ASIO_SOCKET_BASE_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: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// The socket_base class is used as a base for the basic_stream_socket and Chris@16: /// basic_datagram_socket class templates so that we have a common place to Chris@16: /// define the shutdown_type and enum. Chris@16: class socket_base Chris@16: { Chris@16: public: Chris@16: /// Different ways a socket may be shutdown. Chris@16: enum shutdown_type Chris@16: { Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// Shutdown the receive side of the socket. Chris@16: shutdown_receive = implementation_defined, Chris@16: Chris@16: /// Shutdown the send side of the socket. Chris@16: shutdown_send = implementation_defined, Chris@16: Chris@16: /// Shutdown both send and receive on the socket. Chris@16: shutdown_both = implementation_defined Chris@16: #else Chris@16: shutdown_receive = BOOST_ASIO_OS_DEF(SHUT_RD), Chris@16: shutdown_send = BOOST_ASIO_OS_DEF(SHUT_WR), Chris@16: shutdown_both = BOOST_ASIO_OS_DEF(SHUT_RDWR) Chris@16: #endif Chris@16: }; Chris@16: Chris@16: /// Bitmask type for flags that can be passed to send and receive operations. Chris@16: typedef int message_flags; Chris@16: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// Peek at incoming data without removing it from the input queue. Chris@16: static const int message_peek = implementation_defined; Chris@16: Chris@16: /// Process out-of-band data. Chris@16: static const int message_out_of_band = implementation_defined; Chris@16: Chris@16: /// Specify that the data should not be subject to routing. Chris@16: static const int message_do_not_route = implementation_defined; Chris@16: Chris@16: /// Specifies that the data marks the end of a record. Chris@16: static const int message_end_of_record = implementation_defined; Chris@16: #else Chris@16: BOOST_ASIO_STATIC_CONSTANT(int, Chris@16: message_peek = BOOST_ASIO_OS_DEF(MSG_PEEK)); Chris@16: BOOST_ASIO_STATIC_CONSTANT(int, Chris@16: message_out_of_band = BOOST_ASIO_OS_DEF(MSG_OOB)); Chris@16: BOOST_ASIO_STATIC_CONSTANT(int, Chris@16: message_do_not_route = BOOST_ASIO_OS_DEF(MSG_DONTROUTE)); Chris@16: BOOST_ASIO_STATIC_CONSTANT(int, Chris@16: message_end_of_record = BOOST_ASIO_OS_DEF(MSG_EOR)); Chris@16: #endif Chris@16: Chris@16: /// Socket option to permit sending of broadcast messages. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_BROADCAST socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::udp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::broadcast option(true); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::udp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::broadcast option; Chris@16: * socket.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined broadcast; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_BROADCAST)> Chris@16: broadcast; Chris@16: #endif Chris@16: Chris@16: /// Socket option to enable socket-level debugging. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_DEBUG socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::debug option(true); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::debug option; Chris@16: * socket.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined debug; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DEBUG)> debug; Chris@16: #endif Chris@16: Chris@16: /// Socket option to prevent routing, use local interfaces only. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_DONTROUTE socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::udp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::do_not_route option(true); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::udp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::do_not_route option; Chris@16: * socket.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined do_not_route; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DONTROUTE)> Chris@16: do_not_route; Chris@16: #endif Chris@16: Chris@16: /// Socket option to send keep-alives. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_KEEPALIVE socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::keep_alive option(true); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::keep_alive option; Chris@16: * socket.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined keep_alive; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_KEEPALIVE)> keep_alive; Chris@16: #endif Chris@16: Chris@16: /// Socket option for the send buffer size of a socket. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_SNDBUF socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::send_buffer_size option(8192); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::send_buffer_size option; Chris@16: * socket.get_option(option); Chris@16: * int size = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Integer_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined send_buffer_size; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::integer< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDBUF)> Chris@16: send_buffer_size; Chris@16: #endif Chris@16: Chris@16: /// Socket option for the send low watermark. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_SNDLOWAT socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::send_low_watermark option(1024); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::send_low_watermark option; Chris@16: * socket.get_option(option); Chris@16: * int size = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Integer_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined send_low_watermark; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::integer< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDLOWAT)> Chris@16: send_low_watermark; Chris@16: #endif Chris@16: Chris@16: /// Socket option for the receive buffer size of a socket. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_RCVBUF socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::receive_buffer_size option(8192); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::receive_buffer_size option; Chris@16: * socket.get_option(option); Chris@16: * int size = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Integer_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined receive_buffer_size; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::integer< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVBUF)> Chris@16: receive_buffer_size; Chris@16: #endif Chris@16: Chris@16: /// Socket option for the receive low watermark. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_RCVLOWAT socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::receive_low_watermark option(1024); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::receive_low_watermark option; Chris@16: * socket.get_option(option); Chris@16: * int size = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Integer_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined receive_low_watermark; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::integer< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVLOWAT)> Chris@16: receive_low_watermark; Chris@16: #endif Chris@16: Chris@16: /// Socket option to allow the socket to be bound to an address that is Chris@16: /// already in use. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_REUSEADDR socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::reuse_address option(true); Chris@16: * acceptor.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::reuse_address option; Chris@16: * acceptor.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined reuse_address; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_REUSEADDR)> Chris@16: reuse_address; Chris@16: #endif Chris@16: Chris@16: /// Socket option to specify whether the socket lingers on close if unsent Chris@16: /// data is present. Chris@16: /** Chris@16: * Implements the SOL_SOCKET/SO_LINGER socket option. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::linger option(true, 30); Chris@16: * socket.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::linger option; Chris@16: * socket.get_option(option); Chris@16: * bool is_set = option.enabled(); Chris@16: * unsigned short timeout = option.timeout(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Linger_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined linger; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::linger< Chris@16: BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_LINGER)> Chris@16: linger; Chris@16: #endif Chris@16: Chris@16: /// Socket option to report aborted connections on accept. Chris@16: /** Chris@16: * Implements a custom socket option that determines whether or not an accept Chris@16: * operation is permitted to fail with boost::asio::error::connection_aborted. Chris@16: * By default the option is false. Chris@16: * Chris@16: * @par Examples Chris@16: * Setting the option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::enable_connection_aborted option(true); Chris@16: * acceptor.set_option(option); Chris@16: * @endcode Chris@16: * Chris@16: * @par Chris@16: * Getting the current option value: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::enable_connection_aborted option; Chris@16: * acceptor.get_option(option); Chris@16: * bool is_set = option.value(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * Socket_Option, Boolean_Socket_Option. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined enable_connection_aborted; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_option::boolean< Chris@16: boost::asio::detail::custom_socket_option_level, Chris@16: boost::asio::detail::enable_connection_aborted_option> Chris@16: enable_connection_aborted; Chris@16: #endif Chris@16: Chris@16: /// (Deprecated: Use non_blocking().) IO control command to Chris@16: /// set the blocking mode of the socket. Chris@16: /** Chris@16: * Implements the FIONBIO IO control command. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::non_blocking_io command(true); Chris@16: * socket.io_control(command); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * IO_Control_Command, Boolean_IO_Control_Command. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined non_blocking_io; Chris@16: #else Chris@16: typedef boost::asio::detail::io_control::non_blocking_io non_blocking_io; Chris@16: #endif Chris@16: Chris@16: /// IO control command to get the amount of data that can be read without Chris@16: /// blocking. Chris@16: /** Chris@16: * Implements the FIONREAD IO control command. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * ... Chris@16: * boost::asio::socket_base::bytes_readable command(true); Chris@16: * socket.io_control(command); Chris@16: * std::size_t bytes_readable = command.get(); Chris@16: * @endcode Chris@16: * Chris@16: * @par Concepts: Chris@16: * IO_Control_Command, Size_IO_Control_Command. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined bytes_readable; Chris@16: #else Chris@16: typedef boost::asio::detail::io_control::bytes_readable bytes_readable; Chris@16: #endif Chris@16: Chris@16: /// The maximum length of the queue of pending incoming connections. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: static const int max_connections = implementation_defined; Chris@16: #else Chris@16: BOOST_ASIO_STATIC_CONSTANT(int, max_connections Chris@16: = BOOST_ASIO_OS_DEF(SOMAXCONN)); Chris@16: #endif Chris@16: Chris@16: protected: Chris@16: /// Protected destructor to prevent deletion through this type. Chris@16: ~socket_base() Chris@16: { 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_SOCKET_BASE_HPP