winrt_ssocket_service_base.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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);
  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. // Disable sends or receives on the socket.
  142. boost::system::error_code shutdown(base_implementation_type&,
  143. socket_base::shutdown_type, boost::system::error_code& ec)
  144. {
  145. ec = boost::asio::error::operation_not_supported;
  146. return ec;
  147. }
  148. // Send the given data to the peer.
  149. template <typename ConstBufferSequence>
  150. std::size_t send(base_implementation_type& impl,
  151. const ConstBufferSequence& buffers,
  152. socket_base::message_flags flags, boost::system::error_code& ec)
  153. {
  154. return do_send(impl,
  155. buffer_sequence_adapter<boost::asio::const_buffer,
  156. ConstBufferSequence>::first(buffers), flags, ec);
  157. }
  158. // Wait until data can be sent without blocking.
  159. std::size_t send(base_implementation_type&, const null_buffers&,
  160. socket_base::message_flags, boost::system::error_code& ec)
  161. {
  162. ec = boost::asio::error::operation_not_supported;
  163. return 0;
  164. }
  165. // Start an asynchronous send. The data being sent must be valid for the
  166. // lifetime of the asynchronous operation.
  167. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  168. void async_send(base_implementation_type& impl,
  169. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  170. Handler& handler, const IoExecutor& io_ex)
  171. {
  172. bool is_continuation =
  173. boost_asio_handler_cont_helpers::is_continuation(handler);
  174. // Allocate and construct an operation to wrap the handler.
  175. typedef winrt_socket_send_op<ConstBufferSequence, Handler, IoExecutor> op;
  176. typename op::ptr p = { boost::asio::detail::addressof(handler),
  177. op::ptr::allocate(handler), 0 };
  178. p.p = new (p.v) op(buffers, handler, io_ex);
  179. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  180. *p.p, "socket", &impl, 0, "async_send"));
  181. start_send_op(impl,
  182. buffer_sequence_adapter<boost::asio::const_buffer,
  183. ConstBufferSequence>::first(buffers),
  184. flags, p.p, is_continuation);
  185. p.v = p.p = 0;
  186. }
  187. // Start an asynchronous wait until data can be sent without blocking.
  188. template <typename Handler, typename IoExecutor>
  189. void async_send(base_implementation_type&, const null_buffers&,
  190. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  191. {
  192. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  193. const std::size_t bytes_transferred = 0;
  194. boost::asio::post(io_ex,
  195. detail::bind_handler(handler, ec, bytes_transferred));
  196. }
  197. // Receive some data from the peer. Returns the number of bytes received.
  198. template <typename MutableBufferSequence>
  199. std::size_t receive(base_implementation_type& impl,
  200. const MutableBufferSequence& buffers,
  201. socket_base::message_flags flags, boost::system::error_code& ec)
  202. {
  203. return do_receive(impl,
  204. buffer_sequence_adapter<boost::asio::mutable_buffer,
  205. MutableBufferSequence>::first(buffers), flags, ec);
  206. }
  207. // Wait until data can be received without blocking.
  208. std::size_t receive(base_implementation_type&, const null_buffers&,
  209. socket_base::message_flags, boost::system::error_code& ec)
  210. {
  211. ec = boost::asio::error::operation_not_supported;
  212. return 0;
  213. }
  214. // Start an asynchronous receive. The buffer for the data being received
  215. // must be valid for the lifetime of the asynchronous operation.
  216. template <typename MutableBufferSequence,
  217. typename Handler, typename IoExecutor>
  218. void async_receive(base_implementation_type& impl,
  219. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  220. Handler& handler, const IoExecutor& io_ex)
  221. {
  222. bool is_continuation =
  223. boost_asio_handler_cont_helpers::is_continuation(handler);
  224. // Allocate and construct an operation to wrap the handler.
  225. typedef winrt_socket_recv_op<MutableBufferSequence, Handler, IoExecutor> op;
  226. typename op::ptr p = { boost::asio::detail::addressof(handler),
  227. op::ptr::allocate(handler), 0 };
  228. p.p = new (p.v) op(buffers, handler, io_ex);
  229. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  230. *p.p, "socket", &impl, 0, "async_receive"));
  231. start_receive_op(impl,
  232. buffer_sequence_adapter<boost::asio::mutable_buffer,
  233. MutableBufferSequence>::first(buffers),
  234. flags, p.p, is_continuation);
  235. p.v = p.p = 0;
  236. }
  237. // Wait until data can be received without blocking.
  238. template <typename Handler, typename IoExecutor>
  239. void async_receive(base_implementation_type&, const null_buffers&,
  240. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  241. {
  242. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  243. const std::size_t bytes_transferred = 0;
  244. boost::asio::post(io_ex,
  245. detail::bind_handler(handler, ec, bytes_transferred));
  246. }
  247. protected:
  248. // Helper function to obtain endpoints associated with the connection.
  249. BOOST_ASIO_DECL std::size_t do_get_endpoint(
  250. const base_implementation_type& impl, bool local,
  251. void* addr, std::size_t addr_len, boost::system::error_code& ec) const;
  252. // Helper function to set a socket option.
  253. BOOST_ASIO_DECL boost::system::error_code do_set_option(
  254. base_implementation_type& impl,
  255. int level, int optname, const void* optval,
  256. std::size_t optlen, boost::system::error_code& ec);
  257. // Helper function to get a socket option.
  258. BOOST_ASIO_DECL void do_get_option(
  259. const base_implementation_type& impl,
  260. int level, int optname, void* optval,
  261. std::size_t* optlen, boost::system::error_code& ec) const;
  262. // Helper function to perform a synchronous connect.
  263. BOOST_ASIO_DECL boost::system::error_code do_connect(
  264. base_implementation_type& impl,
  265. const void* addr, boost::system::error_code& ec);
  266. // Helper function to start an asynchronous connect.
  267. BOOST_ASIO_DECL void start_connect_op(
  268. base_implementation_type& impl, const void* addr,
  269. winrt_async_op<void>* op, bool is_continuation);
  270. // Helper function to perform a synchronous send.
  271. BOOST_ASIO_DECL std::size_t do_send(
  272. base_implementation_type& impl, const boost::asio::const_buffer& data,
  273. socket_base::message_flags flags, boost::system::error_code& ec);
  274. // Helper function to start an asynchronous send.
  275. BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
  276. const boost::asio::const_buffer& data, socket_base::message_flags flags,
  277. winrt_async_op<unsigned int>* op, bool is_continuation);
  278. // Helper function to perform a synchronous receive.
  279. BOOST_ASIO_DECL std::size_t do_receive(
  280. base_implementation_type& impl, const boost::asio::mutable_buffer& data,
  281. socket_base::message_flags flags, boost::system::error_code& ec);
  282. // Helper function to start an asynchronous receive.
  283. BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
  284. const boost::asio::mutable_buffer& data, socket_base::message_flags flags,
  285. winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
  286. bool is_continuation);
  287. // The scheduler implementation used for delivering completions.
  288. #if defined(BOOST_ASIO_HAS_IOCP)
  289. typedef class win_iocp_io_context scheduler_impl;
  290. #else
  291. typedef class scheduler scheduler_impl;
  292. #endif
  293. scheduler_impl& scheduler_;
  294. // The manager that keeps track of outstanding operations.
  295. winrt_async_manager& async_manager_;
  296. // Mutex to protect access to the linked list of implementations.
  297. boost::asio::detail::mutex mutex_;
  298. // The head of a linked list of all implementations.
  299. base_implementation_type* impl_list_;
  300. };
  301. } // namespace detail
  302. } // namespace asio
  303. } // namespace boost
  304. #include <boost/asio/detail/pop_options.hpp>
  305. #if defined(BOOST_ASIO_HEADER_ONLY)
  306. # include <boost/asio/detail/impl/winrt_ssocket_service_base.ipp>
  307. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  308. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  309. #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP