Realistic 3D camera system
3D camera system components
resolve_endpoint_op.hpp
Go to the documentation of this file.
1 //
2 // detail/resolve_endpoint_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_RESOLVER_ENDPOINT_OP_HPP
12 #define ASIO_DETAIL_RESOLVER_ENDPOINT_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"
29 
31 
32 namespace asio {
33 namespace detail {
34 
35 template <typename Protocol, typename Handler>
37 {
38 public:
40 
41  typedef typename Protocol::endpoint endpoint_type;
43 
45  const endpoint_type& endpoint, io_service_impl& ios, Handler& handler)
47  cancel_token_(cancel_token),
48  endpoint_(endpoint),
49  io_service_impl_(ios),
50  handler_(ASIO_MOVE_CAST(Handler)(handler))
51  {
52  }
53 
54  static void do_complete(io_service_impl* owner, operation* base,
55  const asio::error_code& /*ec*/,
56  std::size_t /*bytes_transferred*/)
57  {
58  // Take ownership of the operation object.
59  resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base));
60  ptr p = { asio::detail::addressof(o->handler_), o, o };
61 
62  if (owner && owner != &o->io_service_impl_)
63  {
64  // The operation is being run on the worker io_service. Time to perform
65  // the resolver operation.
66 
67  // Perform the blocking endpoint resolution operation.
68  char host_name[NI_MAXHOST];
69  char service_name[NI_MAXSERV];
70  socket_ops::background_getnameinfo(o->cancel_token_, o->endpoint_.data(),
71  o->endpoint_.size(), host_name, NI_MAXHOST, service_name, NI_MAXSERV,
72  o->endpoint_.protocol().type(), o->ec_);
73  o->iter_ = iterator_type::create(o->endpoint_, host_name, service_name);
74 
75  // Pass operation back to main io_service for completion.
76  o->io_service_impl_.post_deferred_completion(o);
77  p.v = p.p = 0;
78  }
79  else
80  {
81  // The operation has been returned to the main io_service. The completion
82  // handler is ready to be delivered.
83 
85 
86  // Make a copy of the handler so that the memory can be deallocated
87  // before the upcall is made. Even if we're not about to make an upcall,
88  // a sub-object of the handler may be the true owner of the memory
89  // associated with the handler. Consequently, a local copy of the handler
90  // is required to ensure that any owning sub-object remains valid until
91  // after we have deallocated the memory here.
93  handler(o->handler_, o->ec_, o->iter_);
94  p.h = asio::detail::addressof(handler.handler_);
95  p.reset();
96 
97  if (owner)
98  {
100  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
103  }
104  }
105  }
106 
107 private:
109  endpoint_type endpoint_;
110  io_service_impl& io_service_impl_;
111  Handler handler_;
112  asio::error_code ec_;
113  iterator_type iter_;
114 };
115 
116 } // namespace detail
117 } // namespace asio
118 
120 
121 #endif // ASIO_DETAIL_RESOLVER_ENDPOINT_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
resolve_endpoint_op(socket_ops::weak_cancel_token_type cancel_token, const endpoint_type &endpoint, io_service_impl &ios, 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)
weak_ptr< void > weak_cancel_token_type
Definition: socket_ops.hpp:63
static void do_complete(io_service_impl *owner, operation *base, const asio::error_code &, std::size_t)
#define ASIO_HANDLER_INVOCATION_BEGIN(args)
Class to represent an error code value.
Definition: error_code.hpp:80
asio::ip::basic_resolver_iterator< Protocol > iterator_type
task_io_service_operation operation
Definition: operation.hpp:32
ASIO_DECL std::string host_name()
Get the current host name.
Definition: host_name.ipp:29
#define ASIO_HANDLER_COMPLETION(args)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
asio::error_code background_getnameinfo(const weak_cancel_token_type &cancel_token, const socket_addr_type *addr, std::size_t addrlen, char *host, std::size_t hostlen, char *serv, std::size_t servlen, int sock_type, asio::error_code &ec)
ASIO_DEFINE_HANDLER_PTR(resolve_endpoint_op)