Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RCF - Interprocess Communication for C++

, 25 Oct 2011 CPOL
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
rcf-04.zip
RCF-0.4
demo
vs2003
RCF
Client
Server
doc
html
class_r_c_f_1_1_exception.png
class_r_c_f_1_1_filter.png
class_r_c_f_1_1_filter_factory.png
class_r_c_f_1_1_filter_service.png
class_r_c_f_1_1_identity_filter.png
class_r_c_f_1_1_identity_filter_factory.png
class_r_c_f_1_1_i___client_transport.png
class_r_c_f_1_1_i___endpoint.png
class_r_c_f_1_1_i___service.png
class_r_c_f_1_1_i___session_manager.png
class_r_c_f_1_1_multicast_client_transport.png
class_r_c_f_1_1_object_factory_service.png
class_r_c_f_1_1_open_ssl_encryption_filter.png
class_r_c_f_1_1_open_ssl_encryption_filter_factory.png
class_r_c_f_1_1_publishing_service.png
class_r_c_f_1_1_rcf_server.png
class_r_c_f_1_1_remote_exception.png
class_r_c_f_1_1_subscription_service.png
class_r_c_f_1_1_tcp_endpoint.png
class_r_c_f_1_1_udp_endpoint.png
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.png
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.png
doxygen.png
ftv2blank.png
ftv2doc.png
ftv2folderclosed.png
ftv2folderopen.png
ftv2lastnode.png
ftv2link.png
ftv2mlastnode.png
ftv2mnode.png
ftv2node.png
ftv2plastnode.png
ftv2pnode.png
ftv2vertline.png
tab_b.gif
tab_l.gif
tab_r.gif
latex
annotated.tex
class_r_c_f_1_1_client_stub.tex
class_r_c_f_1_1_exception.eps
class_r_c_f_1_1_exception.tex
class_r_c_f_1_1_filter.eps
class_r_c_f_1_1_filter.tex
class_r_c_f_1_1_filter_description.tex
class_r_c_f_1_1_filter_factory.eps
class_r_c_f_1_1_filter_factory.tex
class_r_c_f_1_1_filter_service.eps
class_r_c_f_1_1_filter_service.tex
class_r_c_f_1_1_identity_filter.eps
class_r_c_f_1_1_identity_filter.tex
class_r_c_f_1_1_identity_filter_factory.eps
class_r_c_f_1_1_identity_filter_factory.tex
class_r_c_f_1_1_i___client_transport.eps
class_r_c_f_1_1_i___client_transport.tex
class_r_c_f_1_1_i___endpoint.eps
class_r_c_f_1_1_i___endpoint.tex
class_r_c_f_1_1_i___proactor.tex
class_r_c_f_1_1_i___rcf_client.tex
class_r_c_f_1_1_i___server_transport.tex
class_r_c_f_1_1_i___server_transport_ex.tex
class_r_c_f_1_1_i___service.eps
class_r_c_f_1_1_i___service.tex
class_r_c_f_1_1_i___session.tex
class_r_c_f_1_1_i___session_manager.eps
class_r_c_f_1_1_i___session_manager.tex
class_r_c_f_1_1_multicast_client_transport.eps
class_r_c_f_1_1_multicast_client_transport.tex
class_r_c_f_1_1_object_factory_service.eps
class_r_c_f_1_1_object_factory_service.tex
class_r_c_f_1_1_open_ssl_encryption_filter.eps
class_r_c_f_1_1_open_ssl_encryption_filter.tex
class_r_c_f_1_1_open_ssl_encryption_filter_factory.eps
class_r_c_f_1_1_open_ssl_encryption_filter_factory.tex
class_r_c_f_1_1_publishing_service.eps
class_r_c_f_1_1_publishing_service.tex
class_r_c_f_1_1_rcf_server.eps
class_r_c_f_1_1_rcf_server.tex
class_r_c_f_1_1_remote_exception.eps
class_r_c_f_1_1_remote_exception.tex
class_r_c_f_1_1_subscription_service.eps
class_r_c_f_1_1_subscription_service.tex
class_r_c_f_1_1_tcp_endpoint.eps
class_r_c_f_1_1_tcp_endpoint.tex
class_r_c_f_1_1_udp_endpoint.eps
class_r_c_f_1_1_udp_endpoint.tex
class_r_c_f_1_1_zlib_stateful_compression_filter.tex
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.tex
class_r_c_f_1_1_zlib_stateless_compression_filter.tex
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.tex
dirs.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2FProtocol_2F.tex
doxygen.sty
Helvetica.ttf
hierarchy.tex
Makefile
refman.tex
struct_r_c_f_1_1_filter_id_comparison.tex
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
SF
test
borland
Jamfile
Jamrules
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
client.pem
server.pem
rcf-09c.zip
RCF-0.9c
demo
vs2003
RCF
Client
Server
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
bcc
Jamfile
Jamrules
data
caCertA.pem
caCertB.pem
certA.pem
certB.pem
ssCert1.pem
ssCert2.pem
Jamfile
Jamrules
vc6
Jamfile
Jamrules
// uncomment to enable VLD leak detection - will automatically link to required libs
//#include "vld.h"
//#include "vldapi.h"

#include <string>
#include <strstream>

#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>

#include <RCF/test/TestMinimal.hpp>

#include <RCF/AsyncFilter.hpp>
#include <RCF/FilterService.hpp>
#include <RCF/Idl.hpp>
#include <RCF/OpenSslEncryptionFilter.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/ZlibCompressionFilter.hpp>

#include <RCF/test/EndpointFactories.hpp>
#include <RCF/test/TransportFactories.hpp>
#include <RCF/util/CommandLine.hpp>
#include <RCF/util/Profile.hpp>

#include <SF/AdlWorkaround.hpp>

#ifdef BOOST_WINDOWS
#include <RCF/SspiFilter.hpp>
#endif

namespace Test_ZeroAllocation {

    class ContainsByteBuffer
    {
    public:
        RCF::ByteBuffer mByteBuffer;

        template<typename Archive>
        void serialize(Archive &archive, const unsigned int)
        {
            archive & mByteBuffer;
        }
    };

    class Echo
    {
    public:
        std::string echo(const std::string &s)
        {
            return s;
        }

        RCF::ByteBuffer echo(RCF::ByteBuffer byteBuffer1, const std::string &s, RCF::ByteBuffer byteBuffer2)
        {
            void *pv1 = byteBuffer1.getPtr() ;
            std::size_t pvlen1 = byteBuffer1.getLength() ;

            void *pv2 = byteBuffer2.getPtr() ;
            std::size_t pvlen2 = byteBuffer2.getLength() ;

            return byteBuffer2;
        }

        RCF::ByteBuffer echo(RCF::ByteBuffer byteBuffer)
        {
            return byteBuffer;
        }

        ContainsByteBuffer echo(ContainsByteBuffer c)
        {
            return c;
        }

    };

    RCF_BEGIN(I_Echo, "I_Echo")
        RCF_METHOD_R1(std::string, echo, const std::string &)
        RCF_METHOD_R3(RCF::ByteBuffer, echo, RCF::ByteBuffer, std::string, RCF::ByteBuffer)
        RCF_METHOD_R1(RCF::ByteBuffer, echo, RCF::ByteBuffer)
        RCF_METHOD_R1(ContainsByteBuffer, echo, ContainsByteBuffer)
    RCF_END(I_Echo)

    bool gExpectAllocations = true;
    std::size_t gnAllocations = 0;

} // namespace Test_ZeroAllocation

// User-defined operator new.
void *operator new(size_t bytes)
{
    BOOST_CHECK(Test_ZeroAllocation::gExpectAllocations);
    ++Test_ZeroAllocation::gnAllocations;
    return malloc(bytes);
}

// User-defined operator delete.
void operator delete(void *pv) throw()
{
    free(pv);
}

RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Test_ZeroAllocation::ContainsByteBuffer)

SF_ADL_WORKAROUND(RCF, ByteBuffer)

int RCF_TEST_MAIN(int argc, char **argv)
{

    printTestHeader(__FILE__);

    using namespace Test_ZeroAllocation;

    util::CommandLineOption<std::string>    clIp("ip", util::PortNumbers::getSingleton().getIp(), "ip");
    util::CommandLineOption<int>            clPort("port", util::PortNumbers::getSingleton().getCurrent(), "port");
    util::CommandLineOption<std::string>    clScert("scert", TEMP_DIR "ssCert2.pem", "OpenSSL server certificate");
    util::CommandLineOption<std::string>    clSpwd("spwd", "mt2316", "OpenSSL server certificate password");
    util::CommandLineOption<std::string>    clCcert("ccert", TEMP_DIR "ssCert1.pem", "OpenSSL client certificate");
    util::CommandLineOption<std::string>    clCpwd("cpwd", "mt2316", "OpenSSL client certificate password");
    util::CommandLine::getSingleton().parse(argc, argv);

    // TODO: make ip and port options part of util::PortNumbers
    util::PortNumbers::getSingleton().setCurrent(clPort);
    util::PortNumbers::getSingleton().setIp(clIp);

#if !defined(BOOST_WINDOWS)
    BOOST_CHECK(1 == 0 && "Zero-allocation not yet supported with asio transports");
    return boost::exit_success;
#endif

#if defined(BOOST_WINDOWS) && defined(__MINGW32__) && __GNUC__ == 3 && __GNUC_MINOR__ == 4
    BOOST_CHECK(1 == 0 && "Zero-allocation not working in gcc 3.4 for unknown reasons");
    return boost::exit_success;
#endif

    // TODO: run this test over all transports
    for (unsigned int i=0; i<RCF::getTransportFactories().size(); ++i)
    {

        RCF::TransportFactoryPtr transportFactoryPtr( RCF::getTransportFactories()[i] );
        std::pair<RCF::ServerTransportPtr, RCF::ClientTransportAutoPtrPtr> transports = transportFactoryPtr->createTransports();
        RCF::ServerTransportPtr serverTransportPtr( transports.first );
        RCF::ClientTransportAutoPtr clientTransportAutoPtr( *transports.second );
        bool transportFiltersSupported = transportFactoryPtr->isConnectionOriented();

#ifdef RCF_USE_BOOST_ASIO
        // TODO
        if (typeid(*serverTransportPtr) == typeid(RCF::TcpAsioServerTransport))
        {
            // still got some work to do on zero allocation
            continue;
        }
#endif

        RCF::writeTransportTypes(std::cout, *serverTransportPtr, *clientTransportAutoPtr);
        std::string transportDesc = "Transport " + boost::lexical_cast<std::string>(i) + ": ";

        serverTransportPtr->setMaxMessageLength(-1);
        clientTransportAutoPtr->setMaxMessageLength(-1);

        Echo echo;
        RCF::RcfServer server(serverTransportPtr);
        server.bind( (I_Echo*) 0, echo);

        RCF::FilterServicePtr filterServicePtr(new RCF::FilterService());
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::IdentityFilterFactory()));
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::XorFilterFactory()));
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::ZlibStatefulCompressionFilterFactory()));
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::ZlibStatelessCompressionFilterFactory()));
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::OpenSslEncryptionFilterFactory(clScert, clSpwd)));
#ifdef BOOST_WINDOWS
        filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::SspiNtlmFilterFactory()));
#endif
        server.addService(filterServicePtr);
       
        server.start();
       
        // make sure all allocations have taken place
        Platform::OS::SleepMs(1000);

        RcfClient<I_Echo> client(clientTransportAutoPtr->clone());
       
        //client.getClientStub().setRemoteCallTimeoutMs(1000*60*60);

        {
            std::size_t nAllocations = gnAllocations;
            std::auto_ptr<int> apn(new int(17));
            apn.reset();
            BOOST_CHECK(gnAllocations != nAllocations);
        }

        {
            std::string s = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";
            RCF::ByteBuffer byteBuffer0( (char*) s.c_str(), s.length());

            std::vector<RCF::FilterPtr> filters;

            // with no transport or payload filters

            // prime the pump
            client.echo(byteBuffer0);
            Platform::OS::SleepMs(1000);

            {
                std::string s0 = byteBuffer0.string();
                gExpectAllocations = false;
                RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                gExpectAllocations = true;
                std::string s1 = byteBuffer1.string();
                BOOST_CHECK(s0 == s1);
            }

            {
                util::Profile profile(transportDesc + "1000 calls, no dynamic allocations");
                gExpectAllocations = false;
                for(unsigned int i=0; i<1000; ++i)
                {
                    RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                }
                gExpectAllocations = true;
            }

            // with both transport and payload filters

            if (transportFiltersSupported)
            {
                filters.clear();
                filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
                filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
                filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
                client.getClientStub().requestTransportFilters(filters);
            }

            filters.clear();
            filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
            filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
            filters.push_back( RCF::FilterPtr( new RCF::XorFilter()));
            client.getClientStub().setMessageFilters(filters);

            // prime the pump
            client.echo(byteBuffer0);
            Platform::OS::SleepMs(1000);

            for (int i=0; i<3; ++i)
            {
                // byteBuffer0 will be transformed in place, so we need to be a bit careful with the before/after comparison.
                // s0 and s1 will change on each pass.

                std::string s0 = byteBuffer0.string();
                gExpectAllocations = false;
                RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                gExpectAllocations = true;
                std::string s1 = byteBuffer1.string();
                BOOST_CHECK(s0 == s1);
            }

            {
                util::Profile profile(transportDesc + "1000 calls, no dynamic allocations, 3 transport filters + 3 payload filters");
                gExpectAllocations = false;
                for(unsigned int i=0; i<1000; ++i)
                {
                    RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                }
                gExpectAllocations = true;
            }
           
            filters.clear();
            filters.push_back( RCF::FilterPtr( new RCF::ZlibStatelessCompressionFilter()));
            client.getClientStub().setMessageFilters(filters);
            client.echo(byteBuffer0);

            {
                util::Profile profile(transportDesc + "1000 calls, no dynamic allocations, <zlib stateless> payload filters");
                gExpectAllocations = false;
                for(unsigned int i=0; i<1000; ++i)
                {
                    RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                }
                gExpectAllocations = true;
            }
           

            if (transportFiltersSupported)
            {
               
                filters.clear();
                filters.push_back( RCF::FilterPtr( new RCF::ZlibStatefulCompressionFilter()));
                filters.push_back( RCF::FilterPtr( new RCF::OpenSslEncryptionFilter(clCcert, clCpwd)));
                client.getClientStub().requestTransportFilters(filters);
               
                client.echo(byteBuffer0);

                util::Profile profile(transportDesc + "1000 calls, <zlib stateful><OpenSSL> transport filter");
                gExpectAllocations = false;
                for(unsigned int i=0; i<1000; ++i)
                {
                    RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                }
                gExpectAllocations = true;
            }

#ifdef BOOST_WINDOWS

            if (transportFiltersSupported)
            {
                filters.clear();
                filters.push_back( RCF::FilterPtr( new RCF::ZlibStatefulCompressionFilter()));
                filters.push_back( RCF::FilterPtr( new RCF::SspiNtlmFilter()));
                client.getClientStub().requestTransportFilters(filters);
                client.echo(byteBuffer0);

                util::Profile profile(transportDesc + "1000 calls, <zlib stateful><sspi ntlm> transport filter");
                gExpectAllocations = false;
                for(unsigned int i=0; i<1000; ++i)
                {
                    RCF::ByteBuffer byteBuffer1 = client.echo(byteBuffer0);
                }
                gExpectAllocations = true;
            }

#endif

            {
                // try serialization (not marshalling) of ByteBuffer
                ContainsByteBuffer c1;
                c1.mByteBuffer = byteBuffer0;
                ContainsByteBuffer c2 = client.echo(c1);

                gExpectAllocations = false;
                c2.mByteBuffer.clear();
                c2 = client.echo(c1);
                gExpectAllocations = true;
            }

            {
                // try serialization (not marshalling) of ByteBuffer with all serialization protocols
                for(int protocol=1; protocol<10; ++protocol)
                {
                    if (RCF::isSerializationProtocolSupported(protocol))
                    {
                        client.getClientStub().setSerializationProtocol(protocol);

                        ContainsByteBuffer c1;
                        c1.mByteBuffer = byteBuffer0;
                        ContainsByteBuffer c2 = client.echo(c1);
                        BOOST_CHECK(c2.mByteBuffer.getLength() == c1.mByteBuffer.getLength());

                        // will get memory allocations here when using boost serialization
                        c2.mByteBuffer.clear();
                        c2 = client.echo(c1);
                        BOOST_CHECK(c2.mByteBuffer.getLength() == c1.mByteBuffer.getLength());
                    }
                }
            }
           
        }
        server.stop();
    }

    return boost::exit_success;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid