Realistic 3D camera system
3D camera system components
win_iocp_socket_accept_op.hpp
Go to the documentation of this file.
1 //
2 // detail/win_iocp_socket_accept_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_ACCEPT_OP_HPP
12 #define ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_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 
31 #include "asio/error.hpp"
32 
34 
35 namespace asio {
36 namespace detail {
37 
38 template <typename Socket, typename Protocol, typename Handler>
39 class win_iocp_socket_accept_op : public operation
40 {
41 public:
42  ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_accept_op);
43 
44  win_iocp_socket_accept_op(win_iocp_socket_service_base& socket_service,
45  socket_type socket, Socket& peer, const Protocol& protocol,
46  typename Protocol::endpoint* peer_endpoint,
47  bool enable_connection_aborted, Handler& handler)
48  : operation(&win_iocp_socket_accept_op::do_complete),
49  socket_service_(socket_service),
50  socket_(socket),
51  peer_(peer),
52  protocol_(protocol),
53  peer_endpoint_(peer_endpoint),
54  enable_connection_aborted_(enable_connection_aborted),
55  handler_(ASIO_MOVE_CAST(Handler)(handler))
56  {
57  }
58 
59  socket_holder& new_socket()
60  {
61  return new_socket_;
62  }
63 
64  void* output_buffer()
65  {
66  return output_buffer_;
67  }
68 
69  DWORD address_length()
70  {
71  return sizeof(sockaddr_storage_type) + 16;
72  }
73 
74  static void do_complete(io_service_impl* owner, operation* base,
75  const asio::error_code& result_ec,
76  std::size_t /*bytes_transferred*/)
77  {
78  asio::error_code ec(result_ec);
79 
80  // Take ownership of the operation object.
81  win_iocp_socket_accept_op* o(static_cast<win_iocp_socket_accept_op*>(base));
82  ptr p = { asio::detail::addressof(o->handler_), o, o };
83 
84  if (owner)
85  {
86  typename Protocol::endpoint peer_endpoint;
87  std::size_t addr_len = peer_endpoint.capacity();
88  socket_ops::complete_iocp_accept(o->socket_,
89  o->output_buffer(), o->address_length(),
90  peer_endpoint.data(), &addr_len,
91  o->new_socket_.get(), ec);
92 
93  // Restart the accept operation if we got the connection_aborted error
94  // and the enable_connection_aborted socket option is not set.
96  && !o->enable_connection_aborted_)
97  {
98  o->reset();
99  o->socket_service_.restart_accept_op(o->socket_,
100  o->new_socket_, o->protocol_.family(),
101  o->protocol_.type(), o->protocol_.protocol(),
102  o->output_buffer(), o->address_length(), o);
103  p.v = p.p = 0;
104  return;
105  }
106 
107  // If the socket was successfully accepted, transfer ownership of the
108  // socket to the peer object.
109  if (!ec)
110  {
111  o->peer_.assign(o->protocol_,
112  typename Socket::native_handle_type(
113  o->new_socket_.get(), peer_endpoint), ec);
114  if (!ec)
115  o->new_socket_.release();
116  }
117 
118  // Pass endpoint back to caller.
119  if (o->peer_endpoint_)
120  *o->peer_endpoint_ = peer_endpoint;
121  }
122 
124 
125  // Make a copy of the handler so that the memory can be deallocated before
126  // the upcall is made. Even if we're not about to make an upcall, a
127  // sub-object of the handler may be the true owner of the memory associated
128  // with the handler. Consequently, a local copy of the handler is required
129  // to ensure that any owning sub-object remains valid until after we have
130  // deallocated the memory here.
131  detail::binder1<Handler, asio::error_code>
132  handler(o->handler_, ec);
133  p.h = asio::detail::addressof(handler.handler_);
134  p.reset();
135 
136  // Make the upcall if required.
137  if (owner)
138  {
140  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
141  asio_handler_invoke_helpers::invoke(handler, handler.handler_);
143  }
144  }
145 
146 private:
147  win_iocp_socket_service_base& socket_service_;
148  socket_type socket_;
149  socket_holder new_socket_;
150  Socket& peer_;
151  Protocol protocol_;
152  typename Protocol::endpoint* peer_endpoint_;
153  unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
154  bool enable_connection_aborted_;
155  Handler handler_;
156 };
157 
158 } // namespace detail
159 } // namespace asio
160 
162 
163 #endif // defined(ASIO_HAS_IOCP)
164 
165 #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP
#define ASIO_HANDLER_INVOCATION_END
socket_type socket(int af, int type, int protocol, asio::error_code &ec)
sockaddr_storage sockaddr_storage_type
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
A connection has been aborted.
Definition: error.hpp:89