winrt_ssocket_service_base.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // detail/winrt_ssocket_service_base.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_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/io_service.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/addressof.hpp"
  22. #include "asio/detail/buffer_sequence_adapter.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. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. namespace detail {
  30. class winrt_ssocket_service_base
  31. {
  32. public:
  33. // The native type of a socket.
  34. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
  35. // The implementation type of the socket.
  36. struct base_implementation_type
  37. {
  38. // Default constructor.
  39. base_implementation_type()
  40. : socket_(nullptr),
  41. next_(0),
  42. prev_(0)
  43. {
  44. }
  45. // The underlying native socket.
  46. native_handle_type socket_;
  47. // Pointers to adjacent socket implementations in linked list.
  48. base_implementation_type* next_;
  49. base_implementation_type* prev_;
  50. };
  51. // Constructor.
  52. ASIO_DECL winrt_ssocket_service_base(
  53. asio::io_service& io_service);
  54. // Destroy all user-defined handler objects owned by the service.
  55. ASIO_DECL void shutdown_service();
  56. // Construct a new socket implementation.
  57. ASIO_DECL void construct(base_implementation_type&);
  58. // Move-construct a new socket implementation.
  59. ASIO_DECL void base_move_construct(base_implementation_type& impl,
  60. base_implementation_type& other_impl);
  61. // Move-assign from another socket implementation.
  62. ASIO_DECL void base_move_assign(base_implementation_type& impl,
  63. winrt_ssocket_service_base& other_service,
  64. base_implementation_type& other_impl);
  65. // Destroy a socket implementation.
  66. ASIO_DECL void destroy(base_implementation_type& impl);
  67. // Determine whether the socket is open.
  68. bool is_open(const base_implementation_type& impl) const
  69. {
  70. return impl.socket_ != nullptr;
  71. }
  72. // Destroy a socket implementation.
  73. ASIO_DECL asio::error_code close(
  74. base_implementation_type& impl, asio::error_code& ec);
  75. // Get the native socket representation.
  76. native_handle_type native_handle(base_implementation_type& impl)
  77. {
  78. return impl.socket_;
  79. }
  80. // Cancel all operations associated with the socket.
  81. asio::error_code cancel(base_implementation_type&,
  82. asio::error_code& ec)
  83. {
  84. ec = asio::error::operation_not_supported;
  85. return ec;
  86. }
  87. // Determine whether the socket is at the out-of-band data mark.
  88. bool at_mark(const base_implementation_type&,
  89. asio::error_code& ec) const
  90. {
  91. ec = asio::error::operation_not_supported;
  92. return false;
  93. }
  94. // Determine the number of bytes available for reading.
  95. std::size_t available(const base_implementation_type&,
  96. asio::error_code& ec) const
  97. {
  98. ec = asio::error::operation_not_supported;
  99. return 0;
  100. }
  101. // Perform an IO control command on the socket.
  102. template <typename IO_Control_Command>
  103. asio::error_code io_control(base_implementation_type&,
  104. IO_Control_Command&, asio::error_code& ec)
  105. {
  106. ec = asio::error::operation_not_supported;
  107. return ec;
  108. }
  109. // Gets the non-blocking mode of the socket.
  110. bool non_blocking(const base_implementation_type&) const
  111. {
  112. return false;
  113. }
  114. // Sets the non-blocking mode of the socket.
  115. asio::error_code non_blocking(base_implementation_type&,
  116. bool, asio::error_code& ec)
  117. {
  118. ec = asio::error::operation_not_supported;
  119. return ec;
  120. }
  121. // Gets the non-blocking mode of the native socket implementation.
  122. bool native_non_blocking(const base_implementation_type&) const
  123. {
  124. return false;
  125. }
  126. // Sets the non-blocking mode of the native socket implementation.
  127. asio::error_code native_non_blocking(base_implementation_type&,
  128. bool, asio::error_code& ec)
  129. {
  130. ec = asio::error::operation_not_supported;
  131. return ec;
  132. }
  133. // Disable sends or receives on the socket.
  134. asio::error_code shutdown(base_implementation_type&,
  135. socket_base::shutdown_type, 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>
  160. void async_send(base_implementation_type& impl,
  161. const ConstBufferSequence& buffers,
  162. socket_base::message_flags flags, Handler& handler)
  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> op;
  168. typename op::ptr p = { asio::detail::addressof(handler),
  169. asio_handler_alloc_helpers::allocate(
  170. sizeof(op), handler), 0 };
  171. p.p = new (p.v) op(buffers, handler);
  172. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "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>
  181. void async_send(base_implementation_type&, const null_buffers&,
  182. socket_base::message_flags, Handler& handler)
  183. {
  184. asio::error_code ec = asio::error::operation_not_supported;
  185. const std::size_t bytes_transferred = 0;
  186. io_service_.get_io_service().post(
  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, typename Handler>
  209. void async_receive(base_implementation_type& impl,
  210. const MutableBufferSequence& buffers,
  211. socket_base::message_flags flags, Handler& handler)
  212. {
  213. bool is_continuation =
  214. asio_handler_cont_helpers::is_continuation(handler);
  215. // Allocate and construct an operation to wrap the handler.
  216. typedef winrt_socket_recv_op<MutableBufferSequence, Handler> op;
  217. typename op::ptr p = { asio::detail::addressof(handler),
  218. asio_handler_alloc_helpers::allocate(
  219. sizeof(op), handler), 0 };
  220. p.p = new (p.v) op(buffers, handler);
  221. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
  222. start_receive_op(impl,
  223. buffer_sequence_adapter<asio::mutable_buffer,
  224. MutableBufferSequence>::first(buffers),
  225. flags, p.p, is_continuation);
  226. p.v = p.p = 0;
  227. }
  228. // Wait until data can be received without blocking.
  229. template <typename Handler>
  230. void async_receive(base_implementation_type&, const null_buffers&,
  231. socket_base::message_flags, Handler& handler)
  232. {
  233. asio::error_code ec = asio::error::operation_not_supported;
  234. const std::size_t bytes_transferred = 0;
  235. io_service_.get_io_service().post(
  236. detail::bind_handler(handler, ec, bytes_transferred));
  237. }
  238. protected:
  239. // Helper function to obtain endpoints associated with the connection.
  240. ASIO_DECL std::size_t do_get_endpoint(
  241. const base_implementation_type& impl, bool local,
  242. void* addr, std::size_t addr_len, asio::error_code& ec) const;
  243. // Helper function to set a socket option.
  244. ASIO_DECL asio::error_code do_set_option(
  245. base_implementation_type& impl,
  246. int level, int optname, const void* optval,
  247. std::size_t optlen, asio::error_code& ec);
  248. // Helper function to get a socket option.
  249. ASIO_DECL void do_get_option(
  250. const base_implementation_type& impl,
  251. int level, int optname, void* optval,
  252. std::size_t* optlen, asio::error_code& ec) const;
  253. // Helper function to perform a synchronous connect.
  254. ASIO_DECL asio::error_code do_connect(
  255. base_implementation_type& impl,
  256. const void* addr, asio::error_code& ec);
  257. // Helper function to start an asynchronous connect.
  258. ASIO_DECL void start_connect_op(
  259. base_implementation_type& impl, const void* addr,
  260. winrt_async_op<void>* op, bool is_continuation);
  261. // Helper function to perform a synchronous send.
  262. ASIO_DECL std::size_t do_send(
  263. base_implementation_type& impl, const asio::const_buffer& data,
  264. socket_base::message_flags flags, asio::error_code& ec);
  265. // Helper function to start an asynchronous send.
  266. ASIO_DECL void start_send_op(base_implementation_type& impl,
  267. const asio::const_buffer& data, socket_base::message_flags flags,
  268. winrt_async_op<unsigned int>* op, bool is_continuation);
  269. // Helper function to perform a synchronous receive.
  270. ASIO_DECL std::size_t do_receive(
  271. base_implementation_type& impl, const asio::mutable_buffer& data,
  272. socket_base::message_flags flags, asio::error_code& ec);
  273. // Helper function to start an asynchronous receive.
  274. ASIO_DECL void start_receive_op(base_implementation_type& impl,
  275. const asio::mutable_buffer& data, socket_base::message_flags flags,
  276. winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
  277. bool is_continuation);
  278. // The io_service implementation used for delivering completions.
  279. io_service_impl& io_service_;
  280. // The manager that keeps track of outstanding operations.
  281. winrt_async_manager& async_manager_;
  282. // Mutex to protect access to the linked list of implementations.
  283. asio::detail::mutex mutex_;
  284. // The head of a linked list of all implementations.
  285. base_implementation_type* impl_list_;
  286. };
  287. } // namespace detail
  288. } // namespace asio
  289. #include "asio/detail/pop_options.hpp"
  290. #if defined(ASIO_HEADER_ONLY)
  291. # include "asio/detail/impl/winrt_ssocket_service_base.ipp"
  292. #endif // defined(ASIO_HEADER_ONLY)
  293. #endif // defined(ASIO_WINDOWS_RUNTIME)
  294. #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP