5#ifndef ICE_OUTGOING_ASYNC_H
6#define ICE_OUTGOING_ASYNC_H
22#ifdef ICE_CPP11_MAPPING
24# pragma clang diagnostic push
26# pragma clang diagnostic ignored "-Wshadow-uncaptured-local"
38class CollocatedRequestHandler;
40class ICE_API OutgoingAsyncCompletionCallback
43 virtual ~OutgoingAsyncCompletionCallback();
47 virtual bool handleSent(
bool,
bool) = 0;
49 virtual bool handleResponse(
bool) = 0;
51 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const = 0;
52 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const = 0;
53 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const = 0;
61class ICE_API OutgoingAsyncBase :
public virtual OutgoingAsyncCompletionCallback,
62#ifdef ICE_CPP11_MAPPING
63 public std::enable_shared_from_this<OutgoingAsyncBase>
65 public Ice::AsyncResult
72 virtual bool response();
74 void invokeSentAsync();
75 void invokeExceptionAsync();
76 void invokeResponseAsync();
79 void invokeException();
80 void invokeResponse();
82 virtual void cancelable(
const IceInternal::CancellationHandlerPtr&);
85#ifndef ICE_CPP11_MAPPING
88 virtual Ice::CommunicatorPtr getCommunicator()
const;
89 virtual Ice::ConnectionPtr getConnection()
const;
90 virtual Ice::ObjectPrx getProxy()
const;
93 virtual const std::string& getOperation()
const;
95 virtual bool isCompleted()
const;
96 virtual void waitForCompleted();
98 virtual bool isSent()
const;
99 virtual void waitForSent();
101 virtual bool sentSynchronously()
const;
103 virtual void throwLocalException()
const;
105 virtual bool _waitForResponse();
106 virtual Ice::InputStream* _startReadParams();
107 virtual void _endReadParams();
108 virtual void _readEmptyParams();
109 virtual void _readParamEncaps(const ::Ice::Byte*&,
::Ice::Int&);
110 virtual void _throwUserException();
112 virtual void _scheduleCallback(
const CallbackPtr&);
115 void attachRemoteObserver(
const Ice::ConnectionInfoPtr& c,
const Ice::EndpointPtr& endpt,
Ice::Int requestId)
118 _childObserver.attach(getObserver().getRemoteObserver(c, endpt, requestId, size));
121 void attachCollocatedObserver(
const Ice::ObjectAdapterPtr& adapter,
Ice::Int requestId)
124 _childObserver.attach(getObserver().getCollocatedObserver(adapter, requestId, size));
127 Ice::OutputStream* getOs()
132 Ice::InputStream* getIs()
139 OutgoingAsyncBase(
const InstancePtr&);
143 bool responseImpl(
bool,
bool);
145 void cancel(
const Ice::LocalException&);
146 void checkCanceled();
148 void warning(
const std::exception&)
const;
149 void warning()
const;
155 virtual IceInternal::InvocationObserver& getObserver()
160 const InstancePtr _instance;
161 Ice::ConnectionPtr _cachedConnection;
162 bool _sentSynchronously;
164 unsigned char _state;
166#ifdef ICE_CPP11_MAPPING
168 using Lock = std::lock_guard<std::mutex>;
170 IceUtil::Monitor<IceUtil::Mutex> _m;
171 typedef IceUtil::Monitor<IceUtil::Mutex>::Lock Lock;
175 IceInternal::UniquePtr<Ice::Exception> _ex;
176 IceInternal::UniquePtr<Ice::LocalException> _cancellationException;
178 InvocationObserver _observer;
179 ObserverHelperT<Ice::Instrumentation::ChildInvocationObserver> _childObserver;
181 Ice::OutputStream _os;
182 Ice::InputStream _is;
184 CancellationHandlerPtr _cancellationHandler;
186 static const unsigned char OK;
187 static const unsigned char Sent;
188#ifndef ICE_CPP11_MAPPING
189 static const unsigned char Done;
190 static const unsigned char EndCalled;
200class ICE_API ProxyOutgoingAsyncBase :
public OutgoingAsyncBase,
201 public IceUtil::TimerTask
205 virtual AsyncStatus invokeRemote(
const Ice::ConnectionIPtr&,
bool,
bool) = 0;
206 virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*) = 0;
209 virtual void cancelable(
const CancellationHandlerPtr&);
215#ifdef ICE_CPP11_MAPPING
216 std::shared_ptr<ProxyOutgoingAsyncBase> shared_from_this()
218 return std::static_pointer_cast<ProxyOutgoingAsyncBase>(OutgoingAsyncBase::shared_from_this());
221 virtual Ice::ObjectPrx getProxy()
const;
222 virtual Ice::CommunicatorPtr getCommunicator()
const;
227 ProxyOutgoingAsyncBase(
const Ice::ObjectPrxPtr&);
228 ~ProxyOutgoingAsyncBase();
230 void invokeImpl(
bool);
233 bool responseImpl(
bool,
bool);
235 virtual void runTimerTask();
237 const Ice::ObjectPrxPtr _proxy;
238 RequestHandlerPtr _handler;
250class ICE_API OutgoingAsync :
public ProxyOutgoingAsyncBase
254 OutgoingAsync(
const Ice::ObjectPrxPtr&,
bool);
259 virtual bool response();
261 virtual AsyncStatus invokeRemote(
const Ice::ConnectionIPtr&,
bool,
bool);
262 virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*);
265 void invoke(
const std::string&);
266#ifdef ICE_CPP11_MAPPING
268 std::function<
void(Ice::OutputStream*)>);
269 void throwUserException();
274 _os.startEncapsulation(_encoding, format);
277 void endWriteParams()
279 _os.endEncapsulation();
281 void writeEmptyParams()
283 _os.writeEmptyEncapsulation(_encoding);
285 void writeParamEncaps(const ::Ice::Byte* encaps,
::Ice::Int size)
289 _os.writeEmptyEncapsulation(_encoding);
293 _os.writeEncapsulation(encaps, size);
299 const Ice::EncodingVersion _encoding;
301#ifdef ICE_CPP11_MAPPING
302 std::function<void(const ::Ice::UserException&)> _userException;
313#ifdef ICE_CPP11_MAPPING
315class ICE_API LambdaInvoke :
public virtual OutgoingAsyncCompletionCallback
319 LambdaInvoke(std::function<
void(::std::exception_ptr)> exception, std::function<
void(
bool)> sent) :
320 _exception(std::move(exception)), _sent(std::move(sent))
326 virtual bool handleSent(
bool,
bool)
override;
328 virtual bool handleResponse(
bool)
override;
330 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const override;
331 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const override;
332 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const override;
334 std::function<void(::std::exception_ptr)> _exception;
335 std::function<void(
bool)> _sent;
336 std::function<void(
bool)> _response;
339template<
typename Promise>
340class PromiseInvoke :
public virtual OutgoingAsyncCompletionCallback
345 getFuture() ->
decltype(std::declval<Promise>().get_future())
347 return _promise.get_future();
353 std::function<void(
bool)> _response;
357 virtual bool handleSent(
bool,
bool)
override
370 _promise.set_exception(std::current_exception());
375 virtual bool handleResponse(
bool ok)
override
381 virtual void handleInvokeSent(
bool, OutgoingAsyncBase*)
const override
386 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase*)
const override
391 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase*)
const override
398class OutgoingAsyncT :
public OutgoingAsync
402 using OutgoingAsync::OutgoingAsync;
405 invoke(
const std::string& operation,
409 std::function<
void(Ice::OutputStream*)> write,
410 std::function<
void(
const Ice::UserException&)> userException)
412 _read = [](Ice::InputStream* stream)
418 _userException = std::move(userException);
419 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
423 invoke(
const std::string& operation,
427 std::function<
void(Ice::OutputStream*)> write,
428 std::function<
void(
const Ice::UserException&)> userException,
429 std::function<T(Ice::InputStream*)> read)
431 _read = std::move(read);
432 _userException = std::move(userException);
433 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
438 std::function<T(Ice::InputStream*)> _read;
442class OutgoingAsyncT<void> :
public OutgoingAsync
446 using OutgoingAsync::OutgoingAsync;
449 invoke(
const std::string& operation,
453 std::function<
void(Ice::OutputStream*)> write,
454 std::function<
void(
const Ice::UserException&)> userException)
456 _userException = std::move(userException);
457 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
462class LambdaOutgoing :
public OutgoingAsyncT<R>,
public LambdaInvoke
466 LambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
467 std::function<
void(R)> response,
468 std::function<
void(::std::exception_ptr)> ex,
469 std::function<
void(
bool)> sent) :
470 OutgoingAsyncT<R>(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
472#if ICE_CPLUSPLUS >= 201402L
474 _response = [
this, response = std::move(response)](
bool ok)
476 _response = [
this, response](
bool ok)
481 this->throwUserException();
486 this->_is.startEncapsulation();
487 R v = this->_read(&this->_is);
488 this->_is.endEncapsulation();
491 response(std::move(v));
495 throw std::current_exception();
503class LambdaOutgoing<void> :
public OutgoingAsyncT<void>,
public LambdaInvoke
507 LambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
508 std::function<
void()> response,
509 std::function<
void(::std::exception_ptr)> ex,
510 std::function<
void(
bool)> sent) :
511 OutgoingAsyncT<void>(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
513#if ICE_CPLUSPLUS >= 201402L
515 _response = [
this, response = std::move(response)](
bool ok)
517 _response = [
this, response](
bool ok)
522 this->throwUserException();
526 if(!this->_is.b.empty())
528 this->_is.skipEmptyEncapsulation();
537 throw std::current_exception();
544class CustomLambdaOutgoing :
public OutgoingAsync,
public LambdaInvoke
548 CustomLambdaOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
549 std::function<
void(Ice::InputStream*)> read,
550 std::function<
void(::std::exception_ptr)> ex,
551 std::function<
void(
bool)> sent) :
552 OutgoingAsync(proxy, false), LambdaInvoke(std::move(ex), std::move(sent))
554#if ICE_CPLUSPLUS >= 201402L
556 _response = [
this, read = std::move(read)](
bool ok)
558 _response = [
this, read](
bool ok)
563 this->throwUserException();
576 invoke(
const std::string& operation,
580 std::function<
void(Ice::OutputStream*)> write,
581 std::function<
void(
const Ice::UserException&)> userException)
583 _userException = std::move(userException);
584 OutgoingAsync::invoke(operation, mode, format, ctx, std::move(write));
588template<
typename P,
typename R>
589class PromiseOutgoing :
public OutgoingAsyncT<R>,
public PromiseInvoke<P>
593 PromiseOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
bool sync) :
594 OutgoingAsyncT<R>(proxy, sync)
596 this->_response = [
this](
bool ok)
601 this->_is.startEncapsulation();
602 R v = this->_read(&this->_is);
603 this->_is.endEncapsulation();
604 this->_promise.set_value(std::move(v));
608 this->throwUserException();
615class PromiseOutgoing<P, void> :
public OutgoingAsyncT<void>,
public PromiseInvoke<P>
619 PromiseOutgoing(
const std::shared_ptr<Ice::ObjectPrx>& proxy,
bool sync) :
620 OutgoingAsyncT<void>(proxy, sync)
622 this->_response = [&](
bool ok)
624 if(this->_is.b.empty())
631 this->_promise.set_value();
635 this->_is.skipEmptyEncapsulation();
636 this->_promise.set_value();
640 this->throwUserException();
645 virtual bool handleSent(
bool done,
bool)
override
649 PromiseInvoke<P>::_promise.set_value();
660class ICE_API CallbackBase :
public IceUtil::Shared
664 virtual ~CallbackBase();
666 void checkCallback(
bool,
bool);
668 virtual void completed(const ::Ice::AsyncResultPtr&)
const = 0;
669 virtual IceUtil::Handle<CallbackBase> verify(const ::Ice::LocalObjectPtr&) = 0;
670 virtual void sent(const ::Ice::AsyncResultPtr&)
const = 0;
671 virtual bool hasSentCallback()
const = 0;
673typedef IceUtil::Handle<CallbackBase> CallbackBasePtr;
678class ICE_API GenericCallbackBase :
public virtual CallbackBase
682 virtual ~GenericCallbackBase();
695class AsyncCallback :
public GenericCallbackBase
699 typedef T callback_type;
700 typedef IceUtil::Handle<T> TPtr;
702 typedef void (T::*Callback)(const ::Ice::AsyncResultPtr&);
704 AsyncCallback(
const TPtr& instance, Callback cb, Callback sentcb = 0) :
705 _callback(instance), _completed(cb), _sent(sentcb)
707 checkCallback(instance, cb != 0);
710 virtual void completed(const ::Ice::AsyncResultPtr& result)
const
712 (_callback.get()->*_completed)(result);
715 virtual CallbackBasePtr verify(const ::Ice::LocalObjectPtr&)
720 virtual void sent(const ::Ice::AsyncResultPtr& result)
const
724 (_callback.get()->*_sent)(result);
728 virtual bool hasSentCallback()
const
740class CallbackCompletion :
public virtual OutgoingAsyncCompletionCallback
744 CallbackCompletion(
const CallbackBasePtr& cb,
const Ice::LocalObjectPtr& cookie) : _callback(cb)
748 throw IceUtil::IllegalArgumentException(__FILE__, __LINE__);
750 const_cast<CallbackBasePtr&
>(_callback) = _callback->verify(cookie);
753 virtual bool handleSent(
bool,
bool alreadySent)
755 return _callback && _callback->hasSentCallback() && !alreadySent;
763 virtual bool handleResponse(
bool)
768 virtual void handleInvokeSent(
bool, OutgoingAsyncBase* outAsync)
const
770 _callback->sent(outAsync);
773 virtual void handleInvokeException(
const Ice::Exception&, OutgoingAsyncBase* outAsync)
const
775 _callback->completed(outAsync);
778 virtual void handleInvokeResponse(
bool, OutgoingAsyncBase* outAsync)
const
780 _callback->completed(outAsync);
785 const CallbackBasePtr _callback;
788class CallbackOutgoing :
public OutgoingAsync,
public CallbackCompletion
792 CallbackOutgoing(
const Ice::ObjectPrx& proxy,
793 const std::string& operation,
794 const CallbackBasePtr& cb,
797 OutgoingAsync(proxy, sync), CallbackCompletion(cb, cookie), _operation(operation)
802 virtual const std::string&
810 const std::string& _operation;
817#ifndef ICE_CPP11_MAPPING
822typedef IceUtil::Handle< ::IceInternal::GenericCallbackBase> CallbackPtr;
831template<
class T> CallbackPtr
832newCallback(
const IceUtil::Handle<T>& instance,
833 void (T::*cb)(
const AsyncResultPtr&),
834 void (T::*sentcb)(
const AsyncResultPtr&) = 0)
836 return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);
846template<
class T> CallbackPtr
847newCallback(T* instance,
848 void (T::*cb)(
const AsyncResultPtr&),
849 void (T::*sentcb)(
const AsyncResultPtr&) = 0)
851 return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);
862#ifdef ICE_CPP11_MAPPING
864# pragma clang diagnostic pop
#define ICE_API
Definition Config.h:197
#define ICE_GLOBAL_VAR_SUFFIX
Definition Config.h:185
virtual void ice_throw() const =0
Throws this exception.
Definition BuiltinSequences.h:56
int Int
The mapping for the Slice int type.
Definition Config.h:54
OperationMode
Determines the retry behavior an invocation in case of a (potentially) recoverable error.
Definition Current.h:74
::std::map<::std::string, ::std::string > Context
A request context.
Definition Current.h:68
IceInternal::Handle< LocalObject > LocalObjectPtr
Definition LocalObjectF.h:17
FormatType
Describes the possible formats for classes and exceptions.
Definition Format.h:21
IceUtil::Exception Exception
Definition Exception.h:22