ice async

来源:互联网 发布:最火的网络电脑游戏荒 编辑:程序博客网 时间:2024/04/28 00:31

一 使用ice接口规范 定义接口

["amd"]  Asynchronous Message Dispatch (AMD) 对应的还有 Asynchronous Message Invocation (AMI)

idempotent

exception 使用这个关键字可以定义自己的异常类

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#pragma oncemodule Demo{exception RequestCanceledException{};interface Hello{    ["amd"] idempotent void sayHello(int delay)        throws RequestCanceledException;    void shutdown();};};

二 Slice2cpp 生成的文件

Hello.h
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// This copy of Ice is licensed to you under the terms described in the// ICE_LICENSE file included in this distribution.//// **********************************************************************//// Ice version 3.6.2//// <auto-generated>//// Generated from file `Hello.ice'//// Warning: do not edit this file.//// </auto-generated>//#ifndef __Hello_h__#define __Hello_h__#include <IceUtil/PushDisableWarnings.h>#include <Ice/ProxyF.h>#include <Ice/ObjectF.h>#include <Ice/Exception.h>#include <Ice/LocalObject.h>#include <Ice/StreamHelpers.h>#include <Ice/Proxy.h>#include <Ice/GCObject.h>#include <Ice/AsyncResult.h>#include <Ice/Incoming.h>#include <Ice/IncomingAsync.h>#include <Ice/FactoryTableInit.h>#include <IceUtil/ScopedArray.h>#include <IceUtil/Optional.h>#include <Ice/StreamF.h>#include <IceUtil/UndefSysMacros.h>#ifndef ICE_IGNORE_VERSION#   if ICE_INT_VERSION / 100 != 306#       error Ice version mismatch!#   endif#   if ICE_INT_VERSION % 100 > 50#       error Beta header file detected#   endif#   if ICE_INT_VERSION % 100 < 2#       error Ice patch level mismatch!#   endif#endifnamespace IceProxy{namespace Demo{class Hello;void __read(::IceInternal::BasicStream*, ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello>&);::IceProxy::Ice::Object* upCast(::IceProxy::Demo::Hello*);}}namespace Demo{class Hello;::Ice::Object* upCast(::Demo::Hello*);typedef ::IceInternal::Handle< ::Demo::Hello> HelloPtr;typedef ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello> HelloPrx;void __patch(HelloPtr&, const ::Ice::ObjectPtr&);}namespace Demo{class RequestCanceledException : public ::Ice::UserException{public:    RequestCanceledException() {}    virtual ~RequestCanceledException() throw();    virtual ::std::string ice_name() const;    virtual RequestCanceledException* ice_clone() const;    virtual void ice_throw() const;protected:    virtual void __writeImpl(::IceInternal::BasicStream*) const;    virtual void __readImpl(::IceInternal::BasicStream*);    using ::Ice::UserException::__writeImpl;    using ::Ice::UserException::__readImpl;};static RequestCanceledException __RequestCanceledException_init;}namespace Ice{template<>struct StreamableTraits< ::Demo::RequestCanceledException>{    static const StreamHelperCategory helper = StreamHelperCategoryUserException;};}namespace Demo{class AMD_Hello_sayHello : virtual public ::Ice::AMDCallback{public:    virtual void ice_response() = 0;};typedef ::IceUtil::Handle< ::Demo::AMD_Hello_sayHello> AMD_Hello_sayHelloPtr;}namespace IceAsync{namespace Demo{class AMD_Hello_sayHello : public ::Demo::AMD_Hello_sayHello, public ::IceInternal::IncomingAsync{public:    AMD_Hello_sayHello(::IceInternal::Incoming&);    virtual void ice_response();    // COMPILERFIX: The using directive avoid compiler warnings with -Woverloaded-virtual    using ::IceInternal::IncomingAsync::ice_exception;    virtual void ice_exception(const ::std::exception&);};}}namespace Demo{class Callback_Hello_sayHello_Base : virtual public ::IceInternal::CallbackBase { };typedef ::IceUtil::Handle< Callback_Hello_sayHello_Base> Callback_Hello_sayHelloPtr;class Callback_Hello_shutdown_Base : virtual public ::IceInternal::CallbackBase { };typedef ::IceUtil::Handle< Callback_Hello_shutdown_Base> Callback_Hello_shutdownPtr;}namespace IceProxy{namespace Demo{class Hello : virtual public ::IceProxy::Ice::Object{public:    void sayHello(::Ice::Int __p_delay)    {        sayHello(__p_delay, 0);    }    void sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx)    {        sayHello(__p_delay, &__ctx);    }#ifdef ICE_CPP11    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())    {        return __begin_sayHello(__p_delay, 0, __response, __exception, __sent);    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_sayHello(__p_delay, 0, ::Ice::newCallback(__completed, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())    {        return __begin_sayHello(__p_delay, &__ctx, __response, __exception, __sent);    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_sayHello(__p_delay, &__ctx, ::Ice::newCallback(__completed, __sent));    }    private:    ::Ice::AsyncResultPtr __begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception, const ::IceInternal::Function<void (bool)>& __sent);    public:#endif    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay)    {        return begin_sayHello(__p_delay, 0, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx)    {        return begin_sayHello(__p_delay, &__ctx, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, 0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, &__ctx, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Demo::Callback_Hello_sayHelloPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, 0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::Demo::Callback_Hello_sayHelloPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, &__ctx, __del, __cookie);    }    void end_sayHello(const ::Ice::AsyncResultPtr&);    private:    void sayHello(::Ice::Int, const ::Ice::Context*);    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);    public:    void shutdown()    {        shutdown(0);    }    void shutdown(const ::Ice::Context& __ctx)    {        shutdown(&__ctx);    }#ifdef ICE_CPP11    ::Ice::AsyncResultPtr    begin_shutdown(const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())    {        return begin_shutdown(0, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent));    }    ::Ice::AsyncResultPtr    begin_shutdown(const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_shutdown(0, ::Ice::newCallback(__completed, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_shutdown(const ::Ice::Context& __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())    {        return begin_shutdown(&__ctx, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_shutdown(const ::Ice::Context& __ctx, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_shutdown(&__ctx, ::Ice::newCallback(__completed, __sent));    }#endif    ::Ice::AsyncResultPtr begin_shutdown()    {        return begin_shutdown(0, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx)    {        return begin_shutdown(&__ctx, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(&__ctx, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Demo::Callback_Hello_shutdownPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx, const ::Demo::Callback_Hello_shutdownPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(&__ctx, __del, __cookie);    }    void end_shutdown(const ::Ice::AsyncResultPtr&);    private:    void shutdown(const ::Ice::Context*);    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);    public:        ::IceInternal::ProxyHandle<Hello> ice_context(const ::Ice::Context& __context) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_context(__context).get());    }        ::IceInternal::ProxyHandle<Hello> ice_adapterId(const ::std::string& __id) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());    }        ::IceInternal::ProxyHandle<Hello> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());    }        ::IceInternal::ProxyHandle<Hello> ice_locatorCacheTimeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_connectionCached(bool __cached) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());    }        ::IceInternal::ProxyHandle<Hello> ice_endpointSelection(::Ice::EndpointSelectionType __est) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());    }        ::IceInternal::ProxyHandle<Hello> ice_secure(bool __secure) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_secure(__secure).get());    }        ::IceInternal::ProxyHandle<Hello> ice_preferSecure(bool __preferSecure) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());    }        ::IceInternal::ProxyHandle<Hello> ice_router(const ::Ice::RouterPrx& __router) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_router(__router).get());    }        ::IceInternal::ProxyHandle<Hello> ice_locator(const ::Ice::LocatorPrx& __locator) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_locator(__locator).get());    }        ::IceInternal::ProxyHandle<Hello> ice_collocationOptimized(bool __co) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());    }        ::IceInternal::ProxyHandle<Hello> ice_invocationTimeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_invocationTimeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_twoway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_twoway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_oneway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_oneway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_batchOneway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_batchOneway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_datagram() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_datagram().get());    }        ::IceInternal::ProxyHandle<Hello> ice_batchDatagram() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_batchDatagram().get());    }        ::IceInternal::ProxyHandle<Hello> ice_compress(bool __compress) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_compress(__compress).get());    }        ::IceInternal::ProxyHandle<Hello> ice_timeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_connectionId(const ::std::string& __id) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());    }        ::IceInternal::ProxyHandle<Hello> ice_encodingVersion(const ::Ice::EncodingVersion& __v) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_encodingVersion(__v).get());    }        static const ::std::string& ice_staticId();private:     virtual ::IceProxy::Ice::Object* __newInstance() const;};}}namespace Demo{class Hello : virtual public ::Ice::Object{public:    typedef HelloPrx ProxyType;    typedef HelloPtr PointerType;    virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;    virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;    virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;    static const ::std::string& ice_staticId();    virtual void sayHello_async(const ::Demo::AMD_Hello_sayHelloPtr&, ::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;    ::Ice::DispatchStatus ___sayHello(::IceInternal::Incoming&, const ::Ice::Current&);    virtual void shutdown(const ::Ice::Current& = ::Ice::Current()) = 0;    ::Ice::DispatchStatus ___shutdown(::IceInternal::Incoming&, const ::Ice::Current&);    virtual ::Ice::DispatchStatus __dispatch(::IceInternal::Incoming&, const ::Ice::Current&);protected:    virtual void __writeImpl(::IceInternal::BasicStream*) const;    virtual void __readImpl(::IceInternal::BasicStream*);    using ::Ice::Object::__writeImpl;    using ::Ice::Object::__readImpl;};inline bool operator==(const Hello& l, const Hello& r){    return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);}inline bool operator<(const Hello& l, const Hello& r){    return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);}}namespace Demo{template<class T>class CallbackNC_Hello_sayHello : public Callback_Hello_sayHello_Base, public ::IceInternal::TwowayCallbackNC<T>{public:    typedef IceUtil::Handle<T> TPtr;    typedef void (T::*Exception)(const ::Ice::Exception&);    typedef void (T::*Sent)(bool);    typedef void (T::*Response)();    CallbackNC_Hello_sayHello(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::TwowayCallbackNC<T>(obj, cb != 0, excb, sentcb), _response(cb)    {    }    virtual void completed(const ::Ice::AsyncResultPtr& __result) const    {        ::Demo::HelloPrx __proxy = ::Demo::HelloPrx::uncheckedCast(__result->getProxy());        try        {            __proxy->end_sayHello(__result);        }        catch(const ::Ice::Exception& ex)        {            ::IceInternal::CallbackNC<T>::exception(__result, ex);            return;        }        if(_response)        {            (::IceInternal::CallbackNC<T>::_callback.get()->*_response)();        }    }    private:    Response _response;};template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, 0, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, 0, excb, sentcb);}template<class T, typename CT>class Callback_Hello_sayHello : public Callback_Hello_sayHello_Base, public ::IceInternal::TwowayCallback<T, CT>{public:    typedef IceUtil::Handle<T> TPtr;    typedef void (T::*Exception)(const ::Ice::Exception& , const CT&);    typedef void (T::*Sent)(bool , const CT&);    typedef void (T::*Response)(const CT&);    Callback_Hello_sayHello(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::TwowayCallback<T, CT>(obj, cb != 0, excb, sentcb), _response(cb)    {    }    virtual void completed(const ::Ice::AsyncResultPtr& __result) const    {        ::Demo::HelloPrx __proxy = ::Demo::HelloPrx::uncheckedCast(__result->getProxy());        try        {            __proxy->end_sayHello(__result);        }        catch(const ::Ice::Exception& ex)        {            ::IceInternal::Callback<T, CT>::exception(__result, ex);            return;        }        if(_response)        {            (::IceInternal::Callback<T, CT>::_callback.get()->*_response)(CT::dynamicCast(__result->getCookie()));        }    }    private:    Response _response;};template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, 0, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, 0, excb, sentcb);}template<class T>class CallbackNC_Hello_shutdown : public Callback_Hello_shutdown_Base, public ::IceInternal::OnewayCallbackNC<T>{public:    typedef IceUtil::Handle<T> TPtr;    typedef void (T::*Exception)(const ::Ice::Exception&);    typedef void (T::*Sent)(bool);    typedef void (T::*Response)();    CallbackNC_Hello_shutdown(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)    {    }};template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, 0, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, 0, excb, sentcb);}template<class T, typename CT>class Callback_Hello_shutdown : public Callback_Hello_shutdown_Base, public ::IceInternal::OnewayCallback<T, CT>{public:    typedef IceUtil::Handle<T> TPtr;    typedef void (T::*Exception)(const ::Ice::Exception& , const CT&);    typedef void (T::*Sent)(bool , const CT&);    typedef void (T::*Response)(const CT&);    Callback_Hello_shutdown(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)    {    }};template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, 0, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, 0, excb, sentcb);}}#include <IceUtil/PopDisableWarnings.h>#endif

Hello.cpp
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// This copy of Ice is licensed to you under the terms described in the// ICE_LICENSE file included in this distribution.//// **********************************************************************//// Ice version 3.6.2//// <auto-generated>//// Generated from file `Hello.ice'//// Warning: do not edit this file.//// </auto-generated>//#include <Hello.h>#include <IceUtil/PushDisableWarnings.h>#include <Ice/LocalException.h>#include <Ice/ObjectFactory.h>#include <Ice/Outgoing.h>#include <Ice/OutgoingAsync.h>#include <Ice/BasicStream.h>#include <IceUtil/Iterator.h>#include <IceUtil/PopDisableWarnings.h>#ifndef ICE_IGNORE_VERSION#   if ICE_INT_VERSION / 100 != 306#       error Ice version mismatch!#   endif#   if ICE_INT_VERSION % 100 > 50#       error Beta header file detected#   endif#   if ICE_INT_VERSION % 100 < 2#       error Ice patch level mismatch!#   endif#endifnamespace{const ::std::string __Demo__Hello__sayHello_name = "sayHello";const ::std::string __Demo__Hello__shutdown_name = "shutdown";}namespace{const ::IceInternal::DefaultUserExceptionFactoryInit< ::Demo::RequestCanceledException> __Demo__RequestCanceledException_init("::Demo::RequestCanceledException");}Demo::RequestCanceledException::~RequestCanceledException() throw(){}::std::stringDemo::RequestCanceledException::ice_name() const{    return "Demo::RequestCanceledException";}Demo::RequestCanceledException*Demo::RequestCanceledException::ice_clone() const{    return new RequestCanceledException(*this);}voidDemo::RequestCanceledException::ice_throw() const{    throw *this;}voidDemo::RequestCanceledException::__writeImpl(::IceInternal::BasicStream* __os) const{    __os->startWriteSlice("::Demo::RequestCanceledException", -1, true);    __os->endWriteSlice();}voidDemo::RequestCanceledException::__readImpl(::IceInternal::BasicStream* __is){    __is->startReadSlice();    __is->endReadSlice();}namespace Ice{}IceAsync::Demo::AMD_Hello_sayHello::AMD_Hello_sayHello(::IceInternal::Incoming& in) :    ::IceInternal::IncomingAsync(in){}voidIceAsync::Demo::AMD_Hello_sayHello::ice_response(){    if(__validateResponse(true))    {        __writeEmptyParams();        __response();    }}voidIceAsync::Demo::AMD_Hello_sayHello::ice_exception(const ::std::exception& ex){    if(const ::Demo::RequestCanceledException* __ex = dynamic_cast<const ::Demo::RequestCanceledException*>(&ex))    {        if(__validateResponse(false))        {            __writeUserException(*__ex, ::Ice::DefaultFormat);            __response();        }    }    else    {        ::IceInternal::IncomingAsync::ice_exception(ex);    }}::IceProxy::Ice::Object* ::IceProxy::Demo::upCast(::IceProxy::Demo::Hello* p) { return p; }void::IceProxy::Demo::__read(::IceInternal::BasicStream* __is, ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello>& v){    ::Ice::ObjectPrx proxy;    __is->read(proxy);    if(!proxy)    {        v = 0;    }    else    {        v = new ::IceProxy::Demo::Hello;        v->__copyFrom(proxy);    }}voidIceProxy::Demo::Hello::sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx){    __checkTwowayOnly(__Demo__Hello__sayHello_name);    ::IceInternal::Outgoing __og(this, __Demo__Hello__sayHello_name, ::Ice::Idempotent, __ctx);    try    {        ::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);        __os->write(__p_delay);        __og.endWriteParams();    }    catch(const ::Ice::LocalException& __ex)    {        __og.abort(__ex);    }    if(!__og.invoke())    {        try        {            __og.throwUserException();        }        catch(const ::Demo::RequestCanceledException&)        {            throw;        }        catch(const ::Ice::UserException& __ex)        {            ::Ice::UnknownUserException __uue(__FILE__, __LINE__, __ex.ice_name());            throw __uue;        }    }}::Ice::AsyncResultPtrIceProxy::Demo::Hello::begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie){    __checkAsyncTwowayOnly(__Demo__Hello__sayHello_name);    ::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__Hello__sayHello_name, __del, __cookie);    try    {        __result->prepare(__Demo__Hello__sayHello_name, ::Ice::Idempotent, __ctx);        ::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);        __os->write(__p_delay);        __result->endWriteParams();        __result->invoke();    }    catch(const ::Ice::Exception& __ex)    {        __result->abort(__ex);    }    return __result;}#ifdef ICE_CPP11::Ice::AsyncResultPtrIceProxy::Demo::Hello::__begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception, const ::IceInternal::Function<void (bool)>& __sent){    class Cpp11CB : public ::IceInternal::Cpp11FnCallbackNC    {    public:        Cpp11CB(const ::std::function<void ()>& responseFunc, const ::std::function<void (const ::Ice::Exception&)>& exceptionFunc, const ::std::function<void (bool)>& sentFunc) :            ::IceInternal::Cpp11FnCallbackNC(exceptionFunc, sentFunc),            _response(responseFunc)        {            CallbackBase::checkCallback(true, responseFunc || exceptionFunc != nullptr);        }        virtual void completed(const ::Ice::AsyncResultPtr& __result) const        {            ::Demo::HelloPrx __proxy = ::Demo::HelloPrx::uncheckedCast(__result->getProxy());            try            {                __proxy->end_sayHello(__result);            }            catch(const ::Ice::Exception& ex)            {                Cpp11FnCallbackNC::exception(__result, ex);                return;            }            if(_response != nullptr)            {                _response();            }        }        private:                ::std::function<void ()> _response;    };    return begin_sayHello(__p_delay, __ctx, new Cpp11CB(__response, __exception, __sent));}#endifvoidIceProxy::Demo::Hello::end_sayHello(const ::Ice::AsyncResultPtr& __result){    ::Ice::AsyncResult::__check(__result, this, __Demo__Hello__sayHello_name);    if(!__result->__wait())    {        try        {            __result->__throwUserException();        }        catch(const ::Demo::RequestCanceledException&)        {            throw;        }        catch(const ::Ice::UserException& __ex)        {            throw ::Ice::UnknownUserException(__FILE__, __LINE__, __ex.ice_name());        }    }    __result->__readEmptyParams();}voidIceProxy::Demo::Hello::shutdown(const ::Ice::Context* __ctx){    ::IceInternal::Outgoing __og(this, __Demo__Hello__shutdown_name, ::Ice::Normal, __ctx);    __og.writeEmptyParams();    __invoke(__og);}::Ice::AsyncResultPtrIceProxy::Demo::Hello::begin_shutdown(const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie){    ::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__Hello__shutdown_name, __del, __cookie);    try    {        __result->prepare(__Demo__Hello__shutdown_name, ::Ice::Normal, __ctx);        __result->writeEmptyParams();        __result->invoke();    }    catch(const ::Ice::Exception& __ex)    {        __result->abort(__ex);    }    return __result;}voidIceProxy::Demo::Hello::end_shutdown(const ::Ice::AsyncResultPtr& __result){    __end(__result, __Demo__Hello__shutdown_name);}const ::std::string&IceProxy::Demo::Hello::ice_staticId(){    return ::Demo::Hello::ice_staticId();}::IceProxy::Ice::Object*IceProxy::Demo::Hello::__newInstance() const{    return new Hello;}::Ice::Object* Demo::upCast(::Demo::Hello* p) { return p; }namespace{const ::std::string __Demo__Hello_ids[2] ={    "::Demo::Hello",    "::Ice::Object"};}boolDemo::Hello::ice_isA(const ::std::string& _s, const ::Ice::Current&) const{    return ::std::binary_search(__Demo__Hello_ids, __Demo__Hello_ids + 2, _s);}::std::vector< ::std::string>Demo::Hello::ice_ids(const ::Ice::Current&) const{    return ::std::vector< ::std::string>(&__Demo__Hello_ids[0], &__Demo__Hello_ids[2]);}const ::std::string&Demo::Hello::ice_id(const ::Ice::Current&) const{    return __Demo__Hello_ids[0];}const ::std::string&Demo::Hello::ice_staticId(){#ifdef ICE_HAS_THREAD_SAFE_LOCAL_STATIC    static const ::std::string typeId = "::Demo::Hello";    return typeId;#else    return __Demo__Hello_ids[0];#endif}::Ice::DispatchStatusDemo::Hello::___sayHello(::IceInternal::Incoming& __inS, const ::Ice::Current& __current){    __checkMode(::Ice::Idempotent, __current.mode);    ::IceInternal::BasicStream* __is = __inS.startReadParams();    ::Ice::Int __p_delay;    __is->read(__p_delay);    __inS.endReadParams();    ::Demo::AMD_Hello_sayHelloPtr __cb = new IceAsync::Demo::AMD_Hello_sayHello(__inS);    try    {        sayHello_async(__cb, __p_delay, __current);    }    catch(const ::std::exception& __ex)    {        __cb->ice_exception(__ex);    }    catch(...)    {        __cb->ice_exception();    }    return ::Ice::DispatchAsync;}::Ice::DispatchStatusDemo::Hello::___shutdown(::IceInternal::Incoming& __inS, const ::Ice::Current& __current){    __checkMode(::Ice::Normal, __current.mode);    __inS.readEmptyParams();    shutdown(__current);    __inS.__writeEmptyParams();    return ::Ice::DispatchOK;}namespace{const ::std::string __Demo__Hello_all[] ={    "ice_id",    "ice_ids",    "ice_isA",    "ice_ping",    "sayHello",    "shutdown"};}::Ice::DispatchStatusDemo::Hello::__dispatch(::IceInternal::Incoming& in, const ::Ice::Current& current){    ::std::pair< const ::std::string*, const ::std::string*> r = ::std::equal_range(__Demo__Hello_all, __Demo__Hello_all + 6, current.operation);    if(r.first == r.second)    {        throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);    }    switch(r.first - __Demo__Hello_all)    {        case 0:        {            return ___ice_id(in, current);        }        case 1:        {            return ___ice_ids(in, current);        }        case 2:        {            return ___ice_isA(in, current);        }        case 3:        {            return ___ice_ping(in, current);        }        case 4:        {            return ___sayHello(in, current);        }        case 5:        {            return ___shutdown(in, current);        }    }    assert(false);    throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);}voidDemo::Hello::__writeImpl(::IceInternal::BasicStream* __os) const{    __os->startWriteSlice(ice_staticId(), -1, true);    __os->endWriteSlice();}voidDemo::Hello::__readImpl(::IceInternal::BasicStream* __is){    __is->startReadSlice();    __is->endReadSlice();}void Demo::__patch(HelloPtr& handle, const ::Ice::ObjectPtr& v){    handle = ::Demo::HelloPtr::dynamicCast(v);    if(v && !handle)    {        IceInternal::Ex::throwUOE(::Demo::Hello::ice_staticId(), v);    }}


三 实现接口

HelloI.h
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#ifndef HELLO_I_H#define HELLO_I_H#include <Hello.h>#include <WorkQueue.h>class HelloI : public Demo::Hello{public:    HelloI(const WorkQueuePtr&);    virtual void sayHello_async(const Demo::AMD_Hello_sayHelloPtr&, int, const Ice::Current&);    virtual void shutdown(const Ice::Current&);private:    WorkQueuePtr _workQueue;};#endif

HelloI.cpp
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <Ice/Ice.h>#include <HelloI.h>using namespace std;HelloI::HelloI(const WorkQueuePtr& workQueue) :    _workQueue(workQueue){}voidHelloI::sayHello_async(const Demo::AMD_Hello_sayHelloPtr& cb, int delay, const Ice::Current&){    if(delay == 0)    {        cout << "Hello World!" << endl;        cb->ice_response();    }    else    {        _workQueue->add(cb, delay);    }}voidHelloI::shutdown(const Ice::Current& curr){    cout << "Shutting down..." << endl;    _workQueue->destroy();    curr.adapter->getCommunicator()->shutdown();}


四 客户端与服务 器

Client.cpp
1) 客户端发起异步调用,提供回调方法,在生成的代码中,有一族 begin_syaHello 调用方法,例子中,使用的是
::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Demo::Callback_Hello_sayHelloPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
2) 服务器会实现 virtual void sayHello_async(const Demo::AMD_Hello_sayHelloPtr&, int, const Ice::Current&);
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <Ice/Ice.h>#include <Hello.h>using namespace std;using namespace Demo;class Callback : public IceUtil::Shared{public:    void response()    {    }    void exception(const Ice::Exception& ex)    {        cerr << "sayHello AMI call failed:\n" << ex << endl;    }};typedef IceUtil::Handle<Callback> CallbackPtr;class AsyncClient : public Ice::Application{public:    AsyncClient();    virtual int run(int, char*[]);private:    void exception(const Ice::Exception&);    void menu();};AsyncClient::AsyncClient() :    //    // Since this is an interactive demo we don't want any signal    // handling.    //    Ice::Application(Ice::NoSignalHandling){}intmain(int argc, char* argv[]){    AsyncClient app;    return app.main(argc, argv, "config.client");}intAsyncClient::run(int argc, char* argv[]){    if(argc > 1)    {        cerr << appName() << ": too many arguments" << endl;        return EXIT_FAILURE;    }    HelloPrx hello = HelloPrx::checkedCast(communicator()->propertyToProxy("Hello.Proxy"));    if(!hello)    {        cerr << argv[0] << ": invalid proxy" << endl;        return EXIT_FAILURE;    }    menu();    CallbackPtr cb = new Callback();    char c = 'x';    do    {        try        {            cout << "==> ";            cin >> c;            if(c == 'i')            {                hello->sayHello(0);            }            else if(c == 'd')            {                hello->begin_sayHello(5000, newCallback_Hello_sayHello(cb, &Callback::response, &Callback::exception));            }            else if(c == 's')            {                hello->shutdown();            }            else if(c == 'x')            {                // Nothing to do            }            else if(c == '?')            {                menu();            }            else            {                cout << "unknown command `" << c << "'" << endl;                menu();            }        }        catch(const Ice::Exception& ex)        {            cerr << ex << endl;        }    }    while(cin.good() && c != 'x');    return EXIT_SUCCESS;}voidAsyncClient::menu(){    cout <<        "usage:\n"        "i: send immediate greeting\n"        "d: send delayed greeting\n"        "s: shutdown server\n"        "x: exit\n"        "?: help\n";}

Server.cpp
1) 接口实现中引入了一个工作队列,
2)实现virtual void sayHello_async(const Demo::AMD_Hello_sayHelloPtr&, int, const Ice::Current&);
  如果 delay =0 则马上回调
  否则加入工作队列
3)工作线程执行完成后,会调用回调方法
// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <Ice/Ice.h>#include <HelloI.h>#include <WorkQueue.h>using namespace std;class AsyncServer : public Ice::Application{public:    virtual int run(int, char*[]);    virtual void interruptCallback(int);private:    WorkQueuePtr _workQueue;};intmain(int argc, char* argv[]){    AsyncServer app;    return app.main(argc, argv, "config.server");}intAsyncServer::run(int argc, char*[]){    if(argc > 1)    {        cerr << appName() << ": too many arguments" << endl;        return EXIT_FAILURE;    }    callbackOnInterrupt();    Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Hello");    _workQueue = new WorkQueue();    Demo::HelloPtr hello = new HelloI(_workQueue);    adapter->add(hello, communicator()->stringToIdentity("hello"));    _workQueue->start();    adapter->activate();    communicator()->waitForShutdown();    _workQueue->getThreadControl().join();    return EXIT_SUCCESS;}voidAsyncServer::interruptCallback(int){    _workQueue->destroy();    communicator()->shutdown();}

五 配置文件

config.client
## The client reads this property to create the reference to the# "hello" object in the server.#Hello.Proxy=hello:tcp -h localhost -p 10000## Warn about connection exceptions#Ice.Warn.Connections=1## Network Tracing## 0 = no network tracing# 1 = trace connection establishment and closure# 2 = like 1, but more detailed# 3 = like 2, but also trace data transfer##Ice.Trace.Network=1## Protocol Tracing## 0 = no protocol tracing# 1 = trace protocol messages##Ice.Trace.Protocol=1

config.server
## The server creates one single object adapter with the name# "Hello". The following line sets the endpoints for this# adapter.#Hello.Endpoints=tcp -h localhost -p 10000## Warn about connection exceptions#Ice.Warn.Connections=1## Network Tracing## 0 = no network tracing# 1 = trace connection establishment and closure# 2 = like 1, but more detailed# 3 = like 2, but also trace data transfer##Ice.Trace.Network=1## Protocol Tracing## 0 = no protocol tracing# 1 = trace protocol messages##Ice.Trace.Protocol=1



0 0