ICE 网络库尝试

1.编写 ice 对象定义文件Printer.ice:

 

// **********************************************************************

//

// Copyright (c) 2003-2010 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.

//

// **********************************************************************

 

#ifndef SIMPLE_ICE

#define SIMPLE_ICE

 

module Demo

{

 

    interface Printer

    {

        void printString(string s);

    };

 

};

 

#endif

2. 生成c++ 代理类:
在命令行输入 slice2cpp Printer.ice
生成:
// **********************************************************************
//
// Copyright (c) 2003-2010 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.4.1
// <auto-generated>
//
// Generated from file `printer.ice'
//
// Warning: do not edit this file.
//
// </auto-generated>
#ifndef __printer_h__
#define __printer_h__
#include <Ice/LocalObjectF.h>
#include <Ice/ProxyF.h>
#include <Ice/ObjectF.h>
#include <Ice/Exception.h>
#include <Ice/LocalObject.h>
#include <Ice/Proxy.h>
#include <Ice/Object.h>
#include <Ice/Outgoing.h>
#include <Ice/OutgoingAsync.h>
#include <Ice/Incoming.h>
#include <Ice/Direct.h>
#include <IceUtil/ScopedArray.h>
#include <Ice/StreamF.h>
#include <Ice/UndefSysMacros.h>
#ifndef ICE_IGNORE_VERSION
#   if ICE_INT_VERSION / 100 != 304
#       error Ice version mismatch!
#   endif
#   if ICE_INT_VERSION % 100 > 50
#       error Beta header file detected
#   endif
#   if ICE_INT_VERSION % 100 < 1
#       error Ice patch level mismatch!
#   endif
#endif
namespace IceProxy
{
namespace Demo
{
class Printer;
}
}
namespace Demo
{
class Printer;
bool operator==(const Printer&, const Printer&);
bool operator<(const Printer&, const Printer&);
}
namespace IceInternal
{
::Ice::Object* upCast(::Demo::Printer*);
::IceProxy::Ice::Object* upCast(::IceProxy::Demo::Printer*);
}
namespace Demo
{
typedef ::IceInternal::Handle< ::Demo::Printer> PrinterPtr;
typedef ::IceInternal::ProxyHandle< ::IceProxy::Demo::Printer> PrinterPrx;
void __read(::IceInternal::BasicStream*, PrinterPrx&);
void __patch__PrinterPtr(void*, ::Ice::ObjectPtr&);
}
namespace Demo
{
class Callback_Printer_printString_Base : virtual public ::IceInternal::CallbackBase { };
typedef ::IceUtil::Handle< Callback_Printer_printString_Base> Callback_Printer_printStringPtr;
}
namespace IceProxy
{
namespace Demo
{
class Printer : virtual public ::IceProxy::Ice::Object
{
public:
    void printString(const ::std::string& s)
    {
        printString(s, 0);
    }
    void printString(const ::std::string& s, const ::Ice::Context& __ctx)
    {
        printString(s, &__ctx);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s)
    {
        return begin_printString(s, 0, ::IceInternal::__dummyCallback, 0);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s, const ::Ice::Context& __ctx)
    {
        return begin_printString(s, &__ctx, ::IceInternal::__dummyCallback, 0);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
    {
        return begin_printString(s, 0, __del, __cookie);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
    {
        return begin_printString(s, &__ctx, __del, __cookie);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s, const ::Demo::Callback_Printer_printStringPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
    {
        return begin_printString(s, 0, __del, __cookie);
    }
    ::Ice::AsyncResultPtr begin_printString(const ::std::string& s, const ::Ice::Context& __ctx, const ::Demo::Callback_Printer_printStringPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
    {
        return begin_printString(s, &__ctx, __del, __cookie);
    }
    void end_printString(const ::Ice::AsyncResultPtr&);
    
private:
    void printString(const ::std::string&, const ::Ice::Context*);
    ::Ice::AsyncResultPtr begin_printString(const ::std::string&, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);
    
public:
    
    ::IceInternal::ProxyHandle<Printer> ice_context(const ::Ice::Context& __context) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_context(__context).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_context(__context).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_adapterId(const std::string& __id) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_adapterId(__id).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_endpoints(__endpoints).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_locatorCacheTimeout(int __timeout) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_locatorCacheTimeout(__timeout).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_connectionCached(bool __cached) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_connectionCached(__cached).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_endpointSelection(::Ice::EndpointSelectionType __est) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_endpointSelection(__est).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_secure(bool __secure) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_secure(__secure).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_secure(__secure).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_preferSecure(bool __preferSecure) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_preferSecure(__preferSecure).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_router(const ::Ice::RouterPrx& __router) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_router(__router).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_router(__router).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_locator(const ::Ice::LocatorPrx& __locator) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_locator(__locator).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_locator(__locator).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_collocationOptimized(bool __co) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_collocationOptimized(__co).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_twoway() const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_twoway().get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_twoway().get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_oneway() const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_oneway().get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_oneway().get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_batchOneway() const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_batchOneway().get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_batchOneway().get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_datagram() const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_datagram().get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_datagram().get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_batchDatagram() const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_batchDatagram().get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_batchDatagram().get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_compress(bool __compress) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_compress(__compress).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_compress(__compress).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_timeout(int __timeout) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_timeout(__timeout).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());
    #endif
    }
    
    ::IceInternal::ProxyHandle<Printer> ice_connectionId(const std::string& __id) const
    {
    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6 compiler bug
        typedef ::IceProxy::Ice::Object _Base;
        return dynamic_cast<Printer*>(_Base::ice_connectionId(__id).get());
    #else
        return dynamic_cast<Printer*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());
    #endif
    }
    
    static const ::std::string& ice_staticId();
private: 
    virtual ::IceInternal::Handle< ::IceDelegateM::Ice::Object> __createDelegateM();
    virtual ::IceInternal::Handle< ::IceDelegateD::Ice::Object> __createDelegateD();
    virtual ::IceProxy::Ice::Object* __newInstance() const;
};
}
}
namespace IceDelegate
{
namespace Demo
{
class Printer : virtual public ::IceDelegate::Ice::Object
{
public:
    virtual void printString(const ::std::string&, const ::Ice::Context*) = 0;
};
}
}
namespace IceDelegateM
{
namespace Demo
{
class Printer : virtual public ::IceDelegate::Demo::Printer,
                virtual public ::IceDelegateM::Ice::Object
{
public:
    virtual void printString(const ::std::string&, const ::Ice::Context*);
};
}
}
namespace IceDelegateD
{
namespace Demo
{
class Printer : virtual public ::IceDelegate::Demo::Printer,
                virtual public ::IceDelegateD::Ice::Object
{
public:
    virtual void printString(const ::std::string&, const ::Ice::Context*);
};
}
}
namespace Demo
{
class Printer : virtual public ::Ice::Object
{
public:
    typedef PrinterPrx ProxyType;
    typedef PrinterPtr PointerType;
    
    virtual ::Ice::ObjectPtr ice_clone() const;
    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 printString(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) = 0;
    ::Ice::DispatchStatus ___printString(::IceInternal::Incoming&, const ::Ice::Current&);
    virtual ::Ice::DispatchStatus __dispatch(::IceInternal::Incoming&, const ::Ice::Current&);
    virtual void __write(::IceInternal::BasicStream*) const;
    virtual void __read(::IceInternal::BasicStream*, bool);
// COMPILERFIX: Stream API is not supported with VC++ 6
#if !defined(_MSC_VER) || (_MSC_VER >= 1300)
    virtual void __write(const ::Ice::OutputStreamPtr&) const;
    virtual void __read(const ::Ice::InputStreamPtr&, bool);
#endif
};
inline bool operator==(const Printer& l, const Printer& r)
{
    return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);
}
inline bool operator<(const Printer& l, const Printer& r)
{
    return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);
}
}
namespace Demo
{
template<class T>
class CallbackNC_Printer_printString : public Callback_Printer_printString_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_Printer_printString(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
        : ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)
    {
    }
};
template<class T> Callback_Printer_printStringPtr
newCallback_Printer_printString(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
    return new CallbackNC_Printer_printString<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_Printer_printStringPtr
newCallback_Printer_printString(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
    return new CallbackNC_Printer_printString<T>(instance, 0, excb, sentcb);
}
template<class T> Callback_Printer_printStringPtr
newCallback_Printer_printString(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
    return new CallbackNC_Printer_printString<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_Printer_printStringPtr
newCallback_Printer_printString(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
    return new CallbackNC_Printer_printString<T>(instance, 0, excb, sentcb);
}
template<class T, typename CT>
class Callback_Printer_printString : public Callback_Printer_printString_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_Printer_printString(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
        : ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)
    {
    }
};
template<class T, typename CT> Callback_Printer_printStringPtr
newCallback_Printer_printString(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_Printer_printString<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_Printer_printStringPtr
newCallback_Printer_printString(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
    return new Callback_Printer_printString<T, CT>(instance, 0, excb, sentcb);
}
template<class T, typename CT> Callback_Printer_printStringPtr
newCallback_Printer_printString(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_Printer_printString<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_Printer_printStringPtr
newCallback_Printer_printString(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
    return new Callback_Printer_printString<T, CT>(instance, 0, excb, sentcb);
}
}
#endif
3. 编写服务端代码:
///////////////////////////
// **********************************************************************
//
// Copyright (c) 2003-2010 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.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
class PrinterI : public Printer
{
public:
    virtual void printString(const string &, const Ice::Current&);
};
void 
PrinterI::
printString(const string &s, const Ice::Current&)
{
    cout << s << endl;
}
int
main(int argc, char* argv[])
{
    int status = 0;
    int dummy;
    dummy = 0;
    Ice::CommunicatorPtr ic;
    try
    {
        Ice::PropertiesPtr props = Ice::createProperties();
        props->setProperty("Ice.ThreadPool.Server.Size" , "10"); //设置线程池数
        props->setProperty("Ice.ThreadPool.Server.SizeMax" , "20"); //设置最大线程池数
        
        Ice::InitializationData ini_data;
        ini_data.properties = props;
        //ic = Ice::initialize(argc, argv);
        //ic = Ice::initialize( dummy , 0 , props);
        ic = Ice::initialize(ini_data); 
        Ice::ObjectAdapterPtr adapter =
            ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "tcp -p 10000");
        Ice::ObjectPtr object = new PrinterI;
        adapter->add(object, ic->stringToIdentity("SimplePrinter"));
        adapter->activate();
        ic->waitForShutdown();
    }
    catch(const Ice::Exception& e)
    {
        cerr << e << endl;
        status = 1;
    }
    catch(const char* msg)
    {
        cerr << msg << endl;
        status = 1;
    }
    if(ic)
    {
        try
        {
            ic->destroy();
        }
        catch(const Ice::Exception& e)
        {
            cerr << e << endl;
            status = 1;
        }
    }
    return status;
}
4.编写客户端代码:
// **********************************************************************
//
// Copyright (c) 2003-2010 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.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
int
main(int argc, char * argv[])
{
    int status = 0;
    Ice::CommunicatorPtr ic;
    try
    {
        Ice::PropertiesPtr props = Ice::createProperties();
        props->setProperty("Ice.Default.Host", "127.0.0.1"); //设置线程池数
       
        Ice::InitializationData ini_data;
        ini_data.properties = props;
        //ic = Ice::initialize(argc, argv);
        ic = Ice::initialize(ini_data);
        Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:tcp -p 10000");
        PrinterPrx printer = PrinterPrx::checkedCast(base);
        if(!printer)
        {
            throw "Invalid proxy";
        }
        printer->printString("Hello World! by ken 2010");
    }
    catch(const Ice::Exception& ex)
    {
        cerr << ex << endl;
        status = 1;
    }
    catch(const char* msg)
    {
        cerr << msg << endl;
        status = 1;
    }
    if(ic)
    {
        try
        {
            ic->destroy();
        }
        catch (const Ice::Exception& ex)
        {
            cerr << ex << endl;
            status = 1;
        }
    }
    return status;
}
运行结果:
Hello World! by ken 2010

 

你可能感兴趣的:(exception,网络,String,object,callback,compiler)