Realistic 3D camera system
3D camera system components
reactive_socket_accept_op.hpp
Go to the documentation of this file.
1 //
2 // detail/reactive_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_REACTIVE_SOCKET_ACCEPT_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_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"
26 
28 
29 namespace asio {
30 namespace detail {
31 
32 template <typename Socket, typename Protocol>
34 {
35 public:
37  socket_ops::state_type state, Socket& peer, const Protocol& protocol,
38  typename Protocol::endpoint* peer_endpoint, func_type complete_func)
40  socket_(socket),
41  state_(state),
42  peer_(peer),
43  protocol_(protocol),
44  peer_endpoint_(peer_endpoint)
45  {
46  }
47 
48  static bool do_perform(reactor_op* base)
49  {
51  static_cast<reactive_socket_accept_op_base*>(base));
52 
53  std::size_t addrlen = o->peer_endpoint_ ? o->peer_endpoint_->capacity() : 0;
54  socket_type new_socket = invalid_socket;
55  bool result = socket_ops::non_blocking_accept(o->socket_,
56  o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
57  o->peer_endpoint_ ? &addrlen : 0, o->ec_, new_socket);
58 
59  // On success, assign new connection to peer socket object.
60  if (new_socket != invalid_socket)
61  {
62  socket_holder new_socket_holder(new_socket);
63  if (o->peer_endpoint_)
64  o->peer_endpoint_->resize(addrlen);
65  if (!o->peer_.assign(o->protocol_, new_socket, o->ec_))
66  new_socket_holder.release();
67  }
68 
69  return result;
70  }
71 
72 private:
73  socket_type socket_;
75  Socket& peer_;
76  Protocol protocol_;
77  typename Protocol::endpoint* peer_endpoint_;
78 };
79 
80 template <typename Socket, typename Protocol, typename Handler>
82  public reactive_socket_accept_op_base<Socket, Protocol>
83 {
84 public:
86 
88  socket_ops::state_type state, Socket& peer, const Protocol& protocol,
89  typename Protocol::endpoint* peer_endpoint, Handler& handler)
90  : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
91  protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
92  handler_(ASIO_MOVE_CAST(Handler)(handler))
93  {
94  }
95 
96  static void do_complete(io_service_impl* owner, operation* base,
97  const asio::error_code& /*ec*/,
98  std::size_t /*bytes_transferred*/)
99  {
100  // Take ownership of the handler object.
101  reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
102  ptr p = { asio::detail::addressof(o->handler_), o, o };
103 
105 
106  // Make a copy of the handler so that the memory can be deallocated before
107  // the upcall is made. Even if we're not about to make an upcall, a
108  // sub-object of the handler may be the true owner of the memory associated
109  // with the handler. Consequently, a local copy of the handler is required
110  // to ensure that any owning sub-object remains valid until after we have
111  // deallocated the memory here.
113  handler(o->handler_, o->ec_);
114  p.h = asio::detail::addressof(handler.handler_);
115  p.reset();
116 
117  // Make the upcall if required.
118  if (owner)
119  {
124  }
125  }
126 
127 private:
128  Handler handler_;
129 };
130 
131 } // namespace detail
132 } // namespace asio
133 
135 
136 #endif // ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_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
class task_io_service io_service_impl
Definition: io_service.hpp:48
void invoke(Function &function, Context &context)
#define ASIO_HANDLER_INVOCATION_BEGIN(args)
unsigned char state_type
Definition: socket_ops.hpp:59
Class to represent an error code value.
Definition: error_code.hpp:80
reactive_socket_accept_op(socket_type socket, socket_ops::state_type state, Socket &peer, const Protocol &protocol, typename Protocol::endpoint *peer_endpoint, Handler &handler)
task_io_service_operation operation
Definition: operation.hpp:32
asio::error_code ec_
Definition: reactor_op.hpp:31
reactive_socket_accept_op_base(socket_type socket, socket_ops::state_type state, Socket &peer, const Protocol &protocol, typename Protocol::endpoint *peer_endpoint, func_type complete_func)
#define ASIO_DEFINE_HANDLER_PTR(op)
#define ASIO_HANDLER_COMPLETION(args)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
const int invalid_socket
static void do_complete(io_service_impl *owner, operation *base, const asio::error_code &, std::size_t)
bool non_blocking_accept(socket_type s, state_type state, socket_addr_type *addr, std::size_t *addrlen, asio::error_code &ec, socket_type &new_socket)
Definition: socket_ops.ipp:223