Chris@16: // Chris@16: // stream_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_STREAM_SOCKET_SERVICE_HPP Chris@16: #define BOOST_ASIO_STREAM_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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: # include Chris@16: #elif defined(BOOST_ASIO_HAS_IOCP) Chris@16: # include Chris@16: #else Chris@16: # include Chris@16: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Default service implementation for a stream socket. Chris@16: template Chris@16: class stream_socket_service Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: : public boost::asio::io_service::service Chris@16: #else Chris@16: : public boost::asio::detail::service_base > Chris@16: #endif Chris@16: { Chris@16: public: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// The unique service identifier. Chris@16: static boost::asio::io_service::id id; Chris@16: #endif 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: private: Chris@16: // The type of the platform-specific implementation. Chris@16: #if defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: typedef detail::winrt_ssocket_service service_impl_type; Chris@16: #elif defined(BOOST_ASIO_HAS_IOCP) Chris@16: typedef detail::win_iocp_socket_service service_impl_type; Chris@16: #else Chris@16: typedef detail::reactive_socket_service service_impl_type; Chris@16: #endif Chris@16: Chris@16: public: Chris@16: /// The type of a stream socket implementation. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined implementation_type; Chris@16: #else Chris@16: typedef typename service_impl_type::implementation_type implementation_type; Chris@16: #endif Chris@16: Chris@16: /// (Deprecated: Use native_handle_type.) The native socket type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_type; Chris@16: #else Chris@16: typedef typename service_impl_type::native_handle_type native_type; Chris@16: #endif Chris@16: Chris@16: /// The native socket type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_handle_type; Chris@16: #else Chris@16: typedef typename service_impl_type::native_handle_type native_handle_type; Chris@16: #endif Chris@16: Chris@16: /// Construct a new stream socket service for the specified io_service. Chris@16: explicit stream_socket_service(boost::asio::io_service& io_service) Chris@16: : boost::asio::detail::service_base< Chris@16: stream_socket_service >(io_service), Chris@16: service_impl_(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a new stream socket implementation. Chris@16: void construct(implementation_type& impl) Chris@16: { Chris@16: service_impl_.construct(impl); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a new stream socket implementation. Chris@16: void move_construct(implementation_type& impl, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_construct(impl, other_impl); Chris@16: } Chris@16: Chris@16: /// Move-assign from another stream socket implementation. Chris@16: void move_assign(implementation_type& impl, Chris@16: stream_socket_service& other_service, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_assign(impl, other_service.service_impl_, other_impl); Chris@16: } Chris@16: Chris@16: /// Move-construct a new stream socket implementation from another protocol Chris@16: /// type. Chris@16: template Chris@16: void converting_move_construct(implementation_type& impl, Chris@16: typename stream_socket_service< Chris@16: Protocol1>::implementation_type& other_impl, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: service_impl_.template converting_move_construct( Chris@16: impl, other_impl); Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Destroy a stream socket implementation. Chris@16: void destroy(implementation_type& impl) Chris@16: { Chris@16: service_impl_.destroy(impl); Chris@16: } Chris@16: Chris@16: /// Open a stream socket. 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 (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_STREAM)) Chris@16: service_impl_.open(impl, protocol, ec); Chris@16: else Chris@16: ec = boost::asio::error::invalid_argument; Chris@16: return ec; Chris@16: } Chris@16: Chris@16: /// Assign an existing native socket to a stream socket. 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: return service_impl_.assign(impl, protocol, native_socket, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the socket is open. Chris@16: bool is_open(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.is_open(impl); Chris@16: } Chris@16: Chris@16: /// Close a stream socket implementation. Chris@16: boost::system::error_code close(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.close(impl, ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native socket implementation. Chris@16: native_type native(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Get the native socket implementation. Chris@16: native_handle_type native_handle(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the socket. Chris@16: boost::system::error_code cancel(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.cancel(impl, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the socket is at the out-of-band data mark. Chris@16: bool at_mark(const implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.at_mark(impl, ec); Chris@16: } Chris@16: Chris@16: /// Determine the number of bytes available for reading. Chris@16: std::size_t available(const implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.available(impl, ec); Chris@16: } Chris@16: Chris@16: /// Bind the stream 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: return service_impl_.bind(impl, endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Connect the stream 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: return service_impl_.connect(impl, peer_endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous connect. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler, Chris@16: void (boost::system::error_code)) Chris@16: async_connect(implementation_type& impl, Chris@16: const endpoint_type& peer_endpoint, Chris@16: BOOST_ASIO_MOVE_ARG(ConnectHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: ConnectHandler, void (boost::system::error_code)> init( Chris@16: BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler)); Chris@16: Chris@16: service_impl_.async_connect(impl, peer_endpoint, init.handler); Chris@16: Chris@16: return init.result.get(); 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 SettableSocketOption& option, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.set_option(impl, option, ec); Chris@16: } Chris@16: Chris@16: /// Get a socket option. Chris@16: template Chris@16: boost::system::error_code get_option(const implementation_type& impl, Chris@16: GettableSocketOption& option, boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.get_option(impl, option, 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(implementation_type& impl, Chris@16: IoControlCommand& command, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.io_control(impl, command, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the socket. Chris@16: bool non_blocking(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.non_blocking(impl); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the socket. Chris@16: boost::system::error_code non_blocking(implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.non_blocking(impl, mode, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the native socket implementation. Chris@16: bool native_non_blocking(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.native_non_blocking(impl); 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(implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.native_non_blocking(impl, mode, 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: return service_impl_.local_endpoint(impl, ec); 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: return service_impl_.remote_endpoint(impl, ec); Chris@16: } Chris@16: Chris@16: /// Disable sends or receives on the socket. Chris@16: boost::system::error_code shutdown(implementation_type& impl, Chris@16: socket_base::shutdown_type what, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.shutdown(impl, what, ec); Chris@16: } Chris@16: Chris@16: /// Send the given data to the peer. Chris@16: template Chris@16: std::size_t send(implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.send(impl, buffers, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous send. 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(implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: WriteHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: Chris@16: service_impl_.async_send(impl, buffers, flags, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: /// Receive some data from the peer. Chris@16: template Chris@16: std::size_t receive(implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.receive(impl, buffers, flags, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous receive. 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(implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: socket_base::message_flags flags, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: ReadHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: Chris@16: service_impl_.async_receive(impl, buffers, flags, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: private: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: void shutdown_service() Chris@16: { Chris@16: service_impl_.shutdown_service(); Chris@16: } Chris@16: Chris@16: // The platform-specific implementation. Chris@16: service_impl_type service_impl_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP