Realistic 3D camera system
3D camera system components
win_iocp_socket_connect_op.hpp
Go to the documentation of this file.
1 //
2 // detail/win_iocp_socket_connect_op.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 
11 #ifndef ASIO_DETAIL_WIN_IOCP_SOCKET_CONNECT_OP_HPP
12 #define ASIO_DETAIL_WIN_IOCP_SOCKET_CONNECT_OP_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if defined(ASIO_HAS_IOCP)
21 
29 #include "asio/error.hpp"
30 
32 
33 namespace asio {
34 namespace detail {
35 
36 class win_iocp_socket_connect_op_base : public reactor_op
37 {
38 public:
39  win_iocp_socket_connect_op_base(socket_type socket, func_type complete_func)
40  : reactor_op(&win_iocp_socket_connect_op_base::do_perform, complete_func),
41  socket_(socket),
42  connect_ex_(false)
43  {
44  }
45 
46  static bool do_perform(reactor_op* base)
47  {
48  win_iocp_socket_connect_op_base* o(
49  static_cast<win_iocp_socket_connect_op_base*>(base));
50 
51  return socket_ops::non_blocking_connect(o->socket_, o->ec_);
52  }
53 
54  socket_type socket_;
55  bool connect_ex_;
56 };
57 
58 template <typename Handler>
59 class win_iocp_socket_connect_op : public win_iocp_socket_connect_op_base
60 {
61 public:
62  ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_connect_op);
63 
64  win_iocp_socket_connect_op(socket_type socket, Handler& handler)
65  : win_iocp_socket_connect_op_base(socket,
66  &win_iocp_socket_connect_op::do_complete),
67  handler_(ASIO_MOVE_CAST(Handler)(handler))
68  {
69  }
70 
71  static void do_complete(io_service_impl* owner, operation* base,
72  const asio::error_code& result_ec,
73  std::size_t /*bytes_transferred*/)
74  {
75  asio::error_code ec(result_ec);
76 
77  // Take ownership of the operation object.
78  win_iocp_socket_connect_op* o(
79  static_cast<win_iocp_socket_connect_op*>(base));
80  ptr p = { asio::detail::addressof(o->handler_), o, o };
81 
82  if (owner)
83  {
84  if (o->connect_ex_)
85  socket_ops::complete_iocp_connect(o->socket_, ec);
86  else
87  ec = o->ec_;
88  }
89 
91 
92  // Make a copy of the handler so that the memory can be deallocated before
93  // the upcall is made. Even if we're not about to make an upcall, a
94  // sub-object of the handler may be the true owner of the memory associated
95  // with the handler. Consequently, a local copy of the handler is required
96  // to ensure that any owning sub-object remains valid until after we have
97  // deallocated the memory here.
98  detail::binder1<Handler, asio::error_code>
99  handler(o->handler_, ec);
100  p.h = asio::detail::addressof(handler.handler_);
101  p.reset();
102 
103  // Make the upcall if required.
104  if (owner)
105  {
107  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
108  asio_handler_invoke_helpers::invoke(handler, handler.handler_);
110  }
111  }
112 
113 private:
114  Handler handler_;
115 };
116 
117 } // namespace detail
118 } // namespace asio
119 
121 
122 #endif // defined(ASIO_HAS_IOCP)
123 
124 #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_CONNECT_OP_HPP
#define ASIO_HANDLER_INVOCATION_END
socket_type socket(int af, int type, int protocol, asio::error_code &ec)
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
null_fenced_block fenced_block
class task_io_service io_service_impl
Definition: io_service.hpp:48
void invoke(Function &function, Context &context)
#define ASIO_HANDLER_INVOCATION_BEGIN(args)
Class to represent an error code value.
Definition: error_code.hpp:80
task_io_service_operation operation
Definition: operation.hpp:32
#define ASIO_DEFINE_HANDLER_PTR(op)
#define ASIO_HANDLER_COMPLETION(args)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
bool non_blocking_connect(socket_type s, asio::error_code &ec)
Definition: socket_ops.ipp:559