Realistic 3D camera system
3D camera system components
reactive_socket_recvfrom_op.hpp
Go to the documentation of this file.
1 //
2 // detail/reactive_socket_recvfrom_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_RECVFROM_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_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"
25 
27 
28 namespace asio {
29 namespace detail {
30 
31 template <typename MutableBufferSequence, typename Endpoint>
33 {
34 public:
36  const MutableBufferSequence& buffers, Endpoint& endpoint,
37  socket_base::message_flags flags, func_type complete_func)
39  socket_(socket),
40  protocol_type_(protocol_type),
41  buffers_(buffers),
42  sender_endpoint_(endpoint),
43  flags_(flags)
44  {
45  }
46 
47  static bool do_perform(reactor_op* base)
48  {
50  static_cast<reactive_socket_recvfrom_op_base*>(base));
51 
53  MutableBufferSequence> bufs(o->buffers_);
54 
55  std::size_t addr_len = o->sender_endpoint_.capacity();
56  bool result = socket_ops::non_blocking_recvfrom(o->socket_,
57  bufs.buffers(), bufs.count(), o->flags_,
58  o->sender_endpoint_.data(), &addr_len,
59  o->ec_, o->bytes_transferred_);
60 
61  if (result && !o->ec_)
62  o->sender_endpoint_.resize(addr_len);
63 
64  return result;
65  }
66 
67 private:
68  socket_type socket_;
69  int protocol_type_;
70  MutableBufferSequence buffers_;
71  Endpoint& sender_endpoint_;
73 };
74 
75 template <typename MutableBufferSequence, typename Endpoint, typename Handler>
77  public reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>
78 {
79 public:
81 
83  const MutableBufferSequence& buffers, Endpoint& endpoint,
84  socket_base::message_flags flags, Handler& handler)
85  : reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>(
86  socket, protocol_type, buffers, endpoint, flags,
87  &reactive_socket_recvfrom_op::do_complete),
88  handler_(ASIO_MOVE_CAST(Handler)(handler))
89  {
90  }
91 
92  static void do_complete(io_service_impl* owner, operation* base,
93  const asio::error_code& /*ec*/,
94  std::size_t /*bytes_transferred*/)
95  {
96  // Take ownership of the handler object.
98  static_cast<reactive_socket_recvfrom_op*>(base));
99  ptr p = { asio::detail::addressof(o->handler_), o, o };
100 
102 
103  // Make a copy of the handler so that the memory can be deallocated before
104  // the upcall is made. Even if we're not about to make an upcall, a
105  // sub-object of the handler may be the true owner of the memory associated
106  // with the handler. Consequently, a local copy of the handler is required
107  // to ensure that any owning sub-object remains valid until after we have
108  // deallocated the memory here.
110  handler(o->handler_, o->ec_, o->bytes_transferred_);
111  p.h = asio::detail::addressof(handler.handler_);
112  p.reset();
113 
114  // Make the upcall if required.
115  if (owner)
116  {
118  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
121  }
122  }
123 
124 private:
125  Handler handler_;
126 };
127 
128 } // namespace detail
129 } // namespace asio
130 
132 
133 #endif // ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP
reactive_socket_recvfrom_op_base(socket_type socket, int protocol_type, const MutableBufferSequence &buffers, Endpoint &endpoint, socket_base::message_flags flags, func_type complete_func)
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
static void do_complete(io_service_impl *owner, operation *base, const asio::error_code &, std::size_t)
#define ASIO_HANDLER_INVOCATION_END
socket_type socket(int af, int type, int protocol, asio::error_code &ec)
reactive_socket_recvfrom_op(socket_type socket, int protocol_type, const MutableBufferSequence &buffers, Endpoint &endpoint, socket_base::message_flags flags, Handler &handler)
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)
const MutableBufferSequence & buffers
Definition: read.hpp:521
Holds a buffer that can be modified.
Definition: buffer.hpp:91
#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
asio::error_code ec_
Definition: reactor_op.hpp:31
std::size_t bytes_transferred_
Definition: reactor_op.hpp:34
#define ASIO_DEFINE_HANDLER_PTR(op)
#define ASIO_HANDLER_COMPLETION(args)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
bool non_blocking_recvfrom(socket_type s, buf *bufs, size_t count, int flags, socket_addr_type *addr, std::size_t *addrlen, asio::error_code &ec, size_t &bytes_transferred)
Definition: socket_ops.ipp:996