winrt_ssocket_service_base.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // detail/winrt_ssocket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_BASE_HPP
  11. #define ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_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/buffer.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/execution_context.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/buffer_sequence_adapter.hpp"
  22. #include "asio/detail/memory.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/detail/winrt_async_manager.hpp"
  25. #include "asio/detail/winrt_socket_recv_op.hpp"
  26. #include "asio/detail/winrt_socket_send_op.hpp"
  27. #if defined(ASIO_HAS_IOCP)
  28. # include "asio/detail/win_iocp_io_context.hpp"
  29. #else // defined(ASIO_HAS_IOCP)
  30. # include "asio/detail/scheduler.hpp"
  31. #endif // defined(ASIO_HAS_IOCP)
  32. #include "asio/detail/push_options.hpp"
  33. namespace asio {
  34. namespace detail {
  35. class winrt_ssocket_service_base
  36. {
  37. public:
  38. // The native type of a socket.
  39. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
  40. // The implementation type of the socket.
  41. struct base_implementation_type
  42. {
  43. // Default constructor.
  44. base_implementation_type()
  45. : socket_(nullptr),
  46. next_(0),
  47. prev_(0)
  48. {
  49. }
  50. // The underlying native socket.
  51. native_handle_type socket_;
  52. // Pointers to adjacent socket implementations in linked list.
  53. base_implementation_type* next_;
  54. base_implementation_type* prev_;
  55. };
  56. // Constructor.
  57. ASIO_DECL winrt_ssocket_service_base(execution_context& context);
  58. // Destroy all user-defined handler objects owned by the service.
  59. ASIO_DECL void base_shutdown();
  60. // Construct a new socket implementation.
  61. ASIO_DECL void construct(base_implementation_type&);
  62. // Move-construct a new socket implementation.
  63. ASIO_DECL void base_move_construct(base_implementation_type& impl,
  64. base_implementation_type& other_impl) noexcept;
  65. // Move-assign from another socket implementation.
  66. ASIO_DECL void base_move_assign(base_implementation_type& impl,
  67. winrt_ssocket_service_base& other_service,
  68. base_implementation_type& other_impl);
  69. // Destroy a socket implementation.
  70. ASIO_DECL void destroy(base_implementation_type& impl);
  71. // Determine whether the socket is open.
  72. bool is_open(const base_implementation_type& impl) const
  73. {
  74. return impl.socket_ != nullptr;
  75. }
  76. // Destroy a socket implementation.
  77. ASIO_DECL asio::error_code close(
  78. base_implementation_type& impl, asio::error_code& ec);
  79. // Release ownership of the socket.
  80. ASIO_DECL native_handle_type release(
  81. base_implementation_type& impl, asio::error_code& ec);
  82. // Get the native socket representation.
  83. native_handle_type native_handle(base_implementation_type& impl)
  84. {
  85. return impl.socket_;
  86. }
  87. // Cancel all operations associated with the socket.
  88. asio::error_code cancel(base_implementation_type&,
  89. asio::error_code& ec)
  90. {
  91. ec = asio::error::operation_not_supported;
  92. return ec;
  93. }
  94. // Determine whether the socket is at the out-of-band data mark.
  95. bool at_mark(const base_implementation_type&,
  96. asio::error_code& ec) const
  97. {
  98. ec = asio::error::operation_not_supported;
  99. return false;
  100. }
  101. // Determine the number of bytes available for reading.
  102. std::size_t available(const base_implementation_type&,
  103. asio::error_code& ec) const
  104. {
  105. ec = asio::error::operation_not_supported;
  106. return 0;
  107. }
  108. // Perform an IO control command on the socket.
  109. template <typename IO_Control_Command>
  110. asio::error_code io_control(base_implementation_type&,
  111. IO_Control_Command&, asio::error_code& ec)
  112. {
  113. ec = asio::error::operation_not_supported;
  114. return ec;
  115. }
  116. // Gets the non-blocking mode of the socket.
  117. bool non_blocking(const base_implementation_type&) const
  118. {
  119. return false;
  120. }
  121. // Sets the non-blocking mode of the socket.
  122. asio::error_code non_blocking(base_implementation_type&,
  123. bool, asio::error_code& ec)
  124. {
  125. ec = asio::error::operation_not_supported;
  126. return ec;
  127. }
  128. // Gets the non-blocking mode of the native socket implementation.
  129. bool native_non_blocking(const base_implementation_type&) const
  130. {
  131. return false;
  132. }
  133. // Sets the non-blocking mode of the native socket implementation.
  134. asio::error_code native_non_blocking(base_implementation_type&,
  135. bool, asio::error_code& ec)
  136. {
  137. ec = asio::error::operation_not_supported;
  138. return ec;
  139. }
  140. // Send the given data to the peer.
  141. template <typename ConstBufferSequence>
  142. std::size_t send(base_implementation_type& impl,
  143. const ConstBufferSequence& buffers,
  144. socket_base::message_flags flags, asio::error_code& ec)
  145. {
  146. return do_send(impl,
  147. buffer_sequence_adapter<asio::const_buffer,
  148. ConstBufferSequence>::first(buffers), flags, ec);
  149. }
  150. // Wait until data can be sent without blocking.
  151. std::size_t send(base_implementation_type&, const null_buffers&,
  152. socket_base::message_flags, asio::error_code& ec)
  153. {
  154. ec = asio::error::operation_not_supported;
  155. return 0;
  156. }
  157. // Start an asynchronous send. The data being sent must be valid for the
  158. // lifetime of the asynchronous operation.
  159. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  160. void async_send(base_implementation_type& impl,
  161. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  162. Handler& handler, const IoExecutor& io_ex)
  163. {
  164. bool is_continuation =
  165. asio_handler_cont_helpers::is_continuation(handler);
  166. // Allocate and construct an operation to wrap the handler.
  167. typedef winrt_socket_send_op<ConstBufferSequence, Handler, IoExecutor> op;
  168. typename op::ptr p = { asio::detail::addressof(handler),
  169. op::ptr::allocate(handler), 0 };
  170. p.p = new (p.v) op(buffers, handler, io_ex);
  171. ASIO_HANDLER_CREATION((scheduler_.context(),
  172. *p.p, "socket", &impl, 0, "async_send"));
  173. start_send_op(impl,
  174. buffer_sequence_adapter<asio::const_buffer,
  175. ConstBufferSequence>::first(buffers),
  176. flags, p.p, is_continuation);
  177. p.v = p.p = 0;
  178. }
  179. // Start an asynchronous wait until data can be sent without blocking.
  180. template <typename Handler, typename IoExecutor>
  181. void async_send(base_implementation_type&, const null_buffers&,
  182. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  183. {
  184. asio::error_code ec = asio::error::operation_not_supported;
  185. const std::size_t bytes_transferred = 0;
  186. asio::post(io_ex,
  187. detail::bind_handler(handler, ec, bytes_transferred));
  188. }
  189. // Receive some data from the peer. Returns the number of bytes received.
  190. template <typename MutableBufferSequence>
  191. std::size_t receive(base_implementation_type& impl,
  192. const MutableBufferSequence& buffers,
  193. socket_base::message_flags flags, asio::error_code& ec)
  194. {
  195. return do_receive(impl,
  196. buffer_sequence_adapter<asio::mutable_buffer,
  197. MutableBufferSequence>::first(buffers), flags, ec);
  198. }
  199. // Wait until data can be received without blocking.
  200. std::size_t receive(base_implementation_type&, const null_buffers&,
  201. socket_base::message_flags, asio::error_code& ec)
  202. {
  203. ec = asio::error::operation_not_supported;
  204. return 0;
  205. }
  206. // Start an asynchronous receive. The buffer for the data being received
  207. // must be valid for the lifetime of the asynchronous operation.
  208. template <typename MutableBufferSequence,
  209. typename Handler, typename IoExecutor>
  210. void async_receive(base_implementation_type& impl,
  211. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  212. Handler& handler, const IoExecutor& io_ex)
  213. {
  214. bool is_continuation =
  215. asio_handler_cont_helpers::is_continuation(handler);
  216. // Allocate and construct an operation to wrap the handler.
  217. typedef winrt_socket_recv_op<MutableBufferSequence, Handler, IoExecutor> op;
  218. typename op::ptr p = { asio::detail::addressof(handler),
  219. op::ptr::allocate(handler), 0 };
  220. p.p = new (p.v) op(buffers, handler, io_ex);
  221. ASIO_HANDLER_CREATION((scheduler_.context(),
  222. *p.p, "socket", &impl, 0, "async_receive"));
  223. start_receive_op(impl,
  224. buffer_sequence_adapter<asio::mutable_buffer,
  225. MutableBufferSequence>::first(buffers),
  226. flags, p.p, is_continuation);
  227. p.v = p.p = 0;
  228. }
  229. // Wait until data can be received without blocking.
  230. template <typename Handler, typename IoExecutor>
  231. void async_receive(base_implementation_type&, const null_buffers&,
  232. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  233. {
  234. asio::error_code ec = asio::error::operation_not_supported;
  235. const std::size_t bytes_transferred = 0;
  236. asio::post(io_ex,
  237. detail::bind_handler(handler, ec, bytes_transferred));
  238. }
  239. protected:
  240. // Helper function to obtain endpoints associated with the connection.
  241. ASIO_DECL std::size_t do_get_endpoint(
  242. const base_implementation_type& impl, bool local,
  243. void* addr, std::size_t addr_len, asio::error_code& ec) const;
  244. // Helper function to set a socket option.
  245. ASIO_DECL asio::error_code do_set_option(
  246. base_implementation_type& impl,
  247. int level, int optname, const void* optval,
  248. std::size_t optlen, asio::error_code& ec);
  249. // Helper function to get a socket option.
  250. ASIO_DECL void do_get_option(
  251. const base_implementation_type& impl,
  252. int level, int optname, void* optval,
  253. std::size_t* optlen, asio::error_code& ec) const;
  254. // Helper function to perform a synchronous connect.
  255. ASIO_DECL asio::error_code do_connect(
  256. base_implementation_type& impl,
  257. const void* addr, asio::error_code& ec);
  258. // Helper function to start an asynchronous connect.
  259. ASIO_DECL void start_connect_op(
  260. base_implementation_type& impl, const void* addr,
  261. winrt_async_op<void>* op, bool is_continuation);
  262. // Helper function to perform a synchronous send.
  263. ASIO_DECL std::size_t do_send(
  264. base_implementation_type& impl, const asio::const_buffer& data,
  265. socket_base::message_flags flags, asio::error_code& ec);
  266. // Helper function to start an asynchronous send.
  267. ASIO_DECL void start_send_op(base_implementation_type& impl,
  268. const asio::const_buffer& data, socket_base::message_flags flags,
  269. winrt_async_op<unsigned int>* op, bool is_continuation);
  270. // Helper function to perform a synchronous receive.
  271. ASIO_DECL std::size_t do_receive(
  272. base_implementation_type& impl, const asio::mutable_buffer& data,
  273. socket_base::message_flags flags, asio::error_code& ec);
  274. // Helper function to start an asynchronous receive.
  275. ASIO_DECL void start_receive_op(base_implementation_type& impl,
  276. const asio::mutable_buffer& data, socket_base::message_flags flags,
  277. winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
  278. bool is_continuation);
  279. // The scheduler implementation used for delivering completions.
  280. #if defined(ASIO_HAS_IOCP)
  281. typedef class win_iocp_io_context scheduler_impl;
  282. #else
  283. typedef class scheduler scheduler_impl;
  284. #endif
  285. scheduler_impl& scheduler_;
  286. // The manager that keeps track of outstanding operations.
  287. winrt_async_manager& async_manager_;
  288. // Mutex to protect access to the linked list of implementations.
  289. asio::detail::mutex mutex_;
  290. // The head of a linked list of all implementations.
  291. base_implementation_type* impl_list_;
  292. };
  293. } // namespace detail
  294. } // namespace asio
  295. #include "asio/detail/pop_options.hpp"
  296. #if defined(ASIO_HEADER_ONLY)
  297. # include "asio/detail/impl/winrt_ssocket_service_base.ipp"
  298. #endif // defined(ASIO_HEADER_ONLY)
  299. #endif // defined(ASIO_WINDOWS_RUNTIME)
  300. #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP