PageRenderTime 23ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/package/boost_1_58_0/boost/asio/detail/winrt_ssocket_service_base.hpp

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