24#include <boost/bind/placeholders.hpp>
25#include <boost/scope_exit.hpp>
26#include <boost/system/error_code.hpp>
27#include <boost/bind/bind.hpp>
37namespace RobotRaconteur
44class sync_async_handler :
private boost::noncopyable
47 RR_SHARED_PTR<AutoResetEvent> ev;
48 RR_SHARED_PTR<RobotRaconteurException> err;
49 RR_SHARED_PTR<T> data;
50 boost::mutex data_lock;
52 sync_async_handler() { ev = RR_MAKE_SHARED<AutoResetEvent>(); }
54 sync_async_handler(
const RR_SHARED_PTR<RobotRaconteurException>& err)
56 ev = RR_MAKE_SHARED<AutoResetEvent>();
60 void operator()() { ev->Set(); }
62 void operator()(
const RR_SHARED_PTR<RobotRaconteurException>& err)
64 boost::mutex::scoped_lock lock(data_lock);
69 void operator()(
const RR_SHARED_PTR<T>& data,
const RR_SHARED_PTR<RobotRaconteurException>& err)
71 boost::mutex::scoped_lock lock(data_lock);
77 RR_SHARED_PTR<T> end()
81 boost::mutex::scoped_lock lock(data_lock);
84 RobotRaconteurExceptionUtil::DownCastAndThrowException(err);
97 boost::mutex::scoped_lock lock(data_lock);
100 RobotRaconteurExceptionUtil::DownCastAndThrowException(err);
104 bool try_end(RR_SHARED_PTR<T>& res, RR_SHARED_PTR<RobotRaconteurException>& err_out)
108 boost::mutex::scoped_lock lock(data_lock);
117 err_out = RR_MAKE_SHARED<InternalErrorException>(
"Internal async error");
125 bool try_end_void(RR_SHARED_PTR<RobotRaconteurException>& err_out)
129 boost::mutex::scoped_lock lock(data_lock);
140RR_SHARED_PTR<Timer> async_timeout_wrapper_CreateTimer(
const RR_SHARED_PTR<RobotRaconteurNode>& node,
141 const boost::posix_time::time_duration& period,
142 RR_MOVE_ARG(boost::function<
void(
const TimerEvent&)>) handler,
146void async_timeout_wrapper_closer(
const RR_SHARED_PTR<T>& d)
152 catch (std::exception&)
156template <
typename T,
typename T2>
157void async_timeout_wrapper_closer(
const RR_SHARED_PTR<T>& d)
161 RR_SHARED_PTR<T2> t2 = RR_DYNAMIC_POINTER_CAST<T2>(d);
166 catch (std::exception&)
171class async_timeout_wrapper :
public RR_ENABLE_SHARED_FROM_THIS<async_timeout_wrapper<T> >,
private boost::noncopyable
174 boost::function<void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)> handler_;
175 RR_SHARED_PTR<Timer> timeout_timer_;
176 boost::mutex handled_lock;
178 RR_SHARED_PTR<RobotRaconteurException> timeout_exception_;
179 boost::function<void(
const RR_SHARED_PTR<T>&)> deleter_;
180 RR_WEAK_PTR<RobotRaconteurNode> node;
183 async_timeout_wrapper(
184 const RR_SHARED_PTR<RobotRaconteurNode>& node,
185 boost::function<
void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
186 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<T>&)>) deleter = 0)
187 : handler_(handler), handled(false), node(node)
189 deleter_.swap(deleter);
192 void start_timer(int32_t timeout,
const RR_SHARED_PTR<RobotRaconteurException>& timeout_exception =
193 RR_MAKE_SHARED<ConnectionException>(
"Timeout during operation"));
195 void operator()(
const RR_SHARED_PTR<T>& data,
const RR_SHARED_PTR<RobotRaconteurException>& err)
198 boost::mutex::scoped_lock lock(handled_lock);
201 if (data && deleter_)
208 timeout_timer_->TryStop();
210 timeout_timer_.reset();
216 void handle_error(
const RR_SHARED_PTR<RobotRaconteurException>& err)
219 boost::mutex::scoped_lock lock(handled_lock);
227 timeout_timer_->TryStop();
229 timeout_timer_.reset();
233 handler_(RR_SHARED_PTR<T>(), err);
236 void handle_error(
const boost::system::error_code& err)
238 if (err.value() == boost::system::errc::timed_out)
239 handle_error(timeout_exception_);
241 handle_error(RR_MAKE_SHARED<ConnectionException>(err.message()));
245 void timeout_handler(
const TimerEvent& )
248 boost::mutex::scoped_lock lock(handled_lock);
253 timeout_timer_.reset();
256 handler_(RR_SHARED_PTR<T>(), timeout_exception_);
261void async_timeout_wrapper<T>::start_timer(int32_t timeout,
262 const RR_SHARED_PTR<RobotRaconteurException>& timeout_exception)
264 RR_SHARED_PTR<RobotRaconteurNode> n = node.lock();
268 boost::mutex::scoped_lock lock(handled_lock);
275 timeout_exception_ = RR_MOVE(timeout_exception);
277 async_timeout_wrapper_CreateTimer(n, boost::posix_time::milliseconds(timeout),
278 boost::bind(&async_timeout_wrapper<T>::timeout_handler,
279 this->shared_from_this(), RR_BOOST_PLACEHOLDERS(_1)),
281 timeout_timer_->Start();
285class ROBOTRACONTEUR_CORE_API async_signal_semaphore :
private boost::noncopyable
288 boost::mutex this_lock;
289 boost::condition_variable next_wait;
290 boost::initialized<bool> running;
291 boost::initialized<bool> next;
292 boost::initialized<uint64_t> next_id;
295 template <
typename F>
296 bool try_fire_next(BOOST_ASIO_MOVE_ARG(F) h)
298 boost::mutex::scoped_lock lock(this_lock);
302 uint64_t my_id = ++next_id.data();
306 next_wait.notify_all();
315 next_wait.wait(lock);
316 if (my_id != next_id)
323 running.data() =
true;
325 BOOST_SCOPE_EXIT_TPL(this_)
327 boost::mutex::scoped_lock lock2(this_->this_lock);
328 this_->running.data() =
false;
329 this_->next_wait.notify_all();
331 BOOST_SCOPE_EXIT_END;
341class ROBOTRACONTEUR_CORE_API async_signal_pool_semaphore
342 :
public RR_ENABLE_SHARED_FROM_THIS<async_signal_pool_semaphore>,
343 private boost::noncopyable
346 boost::mutex this_lock;
347 boost::initialized<bool> running;
348 boost::function<void()> next;
349 RR_WEAK_PTR<RobotRaconteurNode> node;
352 async_signal_pool_semaphore(
const RR_SHARED_PTR<RobotRaconteurNode>& node) : node(node) {}
354 template <
typename F>
355 void try_fire_next(BOOST_ASIO_MOVE_ARG(F) h)
357 boost::mutex::scoped_lock lock(this_lock);
361 RR_SHARED_PTR<RobotRaconteurNode> node1 = this->node.lock();
373 void do_fire_next(
const boost::function<
void()>& h)
379 catch (std::exception& exp)
381 handle_exception(&exp);
384 BOOST_SCOPE_EXIT(this_)
386 boost::mutex::scoped_lock lock2(this_->this_lock);
388 boost::function<void()> h2;
389 h2.swap(this_->next);
391 this_->running.data() =
false;
394 RR_SHARED_PTR<RobotRaconteurNode> node = this_->node.lock();
400 boost::bind(&async_signal_pool_semaphore::do_fire_next, this_->shared_from_this(), h2));
402 catch (std::exception&)
404 this_->running.data() =
true;
406 BOOST_SCOPE_EXIT_END;
409 void do_post(
const RR_SHARED_PTR<RobotRaconteurNode>& node1, RR_MOVE_ARG(boost::function<
void()>) h);
411 void handle_exception(std::exception* exp);
414ROBOTRACONTEUR_CORE_API
void InvokeHandler_HandleException(RR_WEAK_PTR<RobotRaconteurNode> node, std::exception& exp);
416ROBOTRACONTEUR_CORE_API
void InvokeHandler_DoPost(RR_WEAK_PTR<RobotRaconteurNode> node,
417 const boost::function<
void()>& h,
bool shutdown_op =
false,
418 bool throw_on_released =
true);
420ROBOTRACONTEUR_CORE_API
void InvokeHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
421 const boost::function<
void()>& handler);
424void InvokeHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
const typename boost::function<
void(
const T&)>& handler,
431 catch (std::exception& exp)
433 InvokeHandler_HandleException(RR_MOVE(node), exp);
437ROBOTRACONTEUR_CORE_API
void InvokeHandler(
438 RR_WEAK_PTR<RobotRaconteurNode> node,
439 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
441ROBOTRACONTEUR_CORE_API
void InvokeHandlerWithException(
442 RR_WEAK_PTR<RobotRaconteurNode> node,
443 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
444 const RR_SHARED_PTR<RobotRaconteurException>& exp);
446ROBOTRACONTEUR_CORE_API
void InvokeHandlerWithException(
447 RR_WEAK_PTR<RobotRaconteurNode> node,
448 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler, std::exception& exp,
449 MessageErrorType default_err = MessageErrorType_UnknownError);
453 RR_WEAK_PTR<RobotRaconteurNode> node,
454 const typename boost::function<
void(
const T&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
459 handler(value, RR_SHARED_PTR<RobotRaconteurException>());
461 catch (std::exception& exp)
463 InvokeHandler_HandleException(RR_MOVE(node), exp);
468void InvokeHandlerWithException(
469 RR_WEAK_PTR<RobotRaconteurNode> node,
470 const typename boost::function<
void(T,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
471 const RR_SHARED_PTR<RobotRaconteurException>& exp)
473 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
476 handler(default_value, exp);
478 catch (std::exception& exp)
480 InvokeHandler_HandleException(RR_MOVE(node), exp);
485void InvokeHandlerWithException(
486 RR_WEAK_PTR<RobotRaconteurNode> node,
487 const typename boost::function<
void(T,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
488 std::exception& exp, MessageErrorType default_err = MessageErrorType_UnknownError)
490 typename boost::initialized<typename boost::remove_const<typename boost::remove_reference<T>::type>::type>
494 RR_SHARED_PTR<RobotRaconteurException> err =
495 RobotRaconteurExceptionUtil::ExceptionToSharedPtr(exp, default_err);
496 handler(default_value, err);
498 catch (std::exception& exp)
500 InvokeHandler_HandleException(RR_MOVE(node), exp);
504ROBOTRACONTEUR_CORE_API
void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
const boost::function<
void()>& handler,
505 bool shutdown_op =
false,
bool throw_on_released =
true);
508void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
const typename boost::function<
void(
const T&)>& handler,
509 const T& value,
bool shutdown_op =
false,
bool throw_on_released =
true)
511 boost::function<void()> h = boost::bind(handler, value);
512 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
515ROBOTRACONTEUR_CORE_API
void PostHandler(
516 RR_WEAK_PTR<RobotRaconteurNode> node,
517 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
bool shutdown_op =
false,
518 bool throw_on_released =
true);
520ROBOTRACONTEUR_CORE_API
void PostHandlerWithException(
521 RR_WEAK_PTR<RobotRaconteurNode> node,
522 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
523 const RR_SHARED_PTR<RobotRaconteurException>& exp,
bool shutdown_op =
false,
bool throw_on_released =
true);
525ROBOTRACONTEUR_CORE_API
void PostHandlerWithException(
526 RR_WEAK_PTR<RobotRaconteurNode> node,
527 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler, std::exception& exp,
528 MessageErrorType default_err = MessageErrorType_UnknownError,
bool shutdown_op =
false,
529 bool throw_on_released =
true);
532void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
533 const typename boost::function<
void(
const T&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
534 const T& value,
bool shutdown_op =
false,
bool throw_on_released =
true)
536 boost::function<void()> h = boost::bind(handler, value, RR_SHARED_PTR<RobotRaconteurException>());
537 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
541void PostHandlerWithException(
542 RR_WEAK_PTR<RobotRaconteurNode> node,
543 const typename boost::function<
void(T,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
544 const RR_SHARED_PTR<RobotRaconteurException>& exp,
bool shutdown_op =
false,
bool throw_on_released =
true)
546 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
547 boost::function<void()> h = boost::bind(handler, default_value, exp);
548 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
552void PostHandlerWithException(
553 RR_WEAK_PTR<RobotRaconteurNode> node,
554 const typename boost::function<
void(T,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
555 std::exception& exp, MessageErrorType default_err = MessageErrorType_UnknownError,
bool shutdown_op =
false,
556 bool throw_on_released =
true)
558 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
559 RR_SHARED_PTR<RobotRaconteurException> err = RobotRaconteurExceptionUtil::ExceptionToSharedPtr(exp, default_err);
560 boost::function<void()> h = boost::bind(handler, default_value, err);
561 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition RobotRaconteurConstants.h:566
InternalErrorException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > ¶m_=boost::intrusive_ptr< RRValue >())
Construct an InternalErrorException.
InvalidOperationException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > ¶m_=boost::intrusive_ptr< RRValue >())
Construct an InvalidOperationException.
The central node implementation.
Definition RobotRaconteurNode.h:132