winrt_socket_connect_op.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // detail/winrt_socket_connect_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_SOCKET_CONNECT_OP_HPP
  11. #define BOOST_ASIO_DETAIL_WINRT_SOCKET_CONNECT_OP_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/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/fenced_block.hpp>
  20. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  21. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  22. #include <boost/asio/detail/memory.hpp>
  23. #include <boost/asio/detail/winrt_async_op.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. template <typename Handler>
  30. class winrt_socket_connect_op :
  31. public winrt_async_op<void>
  32. {
  33. public:
  34. BOOST_ASIO_DEFINE_HANDLER_PTR(winrt_socket_connect_op);
  35. winrt_socket_connect_op(Handler& handler)
  36. : winrt_async_op<void>(&winrt_socket_connect_op::do_complete),
  37. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  38. {
  39. handler_work<Handler>::start(handler_);
  40. }
  41. static void do_complete(void* owner, operation* base,
  42. const boost::system::error_code&, std::size_t)
  43. {
  44. // Take ownership of the operation object.
  45. winrt_socket_connect_op* o(static_cast<winrt_socket_connect_op*>(base));
  46. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  47. handler_work<Handler> w(o->handler_);
  48. BOOST_ASIO_HANDLER_COMPLETION((*o));
  49. // Make a copy of the handler so that the memory can be deallocated before
  50. // the upcall is made. Even if we're not about to make an upcall, a
  51. // sub-object of the handler may be the true owner of the memory associated
  52. // with the handler. Consequently, a local copy of the handler is required
  53. // to ensure that any owning sub-object remains valid until after we have
  54. // deallocated the memory here.
  55. detail::binder1<Handler, boost::system::error_code>
  56. handler(o->handler_, o->ec_);
  57. p.h = boost::asio::detail::addressof(handler.handler_);
  58. p.reset();
  59. // Make the upcall if required.
  60. if (owner)
  61. {
  62. fenced_block b(fenced_block::half);
  63. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  64. w.complete(handler, handler.handler_);
  65. BOOST_ASIO_HANDLER_INVOCATION_END;
  66. }
  67. }
  68. private:
  69. Handler handler_;
  70. };
  71. } // namespace detail
  72. } // namespace asio
  73. } // namespace boost
  74. #include <boost/asio/detail/pop_options.hpp>
  75. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  76. #endif // BOOST_ASIO_DETAIL_WINRT_SOCKET_CONNECT_OP_HPP