Skip to content

Latest commit

 

History

History
118 lines (96 loc) · 2.55 KB

README.md

File metadata and controls

118 lines (96 loc) · 2.55 KB

Ice for C++

Getting started | Examples | NuGet packages | Documentation | Building from source

The Ice framework provides everything you need to build networked applications, including RPC, pub/sub, server deployment, and more.

Ice for C++ is the C++ implementation of Ice.

Sample Code

// Slice definitions (Hello.ice)

#pragma once

module Demo
{
    interface Hello
    {
        void sayHello();
    }
}
// Client application (Client.cpp)

#include <Ice/Ice.h>
#include <Hello.h>

using namespace std;
using namespace Demo;

int
main(int argc, char* argv[])
{
    try
    {
        const Ice::CommunicatorHolder ich(argc, argv);
        HelloPrx hello{ich.communicator(), "hello:default -h localhost -p 10000"};
        hello->sayHello();
    }
    catch (const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return 1;
    }
    return 0;
}
// Server application (Server.cpp)

#include <Ice/Ice.h>
#include <Printer.h>

using namespace std;

int main(int argc, char* argv[])
{
    try
    {
        // CtrlCHandler must be created before the communicator or any other threads
        // are started
        Ice::CtrlCHandler ctrlCHandler;

        const Ice::CommunicatorHolder ich(argc, argv);
        const auto& communicator = ich.communicator();

        ctrlCHandler.setCallback(
            [communicator](int)
            {
                communicator->shutdown();
            });

        auto adapter = communicator->createObjectAdapterWithEndpoints(
            "Hello",
            "default -h localhost -p 10000");
        adapter->add(make_shared<HelloI>(), Ice::stringToIdentity("hello"));
        adapter->activate();
        communicator->waitForShutdown();
    }
    catch (const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return 1;
    }
    return 0;
}
// Printer implementation (Printer.h)

#include <Ice/Ice.h>
#include <Hello.h>

class Printer final : public Demo::Hello
{
public:
    /**
     * Prints a message to the standard output.
     **/
    void sayHello(const Ice::Current&) final
    {
        std::cout << "Hello World!" << std::endl;
    }
}