Realistic 3D camera system
3D camera system components
resolve_op.hpp
Go to the documentation of this file.
1 //
2 // detail/resolve_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_RESOLVE_OP_HPP
12 #define ASIO_DETAIL_RESOLVE_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 #include "asio/error.hpp"
20 #include "asio/io_service.hpp"
30 
32 
33 namespace asio {
34 namespace detail {
35 
36 template <typename Protocol, typename Handler>
37 class resolve_op : public operation
38 {
39 public:
41 
44 
46  const query_type& query, io_service_impl& ios, Handler& handler)
48  cancel_token_(cancel_token),
49  query_(query),
50  io_service_impl_(ios),
51  handler_(ASIO_MOVE_CAST(Handler)(handler)),
52  addrinfo_(0)
53  {
54  }
55 
57  {
58  if (addrinfo_)
59  socket_ops::freeaddrinfo(addrinfo_);
60  }
61 
62  static void do_complete(io_service_impl* owner, operation* base,
63  const asio::error_code& /*ec*/,
64  std::size_t /*bytes_transferred*/)
65  {
66  // Take ownership of the operation object.
67  resolve_op* o(static_cast<resolve_op*>(base));
68  ptr p = { asio::detail::addressof(o->handler_), o, o };
69 
70  if (owner && owner != &o->io_service_impl_)
71  {
72  // The operation is being run on the worker io_service. Time to perform
73  // the resolver operation.
74 
75  // Perform the blocking host resolution operation.
76  socket_ops::background_getaddrinfo(o->cancel_token_,
77  o->query_.host_name().c_str(), o->query_.service_name().c_str(),
78  o->query_.hints(), &o->addrinfo_, o->ec_);
79 
80  // Pass operation back to main io_service for completion.
81  o->io_service_impl_.post_deferred_completion(o);
82  p.v = p.p = 0;
83  }
84  else
85  {
86  // The operation has been returned to the main io_service. The completion
87  // handler is ready to be delivered.
88 
90 
91  // Make a copy of the handler so that the memory can be deallocated
92  // before the upcall is made. Even if we're not about to make an upcall,
93  // a sub-object of the handler may be the true owner of the memory
94  // associated with the handler. Consequently, a local copy of the handler
95  // is required to ensure that any owning sub-object remains valid until
96  // after we have deallocated the memory here.
98  handler(o->handler_, o->ec_, iterator_type());
99  p.h = asio::detail::addressof(handler.handler_);
100  if (o->addrinfo_)
101  {
102  handler.arg2_ = iterator_type::create(o->addrinfo_,
103  o->query_.host_name(), o->query_.service_name());
104  }
105  p.reset();
106 
107  if (owner)
108  {
110  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
113  }
114  }
115  }
116 
117 private:
119  query_type query_;
120  io_service_impl& io_service_impl_;
121  Handler handler_;
122  asio::error_code ec_;
123  asio::detail::addrinfo_type* addrinfo_;
124 };
125 
126 } // namespace detail
127 } // namespace asio
128 
130 
131 #endif // ASIO_DETAIL_RESOLVE_OP_HPP
static basic_resolver_iterator create(asio::detail::addrinfo_type *address_info, const std::string &host_name, const std::string &service_name)
Create an iterator from an addrinfo list returned by getaddrinfo.
#define ASIO_HANDLER_INVOCATION_END
ASIO_DEFINE_HANDLER_PTR(resolve_op)
resolve_op(socket_ops::weak_cancel_token_type cancel_token, const query_type &query, io_service_impl &ios, Handler &handler)
Definition: resolve_op.hpp:45
asio::ip::basic_resolver_iterator< Protocol > iterator_type
Definition: resolve_op.hpp:43
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
class task_io_service io_service_impl
Definition: io_service.hpp:48
addrinfo addrinfo_type
void invoke(Function &function, Context &context)
weak_ptr< void > weak_cancel_token_type
Definition: socket_ops.hpp:63
asio::error_code background_getaddrinfo(const weak_cancel_token_type &cancel_token, const char *host, const char *service, const addrinfo_type &hints, addrinfo_type **result, asio::error_code &ec)
#define ASIO_HANDLER_INVOCATION_BEGIN(args)
void freeaddrinfo(addrinfo_type *ai)
Class to represent an error code value.
Definition: error_code.hpp:80
task_io_service_operation operation
Definition: operation.hpp:32
asio::ip::basic_resolver_query< Protocol > query_type
Definition: resolve_op.hpp:42
#define ASIO_HANDLER_COMPLETION(args)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
static void do_complete(io_service_impl *owner, operation *base, const asio::error_code &, std::size_t)
Definition: resolve_op.hpp:62