Realistic 3D camera system
3D camera system components
connect.hpp
Go to the documentation of this file.
1 //
2 // impl/connect.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_IMPL_CONNECT_HPP
12 #define ASIO_IMPL_CONNECT_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
25 #include "asio/error.hpp"
26 
28 
29 namespace asio {
30 
31 namespace detail
32 {
34  {
35  template <typename Iterator>
36  Iterator operator()(const asio::error_code&, Iterator next)
37  {
38  return next;
39  }
40  };
41 }
42 
43 template <typename Protocol, typename SocketService, typename Iterator>
45 {
47  Iterator result = connect(s, begin, ec);
48  asio::detail::throw_error(ec, "connect");
49  return result;
50 }
51 
52 template <typename Protocol, typename SocketService, typename Iterator>
54  Iterator begin, asio::error_code& ec)
55 {
56  return connect(s, begin, Iterator(), detail::default_connect_condition(), ec);
57 }
58 
59 template <typename Protocol, typename SocketService, typename Iterator>
61  Iterator begin, Iterator end)
62 {
64  Iterator result = connect(s, begin, end, ec);
65  asio::detail::throw_error(ec, "connect");
66  return result;
67 }
68 
69 template <typename Protocol, typename SocketService, typename Iterator>
71  Iterator begin, Iterator end, asio::error_code& ec)
72 {
73  return connect(s, begin, end, detail::default_connect_condition(), ec);
74 }
75 
76 template <typename Protocol, typename SocketService,
77  typename Iterator, typename ConnectCondition>
79  Iterator begin, ConnectCondition connect_condition)
80 {
82  Iterator result = connect(s, begin, connect_condition, ec);
83  asio::detail::throw_error(ec, "connect");
84  return result;
85 }
86 
87 template <typename Protocol, typename SocketService,
88  typename Iterator, typename ConnectCondition>
90  Iterator begin, ConnectCondition connect_condition,
91  asio::error_code& ec)
92 {
93  return connect(s, begin, Iterator(), connect_condition, ec);
94 }
95 
96 template <typename Protocol, typename SocketService,
97  typename Iterator, typename ConnectCondition>
99  Iterator begin, Iterator end, ConnectCondition connect_condition)
100 {
101  asio::error_code ec;
102  Iterator result = connect(s, begin, end, connect_condition, ec);
103  asio::detail::throw_error(ec, "connect");
104  return result;
105 }
106 
107 template <typename Protocol, typename SocketService,
108  typename Iterator, typename ConnectCondition>
110  Iterator begin, Iterator end, ConnectCondition connect_condition,
111  asio::error_code& ec)
112 {
113  ec = asio::error_code();
114 
115  for (Iterator iter = begin; iter != end; ++iter)
116  {
117  iter = connect_condition(ec, iter);
118  if (iter != end)
119  {
120  s.close(ec);
121  s.connect(*iter, ec);
122  if (!ec)
123  return iter;
124  }
125  }
126 
127  if (!ec)
129 
130  return end;
131 }
132 
133 namespace detail
134 {
135  // Enable the empty base class optimisation for the connect condition.
136  template <typename ConnectCondition>
138  {
139  protected:
141  const ConnectCondition& connect_condition)
142  : connect_condition_(connect_condition)
143  {
144  }
145 
146  template <typename Iterator>
148  Iterator& iter, Iterator& end)
149  {
150  if (iter != end)
151  iter = connect_condition_(ec, static_cast<const Iterator&>(iter));
152  }
153 
154  private:
155  ConnectCondition connect_condition_;
156  };
157 
158  // The default_connect_condition implementation is essentially a no-op. This
159  // template specialisation lets us eliminate all costs associated with it.
160  template <>
162  {
163  protected:
165  {
166  }
167 
168  template <typename Iterator>
169  void check_condition(const asio::error_code&, Iterator&, Iterator&)
170  {
171  }
172  };
173 
174  template <typename Protocol, typename SocketService, typename Iterator,
175  typename ConnectCondition, typename ComposedConnectHandler>
176  class connect_op : base_from_connect_condition<ConnectCondition>
177  {
178  public:
180  const Iterator& begin, const Iterator& end,
181  const ConnectCondition& connect_condition,
182  ComposedConnectHandler& handler)
183  : base_from_connect_condition<ConnectCondition>(connect_condition),
184  socket_(sock),
185  iter_(begin),
186  end_(end),
187  start_(0),
188  handler_(ASIO_MOVE_CAST(ComposedConnectHandler)(handler))
189  {
190  }
191 
192 #if defined(ASIO_HAS_MOVE)
193  connect_op(const connect_op& other)
195  socket_(other.socket_),
196  iter_(other.iter_),
197  end_(other.end_),
198  start_(other.start_),
199  handler_(other.handler_)
200  {
201  }
202 
203  connect_op(connect_op&& other)
205  socket_(other.socket_),
206  iter_(other.iter_),
207  end_(other.end_),
208  start_(other.start_),
209  handler_(ASIO_MOVE_CAST(ComposedConnectHandler)(other.handler_))
210  {
211  }
212 #endif // defined(ASIO_HAS_MOVE)
213 
214  void operator()(asio::error_code ec, int start = 0)
215  {
216  switch (start_ = start)
217  {
218  case 1:
219  for (;;)
220  {
221  this->check_condition(ec, iter_, end_);
222 
223  if (iter_ != end_)
224  {
225  socket_.close(ec);
226  socket_.async_connect(*iter_,
227  ASIO_MOVE_CAST(connect_op)(*this));
228  return;
229  }
230 
231  if (start)
232  {
234  socket_.get_io_service().post(detail::bind_handler(*this, ec));
235  return;
236  }
237 
238  default:
239 
240  if (iter_ == end_)
241  break;
242 
243  if (!socket_.is_open())
244  {
246  break;
247  }
248 
249  if (!ec)
250  break;
251 
252  ++iter_;
253  }
254 
255  handler_(static_cast<const asio::error_code&>(ec),
256  static_cast<const Iterator&>(iter_));
257  }
258  }
259 
260  //private:
262  Iterator iter_;
263  Iterator end_;
264  int start_;
265  ComposedConnectHandler handler_;
266  };
267 
268  template <typename Protocol, typename SocketService, typename Iterator,
269  typename ConnectCondition, typename ComposedConnectHandler>
270  inline void* asio_handler_allocate(std::size_t size,
271  connect_op<Protocol, SocketService, Iterator,
272  ConnectCondition, ComposedConnectHandler>* this_handler)
273  {
275  size, this_handler->handler_);
276  }
277 
278  template <typename Protocol, typename SocketService, typename Iterator,
279  typename ConnectCondition, typename ComposedConnectHandler>
280  inline void asio_handler_deallocate(void* pointer, std::size_t size,
281  connect_op<Protocol, SocketService, Iterator,
282  ConnectCondition, ComposedConnectHandler>* this_handler)
283  {
285  pointer, size, this_handler->handler_);
286  }
287 
288  template <typename Protocol, typename SocketService, typename Iterator,
289  typename ConnectCondition, typename ComposedConnectHandler>
291  connect_op<Protocol, SocketService, Iterator,
292  ConnectCondition, ComposedConnectHandler>* this_handler)
293  {
295  this_handler->handler_);
296  }
297 
298  template <typename Function, typename Protocol,
299  typename SocketService, typename Iterator,
300  typename ConnectCondition, typename ComposedConnectHandler>
301  inline void asio_handler_invoke(Function& function,
302  connect_op<Protocol, SocketService, Iterator,
303  ConnectCondition, ComposedConnectHandler>* this_handler)
304  {
306  function, this_handler->handler_);
307  }
308 
309  template <typename Function, typename Protocol,
310  typename SocketService, typename Iterator,
311  typename ConnectCondition, typename ComposedConnectHandler>
312  inline void asio_handler_invoke(const Function& function,
313  connect_op<Protocol, SocketService, Iterator,
314  ConnectCondition, ComposedConnectHandler>* this_handler)
315  {
317  function, this_handler->handler_);
318  }
319 } // namespace detail
320 
321 template <typename Protocol, typename SocketService,
322  typename Iterator, typename ComposedConnectHandler>
323 inline ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
324  void (asio::error_code, Iterator))
326  Iterator begin, ASIO_MOVE_ARG(ComposedConnectHandler) handler)
327 {
328  // If you get an error on the following line it means that your handler does
329  // not meet the documented type requirements for a ComposedConnectHandler.
331  ComposedConnectHandler, handler, Iterator) type_check;
332 
333  detail::async_result_init<ComposedConnectHandler,
334  void (asio::error_code, Iterator)> init(
335  ASIO_MOVE_CAST(ComposedConnectHandler)(handler));
336 
337  detail::connect_op<Protocol, SocketService, Iterator,
339  ComposedConnectHandler, void (asio::error_code, Iterator))>(s,
340  begin, Iterator(), detail::default_connect_condition(), init.handler)(
341  asio::error_code(), 1);
342 
343  return init.result.get();
344 }
345 
346 template <typename Protocol, typename SocketService,
347  typename Iterator, typename ComposedConnectHandler>
348 inline ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
349  void (asio::error_code, Iterator))
351  Iterator begin, Iterator end,
352  ASIO_MOVE_ARG(ComposedConnectHandler) handler)
353 {
354  // If you get an error on the following line it means that your handler does
355  // not meet the documented type requirements for a ComposedConnectHandler.
357  ComposedConnectHandler, handler, Iterator) type_check;
358 
359  detail::async_result_init<ComposedConnectHandler,
360  void (asio::error_code, Iterator)> init(
361  ASIO_MOVE_CAST(ComposedConnectHandler)(handler));
362 
363  detail::connect_op<Protocol, SocketService, Iterator,
365  ComposedConnectHandler, void (asio::error_code, Iterator))>(s,
366  begin, end, detail::default_connect_condition(), init.handler)(
367  asio::error_code(), 1);
368 
369  return init.result.get();
370 }
371 
372 template <typename Protocol, typename SocketService, typename Iterator,
373  typename ConnectCondition, typename ComposedConnectHandler>
374 inline ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
375  void (asio::error_code, Iterator))
377  Iterator begin, ConnectCondition connect_condition,
378  ASIO_MOVE_ARG(ComposedConnectHandler) handler)
379 {
380  // If you get an error on the following line it means that your handler does
381  // not meet the documented type requirements for a ComposedConnectHandler.
383  ComposedConnectHandler, handler, Iterator) type_check;
384 
385  detail::async_result_init<ComposedConnectHandler,
386  void (asio::error_code, Iterator)> init(
387  ASIO_MOVE_CAST(ComposedConnectHandler)(handler));
388 
389  detail::connect_op<Protocol, SocketService, Iterator,
390  ConnectCondition, ASIO_HANDLER_TYPE(
391  ComposedConnectHandler, void (asio::error_code, Iterator))>(s,
392  begin, Iterator(), connect_condition, init.handler)(
393  asio::error_code(), 1);
394 
395  return init.result.get();
396 }
397 
398 template <typename Protocol, typename SocketService, typename Iterator,
399  typename ConnectCondition, typename ComposedConnectHandler>
400 inline ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
401  void (asio::error_code, Iterator))
403  Iterator begin, Iterator end, ConnectCondition connect_condition,
404  ASIO_MOVE_ARG(ComposedConnectHandler) handler)
405 {
406  // If you get an error on the following line it means that your handler does
407  // not meet the documented type requirements for a ComposedConnectHandler.
409  ComposedConnectHandler, handler, Iterator) type_check;
410 
411  detail::async_result_init<ComposedConnectHandler,
412  void (asio::error_code, Iterator)> init(
413  ASIO_MOVE_CAST(ComposedConnectHandler)(handler));
414 
415  detail::connect_op<Protocol, SocketService, Iterator,
416  ConnectCondition, ASIO_HANDLER_TYPE(
417  ComposedConnectHandler, void (asio::error_code, Iterator))>(s,
419  asio::error_code(), 1);
420 
421  return init.result.get();
422 }
423 
424 } // namespace asio
425 
427 
428 #endif // ASIO_IMPL_CONNECT_HPP
void asio_handler_deallocate(void *pointer, std::size_t size, binder1< Handler, Arg1 > *this_handler)
base_from_connect_condition(const ConnectCondition &connect_condition)
Definition: connect.hpp:140
Operation cancelled.
Definition: error.hpp:161
void check_condition(const asio::error_code &ec, Iterator &iter, Iterator &end)
Definition: connect.hpp:147
void throw_error(const asio::error_code &err)
Definition: throw_error.hpp:31
SocketService & s
Definition: connect.hpp:521
SocketService Iterator begin
Definition: connect.hpp:521
connect_op(basic_socket< Protocol, SocketService > &sock, const Iterator &begin, const Iterator &end, const ConnectCondition &connect_condition, ComposedConnectHandler &handler)
Definition: connect.hpp:179
void * asio_handler_allocate(std::size_t size, binder1< Handler, Arg1 > *this_handler)
basic_socket< Protocol, SocketService > & socket_
Definition: connect.hpp:261
asio::basic_streambuf< Allocator > MatchCondition enable_if< is_match_condition< MatchCondition >::value >::type *detail::async_result_init< ReadHandler, void(asio::error_code, std::size_t)> init(ASIO_MOVE_CAST(ReadHandler)(handler))
#define ASIO_HANDLER_TYPE(h, sig)
Iterator connect(basic_socket< Protocol, SocketService > &s, Iterator begin)
Establishes a socket connection by trying each endpoint in a sequence.
Definition: connect.hpp:44
void operator()(asio::error_code ec, int start=0)
Definition: connect.hpp:214
ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, void(asio::error_code, Iterator)) async_connect(basic_socket< Protocol
void invoke(Function &function, Context &context)
ComposedConnectHandler handler_
Definition: connect.hpp:265
Element not found.
Definition: error.hpp:220
Iterator operator()(const asio::error_code &, Iterator next)
Definition: connect.hpp:36
SocketService Iterator ConnectCondition connect_condition
Definition: connect.hpp:702
#define ASIO_COMPOSED_CONNECT_HANDLER_CHECK(handler_type, handler, iter_type)
void asio_handler_invoke(Function &function, binder1< Handler, Arg1 > *this_handler)
asio::basic_streambuf< Allocator > CompletionCondition ASIO_MOVE_ARG(ReadHandler) handler)
Definition: read.hpp:704
bool is_continuation(Context &context)
Class to represent an error code value.
Definition: error_code.hpp:80
void deallocate(void *p, std::size_t s, Handler &h)
void check_condition(const asio::error_code &, Iterator &, Iterator &)
Definition: connect.hpp:169
void close()
Close the socket.
void * allocate(std::size_t s, Handler &h)
handler_type< Handler, Signature >::type handler
Provides socket functionality.
SocketService Iterator Iterator end
Definition: connect.hpp:592
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
binder1< Handler, Arg1 > bind_handler(Handler handler, const Arg1 &arg1)
async_result< typename handler_type< Handler, Signature >::type > result
bool asio_handler_is_continuation(binder1< Handler, Arg1 > *this_handler)
void connect(const endpoint_type &peer_endpoint)
Connect the socket to the specified endpoint.