Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.3KB

  1. //
  2. // detail/winrt_ssocket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS_RUNTIME)
  17. #include "asio/error.hpp"
  18. #include "asio/io_context.hpp"
  19. #include "asio/detail/memory.hpp"
  20. #include "asio/detail/winrt_socket_connect_op.hpp"
  21. #include "asio/detail/winrt_ssocket_service_base.hpp"
  22. #include "asio/detail/winrt_utils.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. template <typename Protocol>
  27. class winrt_ssocket_service :
  28. public winrt_ssocket_service_base
  29. {
  30. public:
  31. // The protocol type.
  32. typedef Protocol protocol_type;
  33. // The endpoint type.
  34. typedef typename Protocol::endpoint endpoint_type;
  35. // The native type of a socket.
  36. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
  37. // The implementation type of the socket.
  38. struct implementation_type : base_implementation_type
  39. {
  40. // Default constructor.
  41. implementation_type()
  42. : base_implementation_type(),
  43. protocol_(endpoint_type().protocol())
  44. {
  45. }
  46. // The protocol associated with the socket.
  47. protocol_type protocol_;
  48. };
  49. // Constructor.
  50. winrt_ssocket_service(asio::io_context& io_context)
  51. : winrt_ssocket_service_base(io_context)
  52. {
  53. }
  54. // Move-construct a new socket implementation.
  55. void move_construct(implementation_type& impl,
  56. implementation_type& other_impl)
  57. {
  58. this->base_move_construct(impl, other_impl);
  59. impl.protocol_ = other_impl.protocol_;
  60. other_impl.protocol_ = endpoint_type().protocol();
  61. }
  62. // Move-assign from another socket implementation.
  63. void move_assign(implementation_type& impl,
  64. winrt_ssocket_service& other_service,
  65. implementation_type& other_impl)
  66. {
  67. this->base_move_assign(impl, other_service, other_impl);
  68. impl.protocol_ = other_impl.protocol_;
  69. other_impl.protocol_ = endpoint_type().protocol();
  70. }
  71. // Move-construct a new socket implementation from another protocol type.
  72. template <typename Protocol1>
  73. void converting_move_construct(implementation_type& impl,
  74. typename winrt_ssocket_service<
  75. Protocol1>::implementation_type& other_impl)
  76. {
  77. this->base_move_construct(impl, other_impl);
  78. impl.protocol_ = protocol_type(other_impl.protocol_);
  79. other_impl.protocol_ = typename Protocol1::endpoint().protocol();
  80. }
  81. // Open a new socket implementation.
  82. asio::error_code open(implementation_type& impl,
  83. const protocol_type& protocol, asio::error_code& ec)
  84. {
  85. if (is_open(impl))
  86. {
  87. ec = asio::error::already_open;
  88. return ec;
  89. }
  90. try
  91. {
  92. impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
  93. impl.protocol_ = protocol;
  94. ec = asio::error_code();
  95. }
  96. catch (Platform::Exception^ e)
  97. {
  98. ec = asio::error_code(e->HResult,
  99. asio::system_category());
  100. }
  101. return ec;
  102. }
  103. // Assign a native socket to a socket implementation.
  104. asio::error_code assign(implementation_type& impl,
  105. const protocol_type& protocol, const native_handle_type& native_socket,
  106. asio::error_code& ec)
  107. {
  108. if (is_open(impl))
  109. {
  110. ec = asio::error::already_open;
  111. return ec;
  112. }
  113. impl.socket_ = native_socket;
  114. impl.protocol_ = protocol;
  115. ec = asio::error_code();
  116. return ec;
  117. }
  118. // Bind the socket to the specified local endpoint.
  119. asio::error_code bind(implementation_type&,
  120. const endpoint_type&, asio::error_code& ec)
  121. {
  122. ec = asio::error::operation_not_supported;
  123. return ec;
  124. }
  125. // Get the local endpoint.
  126. endpoint_type local_endpoint(const implementation_type& impl,
  127. asio::error_code& ec) const
  128. {
  129. endpoint_type endpoint;
  130. endpoint.resize(do_get_endpoint(impl, true,
  131. endpoint.data(), endpoint.size(), ec));
  132. return endpoint;
  133. }
  134. // Get the remote endpoint.
  135. endpoint_type remote_endpoint(const implementation_type& impl,
  136. asio::error_code& ec) const
  137. {
  138. endpoint_type endpoint;
  139. endpoint.resize(do_get_endpoint(impl, false,
  140. endpoint.data(), endpoint.size(), ec));
  141. return endpoint;
  142. }
  143. // Set a socket option.
  144. template <typename Option>
  145. asio::error_code set_option(implementation_type& impl,
  146. const Option& option, asio::error_code& ec)
  147. {
  148. return do_set_option(impl, option.level(impl.protocol_),
  149. option.name(impl.protocol_), option.data(impl.protocol_),
  150. option.size(impl.protocol_), ec);
  151. }
  152. // Get a socket option.
  153. template <typename Option>
  154. asio::error_code get_option(const implementation_type& impl,
  155. Option& option, asio::error_code& ec) const
  156. {
  157. std::size_t size = option.size(impl.protocol_);
  158. do_get_option(impl, option.level(impl.protocol_),
  159. option.name(impl.protocol_),
  160. option.data(impl.protocol_), &size, ec);
  161. if (!ec)
  162. option.resize(impl.protocol_, size);
  163. return ec;
  164. }
  165. // Connect the socket to the specified endpoint.
  166. asio::error_code connect(implementation_type& impl,
  167. const endpoint_type& peer_endpoint, asio::error_code& ec)
  168. {
  169. return do_connect(impl, peer_endpoint.data(), ec);
  170. }
  171. // Start an asynchronous connect.
  172. template <typename Handler>
  173. void async_connect(implementation_type& impl,
  174. const endpoint_type& peer_endpoint, Handler& handler)
  175. {
  176. bool is_continuation =
  177. asio_handler_cont_helpers::is_continuation(handler);
  178. // Allocate and construct an operation to wrap the handler.
  179. typedef winrt_socket_connect_op<Handler> op;
  180. typename op::ptr p = { asio::detail::addressof(handler),
  181. op::ptr::allocate(handler), 0 };
  182. p.p = new (p.v) op(handler);
  183. ASIO_HANDLER_CREATION((io_context_.context(),
  184. *p.p, "socket", &impl, 0, "async_connect"));
  185. start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
  186. p.v = p.p = 0;
  187. }
  188. };
  189. } // namespace detail
  190. } // namespace asio
  191. #include "asio/detail/pop_options.hpp"
  192. #endif // defined(ASIO_WINDOWS_RUNTIME)
  193. #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP