winrt_ssocket_service.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // detail/winrt_ssocket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/io_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 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(boost::asio::io_context& io_context)
  53. : service_base<winrt_ssocket_service<Protocol> >(io_context),
  54. winrt_ssocket_service_base(io_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)
  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. // Set a socket option.
  153. template <typename Option>
  154. boost::system::error_code set_option(implementation_type& impl,
  155. const Option& option, boost::system::error_code& ec)
  156. {
  157. return do_set_option(impl, option.level(impl.protocol_),
  158. option.name(impl.protocol_), option.data(impl.protocol_),
  159. option.size(impl.protocol_), ec);
  160. }
  161. // Get a socket option.
  162. template <typename Option>
  163. boost::system::error_code get_option(const implementation_type& impl,
  164. Option& option, boost::system::error_code& ec) const
  165. {
  166. std::size_t size = option.size(impl.protocol_);
  167. do_get_option(impl, option.level(impl.protocol_),
  168. option.name(impl.protocol_),
  169. option.data(impl.protocol_), &size, ec);
  170. if (!ec)
  171. option.resize(impl.protocol_, size);
  172. return ec;
  173. }
  174. // Connect the socket to the specified endpoint.
  175. boost::system::error_code connect(implementation_type& impl,
  176. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  177. {
  178. return do_connect(impl, peer_endpoint.data(), ec);
  179. }
  180. // Start an asynchronous connect.
  181. template <typename Handler>
  182. void async_connect(implementation_type& impl,
  183. const endpoint_type& peer_endpoint, Handler& handler)
  184. {
  185. bool is_continuation =
  186. boost_asio_handler_cont_helpers::is_continuation(handler);
  187. // Allocate and construct an operation to wrap the handler.
  188. typedef winrt_socket_connect_op<Handler> op;
  189. typename op::ptr p = { boost::asio::detail::addressof(handler),
  190. op::ptr::allocate(handler), 0 };
  191. p.p = new (p.v) op(handler);
  192. BOOST_ASIO_HANDLER_CREATION((io_context_.context(),
  193. *p.p, "socket", &impl, 0, "async_connect"));
  194. start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
  195. p.v = p.p = 0;
  196. }
  197. };
  198. } // namespace detail
  199. } // namespace asio
  200. } // namespace boost
  201. #include <boost/asio/detail/pop_options.hpp>
  202. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  203. #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP