Chris@16: // Chris@16: // detail/winrt_ssocket_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_WINRT_SSOCKET_SERVICE_BASE_HPP Chris@16: #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_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_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: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class winrt_ssocket_service_base Chris@16: { Chris@16: public: Chris@16: // The native type of a socket. Chris@16: typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type; Chris@16: Chris@16: // The implementation type of the socket. Chris@16: struct base_implementation_type Chris@16: { Chris@16: // Default constructor. Chris@16: base_implementation_type() Chris@16: : socket_(nullptr), Chris@16: next_(0), Chris@16: prev_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: // The underlying native socket. Chris@16: native_handle_type socket_; Chris@16: Chris@16: // Pointers to adjacent socket implementations in linked list. Chris@16: base_implementation_type* next_; Chris@16: base_implementation_type* prev_; Chris@16: }; Chris@16: Chris@16: // Constructor. Chris@16: BOOST_ASIO_DECL winrt_ssocket_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&); 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: winrt_ssocket_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_ != nullptr; 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::system::error_code cancel(base_implementation_type&, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; Chris@16: return 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 base_implementation_type&, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; Chris@16: return false; Chris@16: } Chris@16: Chris@16: // Determine the number of bytes available for reading. Chris@16: std::size_t available(const base_implementation_type&, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; Chris@16: return 0; 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&, Chris@16: IO_Control_Command&, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; 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&) const Chris@16: { Chris@16: return false; 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&, Chris@16: bool, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; 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&) const Chris@16: { Chris@16: return false; 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&, Chris@16: bool, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; 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&, Chris@16: socket_base::shutdown_type, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; Chris@16: return ec; Chris@16: } Chris@16: Chris@16: // Send the given data to the peer. Chris@16: template Chris@16: std::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: return do_send(impl, Chris@16: buffer_sequence_adapter::first(buffers), flags, ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be sent without blocking. Chris@16: std::size_t send(base_implementation_type&, const null_buffers&, Chris@16: socket_base::message_flags, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; 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 winrt_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(buffers, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send")); Chris@16: Chris@16: start_send_op(impl, Chris@16: buffer_sequence_adapter::first(buffers), Chris@16: flags, p.p, is_continuation); 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&, const null_buffers&, Chris@16: socket_base::message_flags, Handler& handler) Chris@16: { Chris@16: boost::system::error_code ec = boost::asio::error::operation_not_supported; Chris@16: const std::size_t bytes_transferred = 0; Chris@16: io_service_.get_io_service().post( Chris@16: detail::bind_handler(handler, ec, bytes_transferred)); Chris@16: } Chris@16: Chris@16: // Receive some data from the peer. Returns the number of bytes received. Chris@16: template Chris@16: std::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: return do_receive(impl, Chris@16: buffer_sequence_adapter::first(buffers), flags, ec); Chris@16: } Chris@16: Chris@16: // Wait until data can be received without blocking. Chris@16: std::size_t receive(base_implementation_type&, const null_buffers&, Chris@16: socket_base::message_flags, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; 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 winrt_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(buffers, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive")); Chris@16: Chris@16: start_receive_op(impl, Chris@16: buffer_sequence_adapter::first(buffers), Chris@16: flags, p.p, is_continuation); 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&, const null_buffers&, Chris@16: socket_base::message_flags, Handler& handler) Chris@16: { Chris@16: boost::system::error_code ec = boost::asio::error::operation_not_supported; Chris@16: const std::size_t bytes_transferred = 0; Chris@16: io_service_.get_io_service().post( Chris@16: detail::bind_handler(handler, ec, bytes_transferred)); Chris@16: } Chris@16: Chris@16: protected: Chris@16: // Helper function to obtain endpoints associated with the connection. Chris@16: BOOST_ASIO_DECL std::size_t do_get_endpoint( Chris@16: const base_implementation_type& impl, bool local, Chris@16: void* addr, std::size_t addr_len, boost::system::error_code& ec) const; Chris@16: Chris@16: // Helper function to set a socket option. Chris@16: BOOST_ASIO_DECL boost::system::error_code do_set_option( Chris@16: base_implementation_type& impl, Chris@16: int level, int optname, const void* optval, Chris@16: std::size_t optlen, boost::system::error_code& ec); Chris@16: Chris@16: // Helper function to get a socket option. Chris@16: BOOST_ASIO_DECL void do_get_option( Chris@16: const base_implementation_type& impl, Chris@16: int level, int optname, void* optval, Chris@16: std::size_t* optlen, boost::system::error_code& ec) const; Chris@16: Chris@16: // Helper function to perform a synchronous connect. Chris@16: BOOST_ASIO_DECL boost::system::error_code do_connect( Chris@16: base_implementation_type& impl, Chris@16: const void* addr, boost::system::error_code& ec); Chris@16: Chris@16: // Helper function to start an asynchronous connect. Chris@16: BOOST_ASIO_DECL void start_connect_op( Chris@16: base_implementation_type& impl, const void* addr, Chris@16: winrt_async_op* op, bool is_continuation); Chris@16: Chris@16: // Helper function to perform a synchronous send. Chris@16: BOOST_ASIO_DECL std::size_t do_send( Chris@16: base_implementation_type& impl, const boost::asio::const_buffer& data, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec); Chris@16: Chris@16: // Helper function to start an asynchronous send. Chris@16: BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl, Chris@16: const boost::asio::const_buffer& data, socket_base::message_flags flags, Chris@16: winrt_async_op* op, bool is_continuation); Chris@16: Chris@16: // Helper function to perform a synchronous receive. Chris@16: BOOST_ASIO_DECL std::size_t do_receive( Chris@16: base_implementation_type& impl, const boost::asio::mutable_buffer& data, Chris@16: socket_base::message_flags flags, boost::system::error_code& ec); Chris@16: Chris@16: // Helper function to start an asynchronous receive. Chris@16: BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl, Chris@16: const boost::asio::mutable_buffer& data, socket_base::message_flags flags, Chris@16: winrt_async_op* op, Chris@16: bool is_continuation); Chris@16: Chris@16: // The io_service implementation used for delivering completions. Chris@16: io_service_impl& io_service_; Chris@16: Chris@16: // The manager that keeps track of outstanding operations. Chris@16: winrt_async_manager& async_manager_; Chris@16: Chris@16: // Mutex to protect access to the linked list of implementations. Chris@16: boost::asio::detail::mutex mutex_; Chris@16: Chris@16: // The head of a linked list of all implementations. Chris@16: base_implementation_type* impl_list_; 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_WINDOWS_RUNTIME) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP