Click here to Skip to main content
12,622,741 members (28,488 online)
Click here to Skip to main content

Stats

1.8M views
7.5K downloads
326 bookmarked
Posted

RCF - Interprocess Communication for C++

, 25 Oct 2011 CPOL
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
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-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
//******************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005 - 2007. All rights reserved.
// Consult your license for conditions of use.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//******************************************************************************

#ifndef INCLUDE_RCF_FILTER_HPP
#define INCLUDE_RCF_FILTER_HPP

#include <string>
#include <vector>

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>

#include <RCF/ByteBuffer.hpp>

namespace RCF {

    // collect these constants here so we can avoid collisions
    static const int RCF_FILTER_UNKNOWN                         = 0;
    static const int RCF_FILTER_IDENTITY                        = 1;
    static const int RCF_FILTER_OPENSSL_ENCRYPTION              = 2;
    static const int RCF_FILTER_ZLIB_COMPRESSION_STATELESS      = 3;
    static const int RCF_FILTER_ZLIB_COMPRESSION_STATEFUL       = 4;
    static const int RCF_FILTER_SSPI_NTLM                       = 5;
    static const int RCF_FILTER_SSPI_KERBEROS                   = 6;
    static const int RCF_FILTER_SSPI_NEGOTIATE                  = 7;

    static const int RCF_FILTER_XOR                             = 101;

    /// Runtime description of a filter.
    class FilterDescription
    {
    public:
        FilterDescription(
            const std::string &filterName,
            int filterId,
            bool removable) :
                mFilterName(filterName),
                mFilterId(filterId),
                mRemovable(removable)
        {}

        const std::string &getName() const
        {
            return mFilterName;
        }

        int getId() const
        {
            return mFilterId;
        }

        bool getRemovable() const
        {
            return mRemovable;
        }

    private:
        const std::string mFilterName;
        const int mFilterId;
        const bool mRemovable;
    };

    class Filter;

    typedef boost::shared_ptr<Filter> FilterPtr;

    class Filter
    {
    public:

        Filter() :
            mpPreFilter(RCF_DEFAULT_INIT),
            mpPostFilter(RCF_DEFAULT_INIT)
        {}

        virtual ~Filter()
        {}

        virtual void reset()
        {}

        // TODO: for generality, should take a vector<ByteBuffer> &
        // (applicable if message arrives fragmented through the transport)
        // BTW, bytesRequested is meaningful if byteBuffer is empty
        virtual void read(
            const ByteBuffer &byteBuffer,
            std::size_t bytesRequested) = 0;
       
        virtual void write(const std::vector<ByteBuffer> &byteBuffers) = 0;

        // TODO: remove error parameter, use exceptions instead
        virtual void onReadCompleted(
            const ByteBuffer &byteBuffer,
            int error) = 0;

        virtual void onWriteCompleted(
            std::size_t bytesTransferred,
            int error) = 0;

        virtual const FilterDescription &getFilterDescription() const = 0;

        void setPreFilter(Filter &preFilter)
        {
            mpPreFilter = &preFilter;
        }

        void setPostFilter(Filter &postFilter)
        {
            mpPostFilter = &postFilter;
        }

    protected:

        Filter &getPreFilter()
        {
            return *mpPreFilter;
        }

        Filter &getPostFilter()
        {
            return *mpPostFilter;
        }

        Filter *mpPreFilter;
        Filter *mpPostFilter;
    };

    class IdentityFilter : public Filter
    {
    public:

        void read(const ByteBuffer &byteBuffer, std::size_t bytesRequested)
        {
            getPostFilter().read(byteBuffer, bytesRequested);
        }

        void write(const std::vector<ByteBuffer> &byteBuffers)
        {
            getPostFilter().write(byteBuffers);
        }

        void onReadCompleted(const ByteBuffer &byteBuffer, int error)
        {
            getPreFilter().onReadCompleted(byteBuffer, error);
        }

        void onWriteCompleted(std::size_t bytesTransferred, int error)
        {
            getPreFilter().onWriteCompleted(bytesTransferred, error);
        }

        void reset();
       
        const FilterDescription &getFilterDescription() const;
        static const FilterDescription &sGetFilterDescription();
        static const FilterDescription *spFilterDescription;
    };

    inline void createNonReadOnlyByteBuffers(
        std::vector<ByteBuffer> &nonReadOnlyByteBuffers,
        const std::vector<ByteBuffer> &byteBuffers)
    {
        nonReadOnlyByteBuffers.resize(0);
        for (std::size_t i=0; i<byteBuffers.size(); ++i)
        {
            if (byteBuffers[i].getLength()  > 0)
            {
                if (byteBuffers[i].getReadOnly())
                {
                    boost::shared_ptr< std::vector<char> > spvc(
                        new std::vector<char>( byteBuffers[i].getLength()));

                    memcpy(
                        &(*spvc)[0],
                        byteBuffers[i].getPtr(),
                        byteBuffers[i].getLength() );

                    nonReadOnlyByteBuffers.push_back(
                        ByteBuffer(&(*spvc)[0], spvc->size(), spvc));
                }
                else
                {
                    nonReadOnlyByteBuffers.push_back(byteBuffers[i]);
                }
            }
        }
    }

    class XorFilter : public IdentityFilter
    {
    public:
        static const FilterDescription *spFilterDescription;
        static const FilterDescription &sGetFilterDescription();
        const FilterDescription &getFilterDescription() const;
       
        XorFilter() :
            mMask('U')
        {}
       
        void read(const ByteBuffer &byteBuffer, std::size_t bytesRequested)
        {
            getPostFilter().read(byteBuffer, bytesRequested);
        }

        void write(const std::vector<ByteBuffer> &byteBuffers)
        {
            // need to make copies of any readonly buffers before transforming
            // TODO: only do this if at least one byte buffer is non-readonly

            createNonReadOnlyByteBuffers(mByteBuffers, byteBuffers);

            // inplace transformation
            for (std::size_t i=0; i<mByteBuffers.size(); ++i)
            {
                for (std::size_t j=0; j<mByteBuffers[i].getLength() ; ++j)
                {
                    mByteBuffers[i].getPtr() [j] ^= mMask;
                }
            }

            getPostFilter().write(mByteBuffers);
            mByteBuffers.resize(0);
        }

        void onReadCompleted(const ByteBuffer &byteBuffer, int error)
        {
            RCF_UNUSED_VARIABLE(error);
            // TODO: error handling
            for (std::size_t i=0; i<byteBuffer.getLength() ; ++i)
            {
                byteBuffer.getPtr() [i] ^= mMask;
            }
            getPreFilter().onReadCompleted(byteBuffer, 0);
        }

        void onWriteCompleted(std::size_t bytesTransferred, int error)
        {
            RCF_UNUSED_VARIABLE(error);
            // TODO: error handling
            getPreFilter().onWriteCompleted(bytesTransferred, 0);
        }

    private:
        std::vector<ByteBuffer> mByteBuffers;
        char mMask;
    };
   
    typedef boost::shared_ptr<Filter>                       FilterPtr;
    typedef std::vector<FilterPtr>                          VectorFilter;
    typedef boost::shared_ptr< std::vector<FilterPtr> >     VectorFilterPtr;

    class FilterFactory
    {
    public:
        virtual ~FilterFactory()
        {}

        virtual FilterPtr createFilter() = 0;

        virtual const FilterDescription &getFilterDescription() = 0; // TODO: should be const
    };

    typedef boost::shared_ptr<FilterFactory> FilterFactoryPtr;

    class IdentityFilterFactory : public FilterFactory
    {
    public:
        FilterPtr createFilter();
        const FilterDescription &getFilterDescription();
    };

    class XorFilterFactory : public FilterFactory
    {
    public:
        FilterPtr createFilter();
        const FilterDescription & getFilterDescription();
    };

    struct FilterIdComparison
    {
        bool operator()(FilterPtr filterPtr, int filterId)
        {
            return filterPtr->getFilterDescription().getId() == filterId;
        }
    };

    void connectFilters(const std::vector<FilterPtr> &filters);

    bool filterData(
        const std::vector<ByteBuffer> &unfilteredData,
        std::vector<ByteBuffer> &filteredData,
        const std::vector<FilterPtr> &filters);

    bool unfilterData(
        const ByteBuffer &filteredByteBuffer,
        std::vector<ByteBuffer> &unfilteredByteBuffers,
        std::size_t unfilteredDataLen,
        const std::vector<FilterPtr> &filters);

    bool unfilterData(
        const ByteBuffer &filteredByteBuffer,
        ByteBuffer &unfilteredByteBuffer,
        std::size_t unfilteredDataLen,
        const std::vector<FilterPtr> &filters);

} // namespace RCF

#endif // ! INCLUDE_RCF_FILTER_HPP

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.

You may also be interested in...

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