Chris@16: // Chris@16: // detail/win_iocp_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_WIN_IOCP_SOCKET_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_WIN_IOCP_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: #include Chris@16: #include Chris@101: #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 win_iocp_socket_service : public win_iocp_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: class native_handle_type Chris@16: { Chris@16: public: Chris@16: native_handle_type(socket_type s) Chris@16: : socket_(s), Chris@16: have_remote_endpoint_(false) Chris@16: { Chris@16: } Chris@16: Chris@16: native_handle_type(socket_type s, const endpoint_type& ep) Chris@16: : socket_(s), Chris@16: have_remote_endpoint_(true), Chris@16: remote_endpoint_(ep) Chris@16: { Chris@16: } Chris@16: Chris@16: void operator=(socket_type s) Chris@16: { Chris@16: socket_ = s; Chris@16: have_remote_endpoint_ = false; Chris@16: remote_endpoint_ = endpoint_type(); Chris@16: } Chris@16: Chris@16: operator socket_type() const Chris@16: { Chris@16: return socket_; Chris@16: } Chris@16: Chris@16: bool have_remote_endpoint() const Chris@16: { Chris@16: return have_remote_endpoint_; Chris@16: } Chris@16: Chris@16: endpoint_type remote_endpoint() const Chris@16: { Chris@16: return remote_endpoint_; Chris@16: } Chris@16: Chris@16: private: Chris@16: socket_type socket_; Chris@16: bool have_remote_endpoint_; Chris@16: endpoint_type remote_endpoint_; Chris@16: }; Chris@16: Chris@16: // The implementation type of the socket. Chris@16: struct implementation_type : Chris@16: win_iocp_socket_service_base::base_implementation_type Chris@16: { Chris@16: // Default constructor. Chris@16: implementation_type() Chris@16: : protocol_(endpoint_type().protocol()), Chris@16: have_remote_endpoint_(false), Chris@16: remote_endpoint_() Chris@16: { Chris@16: } Chris@16: Chris@16: // The protocol associated with the socket. Chris@16: protocol_type protocol_; Chris@16: Chris@16: // Whether we have a cached remote endpoint. Chris@16: bool have_remote_endpoint_; Chris@16: Chris@16: // A cached remote endpoint. Chris@16: endpoint_type remote_endpoint_; Chris@16: }; Chris@16: Chris@16: // Constructor. Chris@16: win_iocp_socket_service(boost::asio::io_service& io_service) Chris@16: : win_iocp_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: impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; Chris@16: other_impl.have_remote_endpoint_ = false; Chris@16: Chris@16: impl.remote_endpoint_ = other_impl.remote_endpoint_; Chris@16: other_impl.remote_endpoint_ = endpoint_type(); Chris@16: } Chris@16: Chris@16: // Move-assign from another socket implementation. Chris@16: void move_assign(implementation_type& impl, Chris@16: win_iocp_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: impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; Chris@16: other_impl.have_remote_endpoint_ = false; Chris@16: Chris@16: impl.remote_endpoint_ = other_impl.remote_endpoint_; Chris@16: other_impl.remote_endpoint_ = endpoint_type(); 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 win_iocp_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: impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; Chris@16: other_impl.have_remote_endpoint_ = false; Chris@16: Chris@16: impl.remote_endpoint_ = other_impl.remote_endpoint_; Chris@16: other_impl.remote_endpoint_ = typename Protocol1::endpoint(); 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: { Chris@16: impl.protocol_ = protocol; Chris@16: impl.have_remote_endpoint_ = false; Chris@16: impl.remote_endpoint_ = endpoint_type(); Chris@16: } 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: { Chris@16: impl.protocol_ = protocol; Chris@16: impl.have_remote_endpoint_ = native_socket.have_remote_endpoint(); Chris@16: impl.remote_endpoint_ = native_socket.remote_endpoint(); Chris@16: } 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: if (impl.have_remote_endpoint_) Chris@16: return native_handle_type(impl.socket_, impl.remote_endpoint_); Chris@16: return native_handle_type(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 = impl.remote_endpoint_; Chris@16: std::size_t addr_len = endpoint.capacity(); Chris@16: if (socket_ops::getpeername(impl.socket_, endpoint.data(), Chris@16: &addr_len, impl.have_remote_endpoint_, 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, const endpoint_type& destination, Chris@16: socket_base::message_flags flags, Handler& handler) Chris@16: { Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef win_iocp_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.cancel_token_, buffers, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to")); Chris@16: Chris@16: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: start_send_to_op(impl, bufs.buffers(), bufs.count(), Chris@16: destination.data(), static_cast(destination.size()), Chris@16: flags, p.p); 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: // Allocate and construct an operation to wrap the handler. Chris@16: typedef win_iocp_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(impl.cancel_token_, 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_reactor_op(impl, reactor::write_op, p.p); 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, Chris@16: const null_buffers&, endpoint_type& sender_endpoint, 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: // 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_endp, Chris@16: socket_base::message_flags flags, Handler& handler) Chris@16: { Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef win_iocp_socket_recvfrom_op< Chris@16: MutableBufferSequence, endpoint_type, Handler> 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(sender_endp, impl.cancel_token_, buffers, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive_from")); Chris@16: Chris@16: buffer_sequence_adapter bufs(buffers); Chris@16: Chris@16: start_receive_from_op(impl, bufs.buffers(), bufs.count(), Chris@16: sender_endp.data(), flags, &p.p->endpoint_size(), p.p); 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: // Allocate and construct an operation to wrap the handler. Chris@16: typedef win_iocp_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(impl.cancel_token_, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, Chris@16: "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_null_buffers_receive_op(impl, flags, p.p); 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, Socket& peer, Chris@16: 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: // Allocate and construct an operation to wrap the handler. Chris@16: typedef win_iocp_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: bool enable_connection_aborted = Chris@16: (impl.state_ & socket_ops::enable_connection_aborted) != 0; Chris@16: p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_, Chris@16: peer_endpoint, enable_connection_aborted, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept")); Chris@16: Chris@16: start_accept_op(impl, peer.is_open(), p.p->new_socket(), Chris@16: impl.protocol_.family(), impl.protocol_.type(), Chris@16: impl.protocol_.protocol(), p.p->output_buffer(), Chris@16: p.p->address_length(), p.p); 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: // Allocate and construct an operation to wrap the handler. Chris@101: typedef win_iocp_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@101: start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(), Chris@101: peer_endpoint.data(), static_cast(peer_endpoint.size()), p.p); 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_WIN_IOCP_SOCKET_SERVICE_HPP