Chris@16: // Chris@16: // detail/reactive_socket_service.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_HPP Chris@16: #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_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: 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: #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: template Chris@16: class reactive_socket_service : Chris@16: public reactive_socket_service_base Chris@16: { Chris@16: public: 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: // 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 implementation_type : Chris@16: reactive_socket_service_base::base_implementation_type Chris@16: { Chris@16: // Default constructor. Chris@16: implementation_type() Chris@16: : protocol_(endpoint_type().protocol()) Chris@16: { Chris@16: } Chris@16: Chris@16: // The protocol associated with the socket. Chris@16: protocol_type protocol_; Chris@16: }; Chris@16: Chris@16: // Constructor. Chris@16: reactive_socket_service(boost::asio::io_service& io_service) Chris@16: : reactive_socket_service_base(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: // Move-construct a new socket implementation. Chris@16: void move_construct(implementation_type& impl, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: this->base_move_construct(impl, other_impl); Chris@16: Chris@16: impl.protocol_ = other_impl.protocol_; Chris@16: other_impl.protocol_ = endpoint_type().protocol(); Chris@16: } Chris@16: Chris@16: // Move-assign from another socket implementation. Chris@16: void move_assign(implementation_type& impl, Chris@16: reactive_socket_service_base& other_service, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: this->base_move_assign(impl, other_service, other_impl); Chris@16: Chris@16: impl.protocol_ = other_impl.protocol_; Chris@16: other_impl.protocol_ = endpoint_type().protocol(); Chris@16: } Chris@16: Chris@16: // Move-construct a new socket implementation from another protocol type. Chris@16: template Chris@16: void converting_move_construct(implementation_type& impl, Chris@16: typename reactive_socket_service< Chris@16: Protocol1>::implementation_type& other_impl) Chris@16: { Chris@16: this->base_move_construct(impl, other_impl); Chris@16: Chris@16: impl.protocol_ = protocol_type(other_impl.protocol_); Chris@16: other_impl.protocol_ = typename Protocol1::endpoint().protocol(); Chris@16: } Chris@16: Chris@16: // Open a new socket implementation. Chris@16: boost::system::error_code open(implementation_type& impl, Chris@16: const protocol_type& protocol, boost::system::error_code& ec) Chris@16: { Chris@16: if (!do_open(impl, protocol.family(), Chris@16: protocol.type(), protocol.protocol(), ec)) Chris@16: impl.protocol_ = protocol; Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Assign a native socket to a socket implementation. Chris@16: boost::system::error_code assign(implementation_type& impl, Chris@16: const protocol_type& protocol, const native_handle_type& native_socket, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: if (!do_assign(impl, protocol.type(), native_socket, ec)) Chris@16: impl.protocol_ = protocol; Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Get the native socket representation. Chris@16: native_handle_type native_handle(implementation_type& impl) Chris@16: { Chris@16: return impl.socket_; Chris@16: } Chris@16: Chris@16: // Bind the socket to the specified local endpoint. Chris@16: boost::system::error_code bind(implementation_type& impl, Chris@16: const endpoint_type& endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Set a socket option. Chris@16: template Chris@16: boost::system::error_code set_option(implementation_type& impl, Chris@16: const Option& option, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::setsockopt(impl.socket_, impl.state_, Chris@16: option.level(impl.protocol_), option.name(impl.protocol_), Chris@16: option.data(impl.protocol_), option.size(impl.protocol_), ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Set a socket option. Chris@16: template Chris@16: boost::system::error_code get_option(const implementation_type& impl, Chris@16: Option& option, boost::system::error_code& ec) const Chris@16: { Chris@16: std::size_t size = option.size(impl.protocol_); Chris@16: socket_ops::getsockopt(impl.socket_, impl.state_, Chris@16: option.level(impl.protocol_), option.name(impl.protocol_), Chris@16: option.data(impl.protocol_), &size, ec); Chris@16: if (!ec) Chris@16: option.resize(impl.protocol_, size); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Get the local endpoint. Chris@16: endpoint_type local_endpoint(const implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: endpoint_type endpoint; Chris@16: std::size_t addr_len = endpoint.capacity(); Chris@16: if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec)) Chris@16: return endpoint_type(); Chris@16: endpoint.resize(addr_len); Chris@16: return endpoint; Chris@16: } Chris@16: Chris@16: // Get the remote endpoint. Chris@16: endpoint_type remote_endpoint(const implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: endpoint_type endpoint; Chris@16: std::size_t addr_len = endpoint.capacity(); Chris@16: if (socket_ops::getpeername(impl.socket_, Chris@16: endpoint.data(), &addr_len, false, ec)) Chris@16: return endpoint_type(); Chris@16: endpoint.resize(addr_len); Chris@16: return endpoint; Chris@16: } Chris@16: Chris@16: // Send a datagram to the specified endpoint. Returns the number of bytes Chris@16: // sent. Chris@16: template Chris@16: size_t send_to(implementation_type& impl, 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: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: return socket_ops::sync_sendto(impl.socket_, impl.state_, Chris@16: bufs.buffers(), bufs.count(), flags, Chris@16: destination.data(), destination.size(), ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be sent without blocking. Chris@16: size_t send_to(implementation_type& impl, const null_buffers&, Chris@16: const endpoint_type&, socket_base::message_flags, Chris@16: 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_to(implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: const endpoint_type& destination, socket_base::message_flags flags, Chris@16: 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_sendto_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, destination, flags, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to")); Chris@16: Chris@16: start_op(impl, reactor::write_op, p.p, is_continuation, true, false); 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_to(implementation_type& impl, const null_buffers&, Chris@16: const endpoint_type&, 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_to(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 a datagram with the endpoint of the sender. Returns the number of Chris@16: // bytes received. Chris@16: template Chris@16: size_t receive_from(implementation_type& impl, Chris@16: 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: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: std::size_t addr_len = sender_endpoint.capacity(); Chris@16: std::size_t bytes_recvd = socket_ops::sync_recvfrom( Chris@16: impl.socket_, impl.state_, bufs.buffers(), bufs.count(), Chris@16: flags, sender_endpoint.data(), &addr_len, ec); Chris@16: Chris@16: if (!ec) Chris@16: sender_endpoint.resize(addr_len); Chris@16: Chris@16: return bytes_recvd; Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: size_t receive_from(implementation_type& impl, const null_buffers&, Chris@16: endpoint_type& sender_endpoint, socket_base::message_flags, Chris@16: 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: // Reset endpoint since it can be given no sensible value at this time. Chris@16: sender_endpoint = endpoint_type(); Chris@16: Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // Start an asynchronous receive. The buffer for the data being received and Chris@16: // the sender_endpoint object must both be valid for the lifetime of the Chris@16: // asynchronous operation. Chris@16: template Chris@16: void async_receive_from(implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, endpoint_type& sender_endpoint, 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_recvfrom_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: int protocol = impl.protocol_.type(); Chris@16: p.p = new (p.v) op(impl.socket_, protocol, Chris@16: buffers, sender_endpoint, flags, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", Chris@16: &impl, "async_receive_from")); 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, true, 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_from(implementation_type& impl, Chris@16: const null_buffers&, endpoint_type& sender_endpoint, 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_from(null_buffers)")); Chris@16: Chris@16: // Reset endpoint since it can be given no sensible value at this time. Chris@16: sender_endpoint = endpoint_type(); 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: // Accept a new connection. Chris@16: template Chris@16: boost::system::error_code accept(implementation_type& impl, Chris@16: Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: // We cannot accept a socket that is already open. Chris@16: if (peer.is_open()) Chris@16: { Chris@16: ec = boost::asio::error::already_open; Chris@16: return ec; Chris@16: } Chris@16: Chris@16: std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0; Chris@16: socket_holder new_socket(socket_ops::sync_accept(impl.socket_, Chris@16: impl.state_, peer_endpoint ? peer_endpoint->data() : 0, Chris@16: peer_endpoint ? &addr_len : 0, ec)); Chris@16: Chris@16: // On success, assign new connection to peer socket object. Chris@16: if (new_socket.get() != invalid_socket) Chris@16: { Chris@16: if (peer_endpoint) Chris@16: peer_endpoint->resize(addr_len); Chris@16: if (!peer.assign(impl.protocol_, new_socket.get(), ec)) Chris@16: new_socket.release(); Chris@16: } Chris@16: Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Start an asynchronous accept. The peer and peer_endpoint objects Chris@16: // must be valid until the accept's handler is invoked. Chris@16: template Chris@16: void async_accept(implementation_type& impl, Socket& peer, Chris@16: endpoint_type* peer_endpoint, 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_accept_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_, peer, Chris@16: impl.protocol_, peer_endpoint, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept")); Chris@16: Chris@16: start_accept_op(impl, p.p, is_continuation, peer.is_open()); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: // Connect the socket to the specified endpoint. Chris@16: boost::system::error_code connect(implementation_type& impl, Chris@16: const endpoint_type& peer_endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: socket_ops::sync_connect(impl.socket_, Chris@16: peer_endpoint.data(), peer_endpoint.size(), ec); Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Start an asynchronous connect. Chris@16: template Chris@16: void async_connect(implementation_type& impl, Chris@16: const endpoint_type& peer_endpoint, 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_connect_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_, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect")); Chris@16: Chris@16: start_connect_op(impl, p.p, is_continuation, Chris@16: peer_endpoint.data(), peer_endpoint.size()); Chris@16: p.v = p.p = 0; Chris@16: } 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: #endif // !defined(BOOST_ASIO_HAS_IOCP) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP