winrt_ssocket_service_base.hpp 13 KB

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