ice使用过程跟google protocol buffer、gsoap类似,都是先写一个类似配置文件的东西,然后使用提供的工具生成相应语言的代码。

ice先写一个后缀名为.ice的文件,然后使用slice2cpp.exe生产对应c++代码,如下

module MCal
{
interface Cal
{
int add(int num1,int num2);
int sub(int num1,int num2);
};
};


其中MCal是生成c++类的命名空间,Cal是生成类的名字,add和sub是定义类的两个方法。

可以直接命令行执行slice2cpp Caculator.ice生成,也可以在vs中选择slice2cpp作为编译.ice的工具生成,下面是工具生成的代码

// **********************************************************************
//
// 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.3
//
// <auto-generated>
//
// Generated from file `Caculator.ice'
//
// Warning: do not edit this file.
//
// </auto-generated>
//

#ifndef __Caculator_h__
#define __Caculator_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 <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 < 3
# error Ice patch level mismatch!
# endif
#endif

namespace IceProxy
{

namespace MCal
{

class Cal;
void __read(::IceInternal::BasicStream*, ::IceInternal::ProxyHandle< ::IceProxy::MCal::Cal>&);
::IceProxy::Ice::Object* upCast(::IceProxy::MCal::Cal*);

}

}

namespace MCal
{

class Cal;
::Ice::Object* upCast(::MCal::Cal*);
typedef ::IceInternal::Handle< ::MCal::Cal> CalPtr;
typedef ::IceInternal::ProxyHandle< ::IceProxy::MCal::Cal> CalPrx;
void __patch(CalPtr&, const ::Ice::ObjectPtr&);

}

namespace MCal
{

class Callback_Cal_add_Base : virtual public ::IceInternal::CallbackBase { };
typedef ::IceUtil::Handle< Callback_Cal_add_Base> Callback_Cal_addPtr;

class Callback_Cal_sub_Base : virtual public ::IceInternal::CallbackBase { };
typedef ::IceUtil::Handle< Callback_Cal_sub_Base> Callback_Cal_subPtr;

}

namespace IceProxy
{

namespace MCal
{

class Cal : virtual public ::IceProxy::Ice::Object
{
public:

::Ice::Int add(::Ice::Int __p_num1, ::Ice::Int __p_num2)
{
return add(__p_num1, __p_num2, 0);
}
::Ice::Int add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx)
{
return add(__p_num1, __p_num2, &__ctx);
}
#ifdef ICE_CPP11
::Ice::AsyncResultPtr
begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::IceInternal::Function<void (::Ice::Int)>& __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_add(__p_num1, __p_num2, 0, __response, __exception, __sent);
}
::Ice::AsyncResultPtr
begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_add(__p_num1, __p_num2, 0, ::Ice::newCallback(__completed, __sent), 0);
}
::Ice::AsyncResultPtr
begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __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_add(__p_num1, __p_num2, &__ctx, __response, __exception, __sent);
}
::Ice::AsyncResultPtr
begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, 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_add(__p_num1, __p_num2, &__ctx, ::Ice::newCallback(__completed, __sent));
}

private:

::Ice::AsyncResultPtr __begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception, const ::IceInternal::Function<void (bool)>& __sent);

public:
#endif

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2)
{
return begin_add(__p_num1, __p_num2, 0, ::IceInternal::__dummyCallback, 0);
}

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx)
{
return begin_add(__p_num1, __p_num2, &__ctx, ::IceInternal::__dummyCallback, 0);
}

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_add(__p_num1, __p_num2, 0, __del, __cookie);
}

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_add(__p_num1, __p_num2, &__ctx, __del, __cookie);
}

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::MCal::Callback_Cal_addPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_add(__p_num1, __p_num2, 0, __del, __cookie);
}

::Ice::AsyncResultPtr begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::MCal::Callback_Cal_addPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_add(__p_num1, __p_num2, &__ctx, __del, __cookie);
}

::Ice::Int end_add(const ::Ice::AsyncResultPtr&);

private:

::Ice::Int add(::Ice::Int, ::Ice::Int, const ::Ice::Context*);
::Ice::AsyncResultPtr begin_add(::Ice::Int, ::Ice::Int, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);

public:

::Ice::Int sub(::Ice::Int __p_num1, ::Ice::Int __p_num2)
{
return sub(__p_num1, __p_num2, 0);
}
::Ice::Int sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx)
{
return sub(__p_num1, __p_num2, &__ctx);
}
#ifdef ICE_CPP11
::Ice::AsyncResultPtr
begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::IceInternal::Function<void (::Ice::Int)>& __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_sub(__p_num1, __p_num2, 0, __response, __exception, __sent);
}
::Ice::AsyncResultPtr
begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_sub(__p_num1, __p_num2, 0, ::Ice::newCallback(__completed, __sent), 0);
}
::Ice::AsyncResultPtr
begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __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_sub(__p_num1, __p_num2, &__ctx, __response, __exception, __sent);
}
::Ice::AsyncResultPtr
begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, 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_sub(__p_num1, __p_num2, &__ctx, ::Ice::newCallback(__completed, __sent));
}

private:

::Ice::AsyncResultPtr __begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception, const ::IceInternal::Function<void (bool)>& __sent);

public:
#endif

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2)
{
return begin_sub(__p_num1, __p_num2, 0, ::IceInternal::__dummyCallback, 0);
}

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx)
{
return begin_sub(__p_num1, __p_num2, &__ctx, ::IceInternal::__dummyCallback, 0);
}

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_sub(__p_num1, __p_num2, 0, __del, __cookie);
}

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_sub(__p_num1, __p_num2, &__ctx, __del, __cookie);
}

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::MCal::Callback_Cal_subPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_sub(__p_num1, __p_num2, 0, __del, __cookie);
}

::Ice::AsyncResultPtr begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context& __ctx, const ::MCal::Callback_Cal_subPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_sub(__p_num1, __p_num2, &__ctx, __del, __cookie);
}

::Ice::Int end_sub(const ::Ice::AsyncResultPtr&);

private:

::Ice::Int sub(::Ice::Int, ::Ice::Int, const ::Ice::Context*);
::Ice::AsyncResultPtr begin_sub(::Ice::Int, ::Ice::Int, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);

public:

::IceInternal::ProxyHandle<Cal> ice_context(const ::Ice::Context& __context) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_context(__context).get());
}

::IceInternal::ProxyHandle<Cal> ice_adapterId(const ::std::string& __id) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());
}

::IceInternal::ProxyHandle<Cal> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());
}

::IceInternal::ProxyHandle<Cal> ice_locatorCacheTimeout(int __timeout) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());
}

::IceInternal::ProxyHandle<Cal> ice_connectionCached(bool __cached) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());
}

::IceInternal::ProxyHandle<Cal> ice_endpointSelection(::Ice::EndpointSelectionType __est) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());
}

::IceInternal::ProxyHandle<Cal> ice_secure(bool __secure) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_secure(__secure).get());
}

::IceInternal::ProxyHandle<Cal> ice_preferSecure(bool __preferSecure) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());
}

::IceInternal::ProxyHandle<Cal> ice_router(const ::Ice::RouterPrx& __router) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_router(__router).get());
}

::IceInternal::ProxyHandle<Cal> ice_locator(const ::Ice::LocatorPrx& __locator) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_locator(__locator).get());
}

::IceInternal::ProxyHandle<Cal> ice_collocationOptimized(bool __co) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());
}

::IceInternal::ProxyHandle<Cal> ice_invocationTimeout(int __timeout) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_invocationTimeout(__timeout).get());
}

::IceInternal::ProxyHandle<Cal> ice_twoway() const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_twoway().get());
}

::IceInternal::ProxyHandle<Cal> ice_oneway() const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_oneway().get());
}

::IceInternal::ProxyHandle<Cal> ice_batchOneway() const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_batchOneway().get());
}

::IceInternal::ProxyHandle<Cal> ice_datagram() const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_datagram().get());
}

::IceInternal::ProxyHandle<Cal> ice_batchDatagram() const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_batchDatagram().get());
}

::IceInternal::ProxyHandle<Cal> ice_compress(bool __compress) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_compress(__compress).get());
}

::IceInternal::ProxyHandle<Cal> ice_timeout(int __timeout) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());
}

::IceInternal::ProxyHandle<Cal> ice_connectionId(const ::std::string& __id) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());
}

::IceInternal::ProxyHandle<Cal> ice_encodingVersion(const ::Ice::EncodingVersion& __v) const
{
return dynamic_cast<Cal*>(::IceProxy::Ice::Object::ice_encodingVersion(__v).get());
}

static const ::std::string& ice_staticId();

private:
virtual ::IceProxy::Ice::Object* __newInstance() const;
};

}

}

namespace MCal
{

class Cal : virtual public ::Ice::Object
{
public:

typedef CalPrx ProxyType;
typedef CalPtr 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 ::Ice::Int add(::Ice::Int, ::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;
::Ice::DispatchStatus ___add(::IceInternal::Incoming&, const ::Ice::Current&);

virtual ::Ice::Int sub(::Ice::Int, ::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;
::Ice::DispatchStatus ___sub(::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 Cal& l, const Cal& r)
{
return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);
}

inline bool operator<(const Cal& l, const Cal& r)
{
return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);
}

}

namespace MCal
{

template<class T>
class CallbackNC_Cal_add : public Callback_Cal_add_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)(::Ice::Int);

CallbackNC_Cal_add(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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_add(__result);
}
catch(const ::Ice::Exception& ex)
{
::IceInternal::CallbackNC<T>::exception(__result, ex);
return;
}
if(_response)
{
(::IceInternal::CallbackNC<T>::_callback.get()->*_response)(__ret);
}
}

private:

Response _response;
};

template<class T> Callback_Cal_addPtr
newCallback_Cal_add(const IceUtil::Handle<T>& instance, void (T::*cb)(::Ice::Int), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_Cal_add<T>(instance, cb, excb, sentcb);
}

template<class T> Callback_Cal_addPtr
newCallback_Cal_add(T* instance, void (T::*cb)(::Ice::Int), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_Cal_add<T>(instance, cb, excb, sentcb);
}

template<class T, typename CT>
class Callback_Cal_add : public Callback_Cal_add_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)(::Ice::Int, const CT&);

Callback_Cal_add(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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_add(__result);
}
catch(const ::Ice::Exception& ex)
{
::IceInternal::Callback<T, CT>::exception(__result, ex);
return;
}
if(_response)
{
(::IceInternal::Callback<T, CT>::_callback.get()->*_response)(__ret, CT::dynamicCast(__result->getCookie()));
}
}

private:

Response _response;
};

template<class T, typename CT> Callback_Cal_addPtr
newCallback_Cal_add(const IceUtil::Handle<T>& instance, void (T::*cb)(::Ice::Int, const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_Cal_add<T, CT>(instance, cb, excb, sentcb);
}

template<class T, typename CT> Callback_Cal_addPtr
newCallback_Cal_add(T* instance, void (T::*cb)(::Ice::Int, const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_Cal_add<T, CT>(instance, cb, excb, sentcb);
}

template<class T>
class CallbackNC_Cal_sub : public Callback_Cal_sub_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)(::Ice::Int);

CallbackNC_Cal_sub(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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_sub(__result);
}
catch(const ::Ice::Exception& ex)
{
::IceInternal::CallbackNC<T>::exception(__result, ex);
return;
}
if(_response)
{
(::IceInternal::CallbackNC<T>::_callback.get()->*_response)(__ret);
}
}

private:

Response _response;
};

template<class T> Callback_Cal_subPtr
newCallback_Cal_sub(const IceUtil::Handle<T>& instance, void (T::*cb)(::Ice::Int), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_Cal_sub<T>(instance, cb, excb, sentcb);
}

template<class T> Callback_Cal_subPtr
newCallback_Cal_sub(T* instance, void (T::*cb)(::Ice::Int), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_Cal_sub<T>(instance, cb, excb, sentcb);
}

template<class T, typename CT>
class Callback_Cal_sub : public Callback_Cal_sub_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)(::Ice::Int, const CT&);

Callback_Cal_sub(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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_sub(__result);
}
catch(const ::Ice::Exception& ex)
{
::IceInternal::Callback<T, CT>::exception(__result, ex);
return;
}
if(_response)
{
(::IceInternal::Callback<T, CT>::_callback.get()->*_response)(__ret, CT::dynamicCast(__result->getCookie()));
}
}

private:

Response _response;
};

template<class T, typename CT> Callback_Cal_subPtr
newCallback_Cal_sub(const IceUtil::Handle<T>& instance, void (T::*cb)(::Ice::Int, const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_Cal_sub<T, CT>(instance, cb, excb, sentcb);
}

template<class T, typename CT> Callback_Cal_subPtr
newCallback_Cal_sub(T* instance, void (T::*cb)(::Ice::Int, const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_Cal_sub<T, CT>(instance, cb, excb, sentcb);
}

}

#include <IceUtil/PopDisableWarnings.h>
#endif


// **********************************************************************
//
// 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.3
//
// <auto-generated>
//
// Generated from file `Caculator.ice'
//
// Warning: do not edit this file.
//
// </auto-generated>
//

#include "Caculator.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 < 3
# error Ice patch level mismatch!
# endif
#endif

namespace
{

const ::std::string __MCal__Cal__add_name = "add";

const ::std::string __MCal__Cal__sub_name = "sub";

}
::IceProxy::Ice::Object* ::IceProxy::MCal::upCast(::IceProxy::MCal::Cal* p) { return p; }

void
::IceProxy::MCal::__read(::IceInternal::BasicStream* __is, ::IceInternal::ProxyHandle< ::IceProxy::MCal::Cal>& v)
{
::Ice::ObjectPrx proxy;
__is->read(proxy);
if(!proxy)
{
v = 0;
}
else
{
v = new ::IceProxy::MCal::Cal;
v->__copyFrom(proxy);
}
}

::Ice::Int
IceProxy::MCal::Cal::add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx)
{
__checkTwowayOnly(__MCal__Cal__add_name);
::IceInternal::Outgoing __og(this, __MCal__Cal__add_name, ::Ice::Normal, __ctx);
try
{
::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num1);
__os->write(__p_num2);
__og.endWriteParams();
}
catch(const ::Ice::LocalException& __ex)
{
__og.abort(__ex);
}
if(!__og.invoke())
{
try
{
__og.throwUserException();
}
catch(const ::Ice::UserException& __ex)
{
::Ice::UnknownUserException __uue(__FILE__, __LINE__, __ex.ice_name());
throw __uue;
}
}
::Ice::Int __ret;
::IceInternal::BasicStream* __is = __og.startReadParams();
__is->read(__ret);
__og.endReadParams();
return __ret;
}

::Ice::AsyncResultPtr
IceProxy::MCal::Cal::begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie)
{
__checkAsyncTwowayOnly(__MCal__Cal__add_name);
::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __MCal__Cal__add_name, __del, __cookie);
try
{
__result->prepare(__MCal__Cal__add_name, ::Ice::Normal, __ctx);
::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num1);
__os->write(__p_num2);
__result->endWriteParams();
__result->invoke();
}
catch(const ::Ice::Exception& __ex)
{
__result->abort(__ex);
}
return __result;
}

#ifdef ICE_CPP11

::Ice::AsyncResultPtr
IceProxy::MCal::Cal::__begin_add(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __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 (::Ice::Int)>& 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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_add(__result);
}
catch(const ::Ice::Exception& ex)
{
Cpp11FnCallbackNC::exception(__result, ex);
return;
}
if(_response != nullptr)
{
_response(__ret);
}
}

private:

::std::function<void (::Ice::Int)> _response;
};
return begin_add(__p_num1, __p_num2, __ctx, new Cpp11CB(__response, __exception, __sent));
}
#endif

::Ice::Int
IceProxy::MCal::Cal::end_add(const ::Ice::AsyncResultPtr& __result)
{
::Ice::AsyncResult::__check(__result, this, __MCal__Cal__add_name);
::Ice::Int __ret;
if(!__result->__wait())
{
try
{
__result->__throwUserException();
}
catch(const ::Ice::UserException& __ex)
{
throw ::Ice::UnknownUserException(__FILE__, __LINE__, __ex.ice_name());
}
}
::IceInternal::BasicStream* __is = __result->__startReadParams();
__is->read(__ret);
__result->__endReadParams();
return __ret;
}

::Ice::Int
IceProxy::MCal::Cal::sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx)
{
__checkTwowayOnly(__MCal__Cal__sub_name);
::IceInternal::Outgoing __og(this, __MCal__Cal__sub_name, ::Ice::Normal, __ctx);
try
{
::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num1);
__os->write(__p_num2);
__og.endWriteParams();
}
catch(const ::Ice::LocalException& __ex)
{
__og.abort(__ex);
}
if(!__og.invoke())
{
try
{
__og.throwUserException();
}
catch(const ::Ice::UserException& __ex)
{
::Ice::UnknownUserException __uue(__FILE__, __LINE__, __ex.ice_name());
throw __uue;
}
}
::Ice::Int __ret;
::IceInternal::BasicStream* __is = __og.startReadParams();
__is->read(__ret);
__og.endReadParams();
return __ret;
}

::Ice::AsyncResultPtr
IceProxy::MCal::Cal::begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie)
{
__checkAsyncTwowayOnly(__MCal__Cal__sub_name);
::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __MCal__Cal__sub_name, __del, __cookie);
try
{
__result->prepare(__MCal__Cal__sub_name, ::Ice::Normal, __ctx);
::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num1);
__os->write(__p_num2);
__result->endWriteParams();
__result->invoke();
}
catch(const ::Ice::Exception& __ex)
{
__result->abort(__ex);
}
return __result;
}

#ifdef ICE_CPP11

::Ice::AsyncResultPtr
IceProxy::MCal::Cal::__begin_sub(::Ice::Int __p_num1, ::Ice::Int __p_num2, const ::Ice::Context* __ctx, const ::IceInternal::Function<void (::Ice::Int)>& __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 (::Ice::Int)>& 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
{
::MCal::CalPrx __proxy = ::MCal::CalPrx::uncheckedCast(__result->getProxy());
::Ice::Int __ret;
try
{
__ret = __proxy->end_sub(__result);
}
catch(const ::Ice::Exception& ex)
{
Cpp11FnCallbackNC::exception(__result, ex);
return;
}
if(_response != nullptr)
{
_response(__ret);
}
}

private:

::std::function<void (::Ice::Int)> _response;
};
return begin_sub(__p_num1, __p_num2, __ctx, new Cpp11CB(__response, __exception, __sent));
}
#endif

::Ice::Int
IceProxy::MCal::Cal::end_sub(const ::Ice::AsyncResultPtr& __result)
{
::Ice::AsyncResult::__check(__result, this, __MCal__Cal__sub_name);
::Ice::Int __ret;
if(!__result->__wait())
{
try
{
__result->__throwUserException();
}
catch(const ::Ice::UserException& __ex)
{
throw ::Ice::UnknownUserException(__FILE__, __LINE__, __ex.ice_name());
}
}
::IceInternal::BasicStream* __is = __result->__startReadParams();
__is->read(__ret);
__result->__endReadParams();
return __ret;
}

const ::std::string&
IceProxy::MCal::Cal::ice_staticId()
{
return ::MCal::Cal::ice_staticId();
}

::IceProxy::Ice::Object*
IceProxy::MCal::Cal::__newInstance() const
{
return new Cal;
}

::Ice::Object* MCal::upCast(::MCal::Cal* p) { return p; }

namespace
{
const ::std::string __MCal__Cal_ids[2] =
{
"::Ice::Object",
"::MCal::Cal"
};

}

bool
MCal::Cal::ice_isA(const ::std::string& _s, const ::Ice::Current&) const
{
return ::std::binary_search(__MCal__Cal_ids, __MCal__Cal_ids + 2, _s);
}

::std::vector< ::std::string>
MCal::Cal::ice_ids(const ::Ice::Current&) const
{
return ::std::vector< ::std::string>(&__MCal__Cal_ids[0], &__MCal__Cal_ids[2]);
}

const ::std::string&
MCal::Cal::ice_id(const ::Ice::Current&) const
{
return __MCal__Cal_ids[1];
}

const ::std::string&
MCal::Cal::ice_staticId()
{
#ifdef ICE_HAS_THREAD_SAFE_LOCAL_STATIC
static const ::std::string typeId = "::MCal::Cal";
return typeId;
#else
return __MCal__Cal_ids[1];
#endif
}

::Ice::DispatchStatus
MCal::Cal::___add(::IceInternal::Incoming& __inS, const ::Ice::Current& __current)
{
__checkMode(::Ice::Normal, __current.mode);
::IceInternal::BasicStream* __is = __inS.startReadParams();
::Ice::Int __p_num1;
::Ice::Int __p_num2;
__is->read(__p_num1);
__is->read(__p_num2);
__inS.endReadParams();
::Ice::Int __ret = add(__p_num1, __p_num2, __current);
::IceInternal::BasicStream* __os = __inS.__startWriteParams(::Ice::DefaultFormat);
__os->write(__ret);
__inS.__endWriteParams(true);
return ::Ice::DispatchOK;
}

::Ice::DispatchStatus
MCal::Cal::___sub(::IceInternal::Incoming& __inS, const ::Ice::Current& __current)
{
__checkMode(::Ice::Normal, __current.mode);
::IceInternal::BasicStream* __is = __inS.startReadParams();
::Ice::Int __p_num1;
::Ice::Int __p_num2;
__is->read(__p_num1);
__is->read(__p_num2);
__inS.endReadParams();
::Ice::Int __ret = sub(__p_num1, __p_num2, __current);
::IceInternal::BasicStream* __os = __inS.__startWriteParams(::Ice::DefaultFormat);
__os->write(__ret);
__inS.__endWriteParams(true);
return ::Ice::DispatchOK;
}

namespace
{
const ::std::string __MCal__Cal_all[] =
{
"add",
"ice_id",
"ice_ids",
"ice_isA",
"ice_ping",
"sub"
};

}

::Ice::DispatchStatus
MCal::Cal::__dispatch(::IceInternal::Incoming& in, const ::Ice::Current& current)
{
::std::pair< const ::std::string*, const ::std::string*> r = ::std::equal_range(__MCal__Cal_all, __MCal__Cal_all + 6, current.operation);
if(r.first == r.second)
{
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}

switch(r.first - __MCal__Cal_all)
{
case 0:
{
return ___add(in, current);
}
case 1:
{
return ___ice_id(in, current);
}
case 2:
{
return ___ice_ids(in, current);
}
case 3:
{
return ___ice_isA(in, current);
}
case 4:
{
return ___ice_ping(in, current);
}
case 5:
{
return ___sub(in, current);
}
}

assert(false);
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}

void
MCal::Cal::__writeImpl(::IceInternal::BasicStream* __os) const
{
__os->startWriteSlice(ice_staticId(), -1, true);
__os->endWriteSlice();
}

void
MCal::Cal::__readImpl(::IceInternal::BasicStream* __is)
{
__is->startReadSlice();
__is->endReadSlice();
}

void
MCal::__patch(CalPtr& handle, const ::Ice::ObjectPtr& v)
{
handle = ::MCal::CalPtr::dynamicCast(v);
if(v && !handle)
{
IceInternal::Ex::throwUOE(::MCal::Cal::ice_staticId(), v);
}
}


可以看到生成的类继承自Ice::Object,这点和ACE的使用又有点像了,只不过ACE要自己写类继承。调用的时候一个作为server端监听端口并提供对象访问点,client端连接端口并访问server端提供方法,上面生成的Caculator.h提供给client工程编译即可。

// ICE_Server.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<Ice/Ice.h>
#include<Ice/Application.h>
#include<iostream>

#include "Caculator.h"

using namespace std;
using namespace MCal;

class CalI:public Cal
{
public:
CalI()
{
num1d=0;
num2d=0;
}
~CalI()
{
}
virtual int add(int num1,int num2,const Ice::Current&);
virtual int sub(int num1,int num2,const Ice::Current&);
private:
int num1d;
int num2d;
};

int CalI::add(int num1, int num2, const Ice::Current &)
{
return (num1+num2);
}
int CalI::sub(int num1,int num2,const Ice::Current&)
{
if (num1>num2)
{
return (num1-num2);
}
else
{
cout<<"there is some wrong!"<<endl;
return 0;
}
}

class CalApp:virtual public Ice::Application
{
public:
virtual int run(int,char *[]);
};

int CalApp::run(int,char *[])
{
//shutdownOnInterrupt();
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
"Caladapter","default -p 10000");
Ice::ObjectPtr object = new CalI();
adapter->add(object,communicator()->stringToIdentity("Cal"));
adapter->activate();
communicator()->waitForShutdown();

if(interrupted())
cerr << appName() << ":recv signal,shutting down" << endl;

return 0;
}

#pragma comment(lib, "iced.lib")
#pragma comment(lib, "iceutild.lib")

int main1(int argc,char* argv[])
{
CalApp app;
app.main(argc,argv);

system("pause");

return 0;
}

int main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("CalAdatper", "default -p 10000");
Ice::ObjectPtr object = new CalI();
adapter->add(object, ic->stringToIdentity("Cal"));
adapter->activate();
ic->waitForShutdown();
}
catch (const Ice::Exception& e)
{
cerr << e << endl;
status = 1;
}
catch (const char* msg)
{
cerr << msg << endl;
status = 1;
}
catch (std::exception& ex)
{
cerr << ex.what() << endl;
status = 1;
}
if (ic)
{
ic->destroy();
}
system("pause");

return status;
}


// CalculatorClient.cpp : 定义控制台应用程序的入口点。
//

#include <Ice/Ice.h>
#include <Ice/Application.h>
#include "Calculator.h"
#include <iostream>

using namespace std;
using namespace MCal;

//#define RC_List "/home/tester/Key_Checking/ATT_RC/RMT_B109P_EU_Set.txt"
//#define RC_Data "/home/tester/Key_Checking/ATT_RC/RMT_B109P_EU_Set.dat"

class MyApp : virtual public Ice::Application
{
public:
virtual int run(int,char*[]);
};

int MyApp::run(int,char*[])
{
Ice::ObjectPrx base = communicator()->stringToProxy("Cal:default -p 10000");
CalPrx calculator = CalPrx::checkedCast(base);
if(!calculator)
throw "Invalid proxy!";
cout<<calculator->add(1,2)<<endl;

cout<<calculator->sub(56,12)<<endl;


//Calc->Close();
return 0;
}

#pragma comment(lib, "ice.lib")
#pragma comment(lib, "iceutil.lib")

int main1(int argc,char* argv[])
{
MyApp app;

app.main(argc,argv);

system("pause");

return 0;
}

int main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("Cal:default -p 10000");
CalPrx printer = CalPrx::checkedCast(base);
if (!printer)
{
throw "Invalid proxy";
}
printer->add(5,7);
}
catch (const Ice::Exception& ex)
{
cerr << ex << endl;
status = 1;
}
catch (const char* msg)
{
cerr << msg << endl;
status = 1;
}
if (ic)
ic->destroy();

system("pause");

return status;
}