Chris@16: // Chris@16: // detail/reactive_socket_service_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_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP Chris@16: #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_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: Chris@16: #if !defined(BOOST_ASIO_HAS_IOCP) \ Chris@16: && !defined(BOOST_ASIO_WINDOWS_RUNTIME) 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: #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: namespace detail { Chris@16: Chris@16: class reactive_socket_service_base Chris@16: { Chris@16: public: Chris@16: // The native type of a socket. Chris@16: typedef socket_type native_handle_type; Chris@16: Chris@16: // The implementation type of the socket. Chris@16: struct base_implementation_type Chris@16: { Chris@16: // The native socket representation. Chris@16: socket_type socket_; Chris@16: Chris@16: // The current state of the socket. Chris@16: socket_ops::state_type state_; Chris@16: Chris@16: // Per-descriptor data used by the reactor. Chris@16: reactor::per_descriptor_data reactor_data_; Chris@16: }; Chris@16: Chris@16: // Constructor. Chris@16: BOOST_ASIO_DECL reactive_socket_service_base( Chris@16: boost::asio::io_service& io_service); Chris@16: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: BOOST_ASIO_DECL void shutdown_service(); Chris@16: Chris@16: // Construct a new socket implementation. Chris@16: BOOST_ASIO_DECL void construct(base_implementation_type& impl); Chris@16: Chris@16: // Move-construct a new socket implementation. Chris@16: BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl, Chris@16: base_implementation_type& other_impl); Chris@16: Chris@16: // Move-assign from another socket implementation. Chris@16: BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl, Chris@16: reactive_socket_service_base& other_service, Chris@16: base_implementation_type& other_impl); Chris@16: Chris@16: // Destroy a socket implementation. Chris@16: BOOST_ASIO_DECL void destroy(base_implementation_type& impl); Chris@16: Chris@16: // Determine whether the socket is open. Chris@16: bool is_open(const base_implementation_type& impl) const Chris@16: { Chris@16: return impl.socket_ != invalid_socket; Chris@16: } Chris@16: Chris@16: // Destroy a socket implementation. Chris@16: BOOST_ASIO_DECL boost::system::error_code close( Chris@16: base_implementation_type& impl, boost::system::error_code& ec); Chris@16: Chris@16: // Get the native socket representation. Chris@16: native_handle_type native_handle(base_implementation_type& impl) Chris@16: { Chris@16: return impl.socket_; Chris@16: } Chris@16: Chris@16: // Cancel all operations associated with the socket. Chris@16: BOOST_ASIO_DECL boost::system::error_code cancel( Chris@16: base_implementation_type& impl, boost::system::error_code& ec); Chris@16: Chris@16: // Determine whether the socket is at the out-of-band data mark. Chris@16: bool at_mark(const base_implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: return socket_ops::sockatmark(impl.socket_, ec); Chris@16: } Chris@16: Chris@16: // Determine the number of bytes available for reading. Chris@16: std::size_t available(const base_implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: return socket_ops::available(impl.socket_, ec); Chris@16: } Chris@16: Chris@16: // Place the socket into the state where it will listen for new connections. Chris@16: boost::system::error_code listen(base_implementation_type& impl, Chris@16: int backlog, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::listen(impl.socket_, backlog, ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Perform an IO control command on the socket. Chris@16: template Chris@16: boost::system::error_code io_control(base_implementation_type& impl, Chris@16: IO_Control_Command& command, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::ioctl(impl.socket_, impl.state_, command.name(), Chris@16: static_cast(command.data()), ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Gets the non-blocking mode of the socket. Chris@16: bool non_blocking(const base_implementation_type& impl) const Chris@16: { Chris@16: return (impl.state_ & socket_ops::user_set_non_blocking) != 0; Chris@16: } Chris@16: Chris@16: // Sets the non-blocking mode of the socket. Chris@16: boost::system::error_code non_blocking(base_implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Gets the non-blocking mode of the native socket implementation. Chris@16: bool native_non_blocking(const base_implementation_type& impl) const Chris@16: { Chris@16: return (impl.state_ & socket_ops::internal_non_blocking) != 0; Chris@16: } Chris@16: Chris@16: // Sets the non-blocking mode of the native socket implementation. Chris@16: boost::system::error_code native_non_blocking(base_implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Disable sends or receives on the socket. Chris@16: boost::system::error_code shutdown(base_implementation_type& impl, Chris@16: socket_base::shutdown_type what, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::shutdown(impl.socket_, what, ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Send the given data to the peer. Chris@16: template Chris@16: size_t send(base_implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: return socket_ops::sync_send(impl.socket_, impl.state_, Chris@16: bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be sent without blocking. Chris@16: size_t send(base_implementation_type& impl, const null_buffers&, Chris@16: socket_base::message_flags, boost::system::error_code& ec) Chris@16: { Chris@16: // Wait for socket to become ready. Chris@16: socket_ops::poll_write(impl.socket_, impl.state_, ec); Chris@16: Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // Start an asynchronous send. The data being sent must be valid for the Chris@16: // lifetime of the asynchronous operation. Chris@16: template Chris@16: void async_send(base_implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_socket_send_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(impl.socket_, buffers, flags, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send")); Chris@16: Chris@16: start_op(impl, reactor::write_op, p.p, is_continuation, true, Chris@16: ((impl.state_ & socket_ops::stream_oriented) Chris@16: && buffer_sequence_adapter::all_empty(buffers))); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Start an asynchronous wait until data can be sent without blocking. Chris@16: template Chris@16: void async_send(base_implementation_type& impl, const null_buffers&, Chris@16: socket_base::message_flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_null_buffers_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", Chris@16: &impl, "async_send(null_buffers)")); Chris@16: Chris@16: start_op(impl, reactor::write_op, p.p, is_continuation, false, false); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Receive some data from the peer. Returns the number of bytes received. Chris@16: template Chris@16: size_t receive(base_implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: return socket_ops::sync_recv(impl.socket_, impl.state_, Chris@16: bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: size_t receive(base_implementation_type& impl, const null_buffers&, Chris@16: socket_base::message_flags, boost::system::error_code& ec) Chris@16: { Chris@16: // Wait for socket to become ready. Chris@16: socket_ops::poll_read(impl.socket_, impl.state_, ec); Chris@16: Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // Start an asynchronous receive. The buffer for the data being received Chris@16: // must be valid for the lifetime of the asynchronous operation. Chris@16: template Chris@16: void async_receive(base_implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_socket_recv_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(impl.socket_, impl.state_, buffers, flags, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive")); Chris@16: Chris@16: start_op(impl, Chris@16: (flags & socket_base::message_out_of_band) Chris@16: ? reactor::except_op : reactor::read_op, Chris@16: p.p, is_continuation, Chris@16: (flags & socket_base::message_out_of_band) == 0, Chris@16: ((impl.state_ & socket_ops::stream_oriented) Chris@16: && buffer_sequence_adapter::all_empty(buffers))); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: template Chris@16: void async_receive(base_implementation_type& impl, const null_buffers&, Chris@16: socket_base::message_flags flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_null_buffers_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", Chris@16: &impl, "async_receive(null_buffers)")); Chris@16: Chris@16: start_op(impl, Chris@16: (flags & socket_base::message_out_of_band) Chris@16: ? reactor::except_op : reactor::read_op, Chris@16: p.p, is_continuation, false, false); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Receive some data with associated flags. Returns the number of bytes Chris@16: // received. Chris@16: template Chris@16: size_t receive_with_flags(base_implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags in_flags, Chris@16: socket_base::message_flags& out_flags, boost::system::error_code& ec) Chris@16: { Chris@16: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: return socket_ops::sync_recvmsg(impl.socket_, impl.state_, Chris@16: bufs.buffers(), bufs.count(), in_flags, out_flags, ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: size_t receive_with_flags(base_implementation_type& impl, Chris@16: const null_buffers&, socket_base::message_flags, Chris@16: socket_base::message_flags& out_flags, boost::system::error_code& ec) Chris@16: { Chris@16: // Wait for socket to become ready. Chris@16: socket_ops::poll_read(impl.socket_, impl.state_, ec); Chris@16: Chris@16: // Clear out_flags, since we cannot give it any other sensible value when Chris@16: // performing a null_buffers operation. Chris@16: out_flags = 0; Chris@16: Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // Start an asynchronous receive. The buffer for the data being received Chris@16: // must be valid for the lifetime of the asynchronous operation. Chris@16: template Chris@16: void async_receive_with_flags(base_implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, socket_base::message_flags in_flags, Chris@16: socket_base::message_flags& out_flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_socket_recvmsg_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(impl.socket_, buffers, in_flags, out_flags, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", Chris@16: &impl, "async_receive_with_flags")); Chris@16: Chris@16: start_op(impl, Chris@16: (in_flags & socket_base::message_out_of_band) Chris@16: ? reactor::except_op : reactor::read_op, Chris@16: p.p, is_continuation, Chris@16: (in_flags & socket_base::message_out_of_band) == 0, false); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: template Chris@16: void async_receive_with_flags(base_implementation_type& impl, Chris@16: const null_buffers&, socket_base::message_flags in_flags, Chris@16: socket_base::message_flags& out_flags, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef reactive_null_buffers_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, Chris@16: "async_receive_with_flags(null_buffers)")); Chris@16: Chris@16: // Clear out_flags, since we cannot give it any other sensible value when Chris@16: // performing a null_buffers operation. Chris@16: out_flags = 0; Chris@16: Chris@16: start_op(impl, Chris@16: (in_flags & socket_base::message_out_of_band) Chris@16: ? reactor::except_op : reactor::read_op, Chris@16: p.p, is_continuation, false, false); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: protected: Chris@16: // Open a new socket implementation. Chris@16: BOOST_ASIO_DECL boost::system::error_code do_open( Chris@16: base_implementation_type& impl, int af, Chris@16: int type, int protocol, boost::system::error_code& ec); Chris@16: Chris@16: // Assign a native socket to a socket implementation. Chris@16: BOOST_ASIO_DECL boost::system::error_code do_assign( Chris@16: base_implementation_type& impl, int type, Chris@16: const native_handle_type& native_socket, boost::system::error_code& ec); Chris@16: Chris@16: // Start the asynchronous read or write operation. Chris@16: BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type, Chris@16: reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop); Chris@16: Chris@16: // Start the asynchronous accept operation. Chris@16: BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl, Chris@16: reactor_op* op, bool is_continuation, bool peer_is_open); Chris@16: Chris@16: // Start the asynchronous connect operation. Chris@16: BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl, Chris@16: reactor_op* op, bool is_continuation, Chris@16: const socket_addr_type* addr, size_t addrlen); Chris@16: Chris@16: // The selector that performs event demultiplexing for the service. Chris@16: reactor& reactor_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // !defined(BOOST_ASIO_HAS_IOCP) Chris@16: // && !defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP