5 #ifndef ICE_OUTGOING_ASYNC_H
6 #define ICE_OUTGOING_ASYNC_H
22 #ifndef ICE_CPP11_MAPPING
32 class CollocatedRequestHandler;
34 class ICE_API OutgoingAsyncCompletionCallback
37 virtual ~OutgoingAsyncCompletionCallback();
41 virtual bool handleSent(
bool,
bool) = 0;
43 virtual bool handleResponse(
bool) = 0;
45 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const = 0;
46 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const = 0;
47 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const = 0;
55 class ICE_API OutgoingAsyncBase :
public virtual OutgoingAsyncCompletionCallback,
56 #ifdef ICE_CPP11_MAPPING
57 public std::enable_shared_from_this<OutgoingAsyncBase>
59 public Ice::AsyncResult
66 virtual bool response();
68 void invokeSentAsync();
69 void invokeExceptionAsync();
70 void invokeResponseAsync();
73 void invokeException();
74 void invokeResponse();
76 virtual void cancelable(
const IceInternal::CancellationHandlerPtr&);
79 #ifndef ICE_CPP11_MAPPING
82 virtual Ice::CommunicatorPtr getCommunicator()
const;
83 virtual Ice::ConnectionPtr getConnection()
const;
87 virtual const std::string& getOperation()
const;
89 virtual bool isCompleted()
const;
90 virtual void waitForCompleted();
92 virtual bool isSent()
const;
93 virtual void waitForSent();
95 virtual bool sentSynchronously()
const;
97 virtual void throwLocalException()
const;
99 virtual bool _waitForResponse();
101 virtual void _endReadParams();
102 virtual void _readEmptyParams();
104 virtual void _throwUserException();
106 virtual void _scheduleCallback(
const CallbackPtr&);
109 void attachRemoteObserver(
const Ice::ConnectionInfoPtr& c,
const Ice::EndpointPtr& endpt,
Ice::Int requestId)
112 _childObserver.attach(getObserver().getRemoteObserver(c, endpt, requestId, size));
115 void attachCollocatedObserver(
const Ice::ObjectAdapterPtr& adapter,
Ice::Int requestId)
118 _childObserver.attach(getObserver().getCollocatedObserver(adapter, requestId, size));
133 OutgoingAsyncBase(
const InstancePtr&);
137 bool responseImpl(
bool,
bool);
140 void checkCanceled();
142 void warning(
const std::exception&)
const;
143 void warning()
const;
149 virtual IceInternal::InvocationObserver& getObserver()
154 const InstancePtr _instance;
155 Ice::ConnectionPtr _cachedConnection;
156 bool _sentSynchronously;
158 unsigned char _state;
160 #ifdef ICE_CPP11_MAPPING
162 using Lock = std::lock_guard<std::mutex>;
169 IceInternal::UniquePtr<Ice::Exception> _ex;
170 IceInternal::UniquePtr<Ice::LocalException> _cancellationException;
172 InvocationObserver _observer;
173 ObserverHelperT<Ice::Instrumentation::ChildInvocationObserver> _childObserver;
178 CancellationHandlerPtr _cancellationHandler;
180 static const unsigned char OK;
181 static const unsigned char Sent;
182 #ifndef ICE_CPP11_MAPPING
183 static const unsigned char Done;
184 static const unsigned char EndCalled;
194 class ICE_API ProxyOutgoingAsyncBase :
public OutgoingAsyncBase,
199 virtual AsyncStatus invokeRemote(
const Ice::ConnectionIPtr&,
bool,
bool) = 0;
200 virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*) = 0;
203 virtual void cancelable(
const CancellationHandlerPtr&);
209 #ifdef ICE_CPP11_MAPPING
210 std::shared_ptr<ProxyOutgoingAsyncBase> shared_from_this()
212 return std::static_pointer_cast<ProxyOutgoingAsyncBase>(OutgoingAsyncBase::shared_from_this());
216 virtual Ice::CommunicatorPtr getCommunicator()
const;
221 ProxyOutgoingAsyncBase(
const Ice::ObjectPrxPtr&);
222 ~ProxyOutgoingAsyncBase();
224 void invokeImpl(
bool);
227 bool responseImpl(
bool,
bool);
229 virtual void runTimerTask();
231 const Ice::ObjectPrxPtr _proxy;
232 RequestHandlerPtr _handler;
244 class ICE_API OutgoingAsync :
public ProxyOutgoingAsyncBase
248 OutgoingAsync(
const Ice::ObjectPrxPtr&,
bool);
253 virtual bool response();
255 virtual AsyncStatus invokeRemote(
const Ice::ConnectionIPtr&,
bool,
bool);
256 virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*);
259 void invoke(
const std::string&);
260 #ifdef ICE_CPP11_MAPPING
263 void throwUserException();
271 void endWriteParams()
273 _os.endEncapsulation();
275 void writeEmptyParams()
277 _os.writeEmptyEncapsulation(_encoding);
283 _os.writeEmptyEncapsulation(_encoding);
287 _os.writeEncapsulation(encaps, size);
295 #ifdef ICE_CPP11_MAPPING
296 std::function<void(const ::Ice::UserException&)> _userException;
304 namespace IceInternal
307 #ifdef ICE_CPP11_MAPPING
309 class ICE_API LambdaInvoke :
public virtual OutgoingAsyncCompletionCallback
313 LambdaInvoke(std::function<
void(::std::exception_ptr)> exception, std::function<
void(
bool)> sent) :
314 _exception(std::move(exception)), _sent(std::move(sent))
320 virtual bool handleSent(
bool,
bool)
override;
322 virtual bool handleResponse(
bool)
override;
324 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const override;
325 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const override;
326 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const override;
328 std::function<void(::std::exception_ptr)> _exception;
329 std::function<void(
bool)> _sent;
330 std::function<void(
bool)> _response;
333 template<
typename Promise>
334 class PromiseInvoke :
public virtual OutgoingAsyncCompletionCallback
339 getFuture() -> decltype(std::declval<Promise>().get_future())
341 return _promise.get_future();
347 std::function<void(
bool)> _response;
351 virtual bool handleSent(
bool,
bool)
override
364 _promise.set_exception(std::current_exception());
369 virtual bool handleResponse(
bool ok)
override
375 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const override
380 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const override
385 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const override
392 class OutgoingAsyncT :
public OutgoingAsync
396 using OutgoingAsync::OutgoingAsync;
399 invoke(
const std::string& operation,
412 _userException = std::move(userException);
413 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
417 invoke(
const std::string& operation,
425 _read = std::move(read);
426 _userException = std::move(userException);
427 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
436 class OutgoingAsyncT<void> :
public OutgoingAsync
440 using OutgoingAsync::OutgoingAsync;
443 invoke(
const std::string& operation,
450 _userException = std::move(userException);
451 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
456 class LambdaOutgoing :
public OutgoingAsyncT<R>,
public LambdaInvoke
460 LambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
461 std::function<
void(R)> response,
462 std::function<
void(::std::exception_ptr)> ex,
463 std::function<
void(
bool)> sent) :
464 OutgoingAsyncT<R>(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
466 #if ICE_CPLUSPLUS >= 201402L
468 _response = [
this, response = std::move(response)](
bool ok)
470 _response = [
this, response](
bool ok)
475 this->throwUserException();
480 this->_is.startEncapsulation();
481 R v = this->_read(&this->_is);
482 this->_is.endEncapsulation();
485 response(std::move(v));
489 throw std::current_exception();
497 class LambdaOutgoing<void> :
public OutgoingAsyncT<void>,
public LambdaInvoke
501 LambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
502 std::function<
void()> response,
503 std::function<
void(::std::exception_ptr)> ex,
504 std::function<
void(
bool)> sent) :
505 OutgoingAsyncT<void>(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
507 #if ICE_CPLUSPLUS >= 201402L
509 _response = [
this, response = std::move(response)](
bool ok)
511 _response = [
this, response](
bool ok)
516 this->throwUserException();
520 if(!this->_is.b.empty())
522 this->_is.skipEmptyEncapsulation();
531 throw std::current_exception();
538 class CustomLambdaOutgoing :
public OutgoingAsync,
public LambdaInvoke
542 CustomLambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
544 std::function<
void(::std::exception_ptr)> ex,
545 std::function<
void(
bool)> sent) :
546 OutgoingAsync(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
548 #if ICE_CPLUSPLUS >= 201402L
550 _response = [
this, read = std::move(read)](
bool ok)
552 _response = [
this, read](
bool ok)
557 this->throwUserException();
570 invoke(
const std::string& operation,
577 _userException = std::move(userException);
578 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
582 template<
typename P,
typename R>
583 class PromiseOutgoing :
public OutgoingAsyncT<R>,
public PromiseInvoke<P>
587 PromiseOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
bool sync) :
588 OutgoingAsyncT<R>(proxy, sync)
590 this->_response = [
this](
bool ok)
595 this->_is.startEncapsulation();
596 R v = this->_read(&this->_is);
597 this->_is.endEncapsulation();
598 this->_promise.set_value(std::move(v));
602 this->throwUserException();
609 class PromiseOutgoing<P, void> :
public OutgoingAsyncT<void>,
public PromiseInvoke<P>
613 PromiseOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
bool sync) :
614 OutgoingAsyncT<void>(proxy, sync)
616 this->_response = [&](
bool ok)
618 if(this->_is.b.empty())
625 this->_promise.set_value();
629 this->_is.skipEmptyEncapsulation();
630 this->_promise.set_value();
634 this->throwUserException();
639 virtual bool handleSent(
bool done,
bool)
override
643 PromiseInvoke<P>::_promise.set_value();
658 virtual ~CallbackBase();
660 void checkCallback(
bool,
bool);
662 virtual void completed(const ::Ice::AsyncResultPtr&)
const = 0;
664 virtual void sent(const ::Ice::AsyncResultPtr&)
const = 0;
665 virtual bool hasSentCallback()
const = 0;
672 class ICE_API GenericCallbackBase :
public virtual CallbackBase
676 virtual ~GenericCallbackBase();
689 class AsyncCallback :
public GenericCallbackBase
693 typedef T callback_type;
696 typedef void (T::*Callback)(const ::Ice::AsyncResultPtr&);
698 AsyncCallback(
const TPtr& instance, Callback cb, Callback sentcb = 0) :
699 _callback(instance), _completed(cb), _sent(sentcb)
701 checkCallback(instance, cb != 0);
704 virtual void completed(const ::Ice::AsyncResultPtr& result)
const
706 (_callback.get()->*_completed)(result);
714 virtual void sent(const ::Ice::AsyncResultPtr& result)
const
718 (_callback.get()->*_sent)(result);
722 virtual bool hasSentCallback()
const
734 class CallbackCompletion :
public virtual OutgoingAsyncCompletionCallback
738 CallbackCompletion(
const CallbackBasePtr& cb,
const Ice::LocalObjectPtr& cookie) : _callback(cb)
744 const_cast<CallbackBasePtr&
>(_callback) = _callback->verify(cookie);
747 virtual bool handleSent(
bool,
bool alreadySent)
749 return _callback && _callback->hasSentCallback() && !alreadySent;
757 virtual bool handleResponse(
bool)
762 virtual void handleInvokeSent(
bool, OutgoingAsyncBase* outAsync)
const
764 _callback->sent(outAsync);
767 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase* outAsync)
const
769 _callback->completed(outAsync);
772 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase* outAsync)
const
774 _callback->completed(outAsync);
779 const CallbackBasePtr _callback;
782 class CallbackOutgoing :
public OutgoingAsync,
public CallbackCompletion
787 const std::string& operation,
788 const CallbackBasePtr& cb,
791 OutgoingAsync(proxy, sync), CallbackCompletion(cb, cookie), _operation(operation)
796 virtual const std::string&
804 const std::string& _operation;
811 #ifndef ICE_CPP11_MAPPING
825 template<
class T> CallbackPtr
827 void (T::*cb)(
const AsyncResultPtr&),
828 void (T::*sentcb)(
const AsyncResultPtr&) = 0)
830 return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);
840 template<
class T> CallbackPtr
841 newCallback(T* instance,
842 void (T::*cb)(
const AsyncResultPtr&),
843 void (T::*sentcb)(
const AsyncResultPtr&) = 0)
845 return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);