winrt_ssocket_service.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // detail/winrt_ssocket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <boost/asio/error.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <boost/asio/detail/memory.hpp>
  20. #include <boost/asio/detail/winrt_socket_connect_op.hpp>
  21. #include <boost/asio/detail/winrt_ssocket_service_base.hpp>
  22. #include <boost/asio/detail/winrt_utils.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. template <typename Protocol>
  28. class winrt_ssocket_service :
  29. public execution_context_service_base<winrt_ssocket_service<Protocol> >,
  30. public winrt_ssocket_service_base
  31. {
  32. public:
  33. // The protocol type.
  34. typedef Protocol protocol_type;
  35. // The endpoint type.
  36. typedef typename Protocol::endpoint endpoint_type;
  37. // The native type of a socket.
  38. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
  39. // The implementation type of the socket.
  40. struct implementation_type : base_implementation_type
  41. {
  42. // Default constructor.
  43. implementation_type()
  44. : base_implementation_type(),
  45. protocol_(endpoint_type().protocol())
  46. {
  47. }
  48. // The protocol associated with the socket.
  49. protocol_type protocol_;
  50. };
  51. // Constructor.
  52. winrt_ssocket_service(execution_context& context)
  53. : execution_context_service_base<winrt_ssocket_service<Protocol> >(context),
  54. winrt_ssocket_service_base(context)
  55. {
  56. }
  57. // Destroy all user-defined handler objects owned by the service.
  58. void shutdown()
  59. {
  60. this->base_shutdown();
  61. }
  62. // Move-construct a new socket implementation.
  63. void move_construct(implementation_type& impl,
  64. implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
  65. {
  66. this->base_move_construct(impl, other_impl);
  67. impl.protocol_ = other_impl.protocol_;
  68. other_impl.protocol_ = endpoint_type().protocol();
  69. }
  70. // Move-assign from another socket implementation.
  71. void move_assign(implementation_type& impl,
  72. winrt_ssocket_service& other_service,
  73. implementation_type& other_impl)
  74. {
  75. this->base_move_assign(impl, other_service, other_impl);
  76. impl.protocol_ = other_impl.protocol_;
  77. other_impl.protocol_ = endpoint_type().protocol();
  78. }
  79. // Move-construct a new socket implementation from another protocol type.
  80. template <typename Protocol1>
  81. void converting_move_construct(implementation_type& impl,
  82. winrt_ssocket_service<Protocol1>&,
  83. typename winrt_ssocket_service<
  84. Protocol1>::implementation_type& other_impl)
  85. {
  86. this->base_move_construct(impl, other_impl);
  87. impl.protocol_ = protocol_type(other_impl.protocol_);
  88. other_impl.protocol_ = typename Protocol1::endpoint().protocol();
  89. }
  90. // Open a new socket implementation.
  91. boost::system::error_code open(implementation_type& impl,
  92. const protocol_type& protocol, boost::system::error_code& ec)
  93. {
  94. if (is_open(impl))
  95. {
  96. ec = boost::asio::error::already_open;
  97. return ec;
  98. }
  99. try
  100. {
  101. impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
  102. impl.protocol_ = protocol;
  103. ec = boost::system::error_code();
  104. }
  105. catch (Platform::Exception^ e)
  106. {
  107. ec = boost::system::error_code(e->HResult,
  108. boost::system::system_category());
  109. }
  110. return ec;
  111. }
  112. // Assign a native socket to a socket implementation.
  113. boost::system::error_code assign(implementation_type& impl,
  114. const protocol_type& protocol, const native_handle_type& native_socket,
  115. boost::system::error_code& ec)
  116. {
  117. if (is_open(impl))
  118. {
  119. ec = boost::asio::error::already_open;
  120. return ec;
  121. }
  122. impl.socket_ = native_socket;
  123. impl.protocol_ = protocol;
  124. ec = boost::system::error_code();
  125. return ec;
  126. }
  127. // Bind the socket to the specified local endpoint.
  128. boost::system::error_code bind(implementation_type&,
  129. const endpoint_type&, boost::system::error_code& ec)
  130. {
  131. ec = boost::asio::error::operation_not_supported;
  132. return ec;
  133. }
  134. // Get the local endpoint.
  135. endpoint_type local_endpoint(const implementation_type& impl,
  136. boost::system::error_code& ec) const
  137. {
  138. endpoint_type endpoint;
  139. endpoint.resize(do_get_endpoint(impl, true,
  140. endpoint.data(), endpoint.size(), ec));
  141. return endpoint;
  142. }
  143. // Get the remote endpoint.
  144. endpoint_type remote_endpoint(const implementation_type& impl,
  145. boost::system::error_code& ec) const
  146. {
  147. endpoint_type endpoint;
  148. endpoint.resize(do_get_endpoint(impl, false,
  149. endpoint.data(), endpoint.size(), ec));
  150. return endpoint;
  151. }
  152. // Disable sends or receives on the socket.
  153. boost::system::error_code shutdown(implementation_type&,
  154. socket_base::shutdown_type, boost::system::error_code& ec)
  155. {
  156. ec = boost::asio::error::operation_not_supported;
  157. return ec;
  158. }
  159. // Set a socket option.
  160. template <typename Option>
  161. boost::system::error_code set_option(implementation_type& impl,
  162. const Option& option, boost::system::error_code& ec)
  163. {
  164. return do_set_option(impl, option.level(impl.protocol_),
  165. option.name(impl.protocol_), option.data(impl.protocol_),
  166. option.size(impl.protocol_), ec);
  167. }
  168. // Get a socket option.
  169. template <typename Option>
  170. boost::system::error_code get_option(const implementation_type& impl,
  171. Option& option, boost::system::error_code& ec) const
  172. {
  173. std::size_t size = option.size(impl.protocol_);
  174. do_get_option(impl, option.level(impl.protocol_),
  175. option.name(impl.protocol_),
  176. option.data(impl.protocol_), &size, ec);
  177. if (!ec)
  178. option.resize(impl.protocol_, size);
  179. return ec;
  180. }
  181. // Connect the socket to the specified endpoint.
  182. boost::system::error_code connect(implementation_type& impl,
  183. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  184. {
  185. return do_connect(impl, peer_endpoint.data(), ec);
  186. }
  187. // Start an asynchronous connect.
  188. template <typename Handler, typename IoExecutor>
  189. void async_connect(implementation_type& impl,
  190. const endpoint_type& peer_endpoint,
  191. Handler& handler, const IoExecutor& io_ex)
  192. {
  193. bool is_continuation =
  194. boost_asio_handler_cont_helpers::is_continuation(handler);
  195. // Allocate and construct an operation to wrap the handler.
  196. typedef winrt_socket_connect_op<Handler, IoExecutor> op;
  197. typename op::ptr p = { boost::asio::detail::addressof(handler),
  198. op::ptr::allocate(handler), 0 };
  199. p.p = new (p.v) op(handler, io_ex);
  200. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  201. *p.p, "socket", &impl, 0, "async_connect"));
  202. start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
  203. p.v = p.p = 0;
  204. }
  205. };
  206. } // namespace detail
  207. } // namespace asio
  208. } // namespace boost
  209. #include <boost/asio/detail/pop_options.hpp>
  210. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  211. #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP