Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:47

0001 //
0002 // detail/winrt_ssocket_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 
0022 #include <boost/asio/error.hpp>
0023 #include <boost/asio/execution_context.hpp>
0024 #include <boost/asio/detail/memory.hpp>
0025 #include <boost/asio/detail/winrt_socket_connect_op.hpp>
0026 #include <boost/asio/detail/winrt_ssocket_service_base.hpp>
0027 #include <boost/asio/detail/winrt_utils.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace detail {
0034 
0035 template <typename Protocol>
0036 class winrt_ssocket_service :
0037   public execution_context_service_base<winrt_ssocket_service<Protocol>>,
0038   public winrt_ssocket_service_base
0039 {
0040 public:
0041   // The protocol type.
0042   typedef Protocol protocol_type;
0043 
0044   // The endpoint type.
0045   typedef typename Protocol::endpoint endpoint_type;
0046 
0047   // The native type of a socket.
0048   typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
0049 
0050   // The implementation type of the socket.
0051   struct implementation_type : base_implementation_type
0052   {
0053     // Default constructor.
0054     implementation_type()
0055       : base_implementation_type(),
0056         protocol_(endpoint_type().protocol())
0057     {
0058     }
0059 
0060     // The protocol associated with the socket.
0061     protocol_type protocol_;
0062   };
0063 
0064   // Constructor.
0065   winrt_ssocket_service(execution_context& context)
0066     : execution_context_service_base<winrt_ssocket_service<Protocol>>(context),
0067       winrt_ssocket_service_base(context)
0068   {
0069   }
0070 
0071   // Destroy all user-defined handler objects owned by the service.
0072   void shutdown()
0073   {
0074     this->base_shutdown();
0075   }
0076 
0077   // Move-construct a new socket implementation.
0078   void move_construct(implementation_type& impl,
0079       implementation_type& other_impl) noexcept
0080   {
0081     this->base_move_construct(impl, other_impl);
0082 
0083     impl.protocol_ = other_impl.protocol_;
0084     other_impl.protocol_ = endpoint_type().protocol();
0085   }
0086 
0087   // Move-assign from another socket implementation.
0088   void move_assign(implementation_type& impl,
0089       winrt_ssocket_service& other_service,
0090       implementation_type& other_impl)
0091   {
0092     this->base_move_assign(impl, other_service, other_impl);
0093 
0094     impl.protocol_ = other_impl.protocol_;
0095     other_impl.protocol_ = endpoint_type().protocol();
0096   }
0097 
0098   // Move-construct a new socket implementation from another protocol type.
0099   template <typename Protocol1>
0100   void converting_move_construct(implementation_type& impl,
0101       winrt_ssocket_service<Protocol1>&,
0102       typename winrt_ssocket_service<
0103         Protocol1>::implementation_type& other_impl)
0104   {
0105     this->base_move_construct(impl, other_impl);
0106 
0107     impl.protocol_ = protocol_type(other_impl.protocol_);
0108     other_impl.protocol_ = typename Protocol1::endpoint().protocol();
0109   }
0110 
0111   // Open a new socket implementation.
0112   boost::system::error_code open(implementation_type& impl,
0113       const protocol_type& protocol, boost::system::error_code& ec)
0114   {
0115     if (is_open(impl))
0116     {
0117       ec = boost::asio::error::already_open;
0118       return ec;
0119     }
0120 
0121     try
0122     {
0123       impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
0124       impl.protocol_ = protocol;
0125       ec = boost::system::error_code();
0126     }
0127     catch (Platform::Exception^ e)
0128     {
0129       ec = boost::system::error_code(e->HResult,
0130             boost::system::system_category());
0131     }
0132 
0133     return ec;
0134   }
0135 
0136   // Assign a native socket to a socket implementation.
0137   boost::system::error_code assign(implementation_type& impl,
0138       const protocol_type& protocol, const native_handle_type& native_socket,
0139       boost::system::error_code& ec)
0140   {
0141     if (is_open(impl))
0142     {
0143       ec = boost::asio::error::already_open;
0144       return ec;
0145     }
0146 
0147     impl.socket_ = native_socket;
0148     impl.protocol_ = protocol;
0149     ec = boost::system::error_code();
0150 
0151     return ec;
0152   }
0153 
0154   // Bind the socket to the specified local endpoint.
0155   boost::system::error_code bind(implementation_type&,
0156       const endpoint_type&, boost::system::error_code& ec)
0157   {
0158     ec = boost::asio::error::operation_not_supported;
0159     return ec;
0160   }
0161 
0162   // Get the local endpoint.
0163   endpoint_type local_endpoint(const implementation_type& impl,
0164       boost::system::error_code& ec) const
0165   {
0166     endpoint_type endpoint;
0167     endpoint.resize(do_get_endpoint(impl, true,
0168           endpoint.data(), endpoint.size(), ec));
0169     return endpoint;
0170   }
0171 
0172   // Get the remote endpoint.
0173   endpoint_type remote_endpoint(const implementation_type& impl,
0174       boost::system::error_code& ec) const
0175   {
0176     endpoint_type endpoint;
0177     endpoint.resize(do_get_endpoint(impl, false,
0178           endpoint.data(), endpoint.size(), ec));
0179     return endpoint;
0180   }
0181 
0182   // Disable sends or receives on the socket.
0183   boost::system::error_code shutdown(implementation_type&,
0184       socket_base::shutdown_type, boost::system::error_code& ec)
0185   {
0186     ec = boost::asio::error::operation_not_supported;
0187     return ec;
0188   }
0189 
0190   // Set a socket option.
0191   template <typename Option>
0192   boost::system::error_code set_option(implementation_type& impl,
0193       const Option& option, boost::system::error_code& ec)
0194   {
0195     return do_set_option(impl, option.level(impl.protocol_),
0196         option.name(impl.protocol_), option.data(impl.protocol_),
0197         option.size(impl.protocol_), ec);
0198   }
0199 
0200   // Get a socket option.
0201   template <typename Option>
0202   boost::system::error_code get_option(const implementation_type& impl,
0203       Option& option, boost::system::error_code& ec) const
0204   {
0205     std::size_t size = option.size(impl.protocol_);
0206     do_get_option(impl, option.level(impl.protocol_),
0207         option.name(impl.protocol_),
0208         option.data(impl.protocol_), &size, ec);
0209     if (!ec)
0210       option.resize(impl.protocol_, size);
0211     return ec;
0212   }
0213 
0214   // Connect the socket to the specified endpoint.
0215   boost::system::error_code connect(implementation_type& impl,
0216       const endpoint_type& peer_endpoint, boost::system::error_code& ec)
0217   {
0218     return do_connect(impl, peer_endpoint.data(), ec);
0219   }
0220 
0221   // Start an asynchronous connect.
0222   template <typename Handler, typename IoExecutor>
0223   void async_connect(implementation_type& impl,
0224       const endpoint_type& peer_endpoint,
0225       Handler& handler, const IoExecutor& io_ex)
0226   {
0227     bool is_continuation =
0228       boost_asio_handler_cont_helpers::is_continuation(handler);
0229 
0230     // Allocate and construct an operation to wrap the handler.
0231     typedef winrt_socket_connect_op<Handler, IoExecutor> op;
0232     typename op::ptr p = { boost::asio::detail::addressof(handler),
0233       op::ptr::allocate(handler), 0 };
0234     p.p = new (p.v) op(handler, io_ex);
0235 
0236     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
0237           *p.p, "socket", &impl, 0, "async_connect"));
0238 
0239     start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
0240     p.v = p.p = 0;
0241   }
0242 };
0243 
0244 } // namespace detail
0245 } // namespace asio
0246 } // namespace boost
0247 
0248 #include <boost/asio/detail/pop_options.hpp>
0249 
0250 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0251 
0252 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP