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

RCF - Interprocess Communication for C++

, 25 Oct 2011
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
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef INCLUDE_RCF_ASYNCFILTER_HPP
#define INCLUDE_RCF_ASYNCFILTER_HPP

#include <string>
#include <vector>

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

namespace RCF {

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

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

        int getId() const
        {
            return mFilterId;
        }

    private:
        std::string mFilterName;
        int mFilterId;
    };

    /// Interface for synchronous/asynchronous communications filters.
    class Filter
    {
    public:
        typedef boost::function2<void, char *, std::size_t>          ReadFunction;
        typedef boost::function2<void, const char *, std::size_t>    WriteFunction;
        typedef boost::function2<void, std::size_t, int>             ReadWriteCompletionCallback;

        /// Virtual destructor.
        virtual ~Filter()
        {}

        /// Reads data from the filter.
        /// \param buffer Buffer to transfer data to.
        /// \param bufferLen Maximum count of bytes to transfer.
        virtual void read(char *buffer, std::size_t bufferLen) = 0;

        /// Writes data to the filter.
        /// \param buffer Buffer from which to transfer data.
        /// \param bufferLen Maximum count of bytes to transfer.
        virtual void write(const char *buffer, std::size_t bufferLen) = 0;

        /// Callback, generally invoked by the next filter downstream in a filter chain, upon completion of a read or write request.
        /// \param bytesTransferred Count of bytes transferred.
        /// \param error -1 for error, otherwise 0.
        virtual void onReadWriteCompleted(std::size_t bytesTransferred, int error) = 0;

        /// Provides a runtime description of the filter.
        /// \return FilterDescription structure.
        virtual FilterDescription getFilterDescription() const = 0;

        // TODO: name these functions better (too ambiguous)

        /// Sets the downstream read function.
        virtual void setReadFunction( ReadFunction readFunction ) = 0;

        /// Sets the downstream write function.
        virtual void setWriteFunction( WriteFunction writeFunction ) = 0;

        /// Sets the upstream read/write completion callback.
        virtual void setReadWriteCompletionCallback( ReadWriteCompletionCallback readWriteCompletionCallback ) = 0;

        /// Gets a functor wrapping the filters read() function.
        virtual ReadFunction getReadFunction() = 0;

        /// Gets a functor wrapping the filters write() function.
        virtual WriteFunction getWriteFunction() = 0;

        /// Gets a functor wrapping the filters read/write completion callback.
        virtual ReadWriteCompletionCallback getReadWriteCompletionCallback() = 0;
    };

    static const int RCF_FILTER_IDENTITY = 1;
    static const int RCF_FILTER_XOR = 101;

    /// Trivial identity filter, applies no transformations at all. Also serves as a convenience base class for other filters.
    class IdentityFilter : public Filter
    {
    public:
        IdentityFilter();
        void read(char *buffer, std::size_t bufferLen);
        void write(const char *buffer, std::size_t bufferLen);
        void onReadWriteCompleted(std::size_t bytesTransferred, int error);
        void setReadFunction( ReadFunction readFunction );
        void setWriteFunction( WriteFunction writeFunction );
        void setReadWriteCompletionCallback( ReadWriteCompletionCallback readWriteCompletionCallback );
        ReadFunction getReadFunction();
        WriteFunction getWriteFunction();
        ReadWriteCompletionCallback getReadWriteCompletionCallback();
        FilterDescription getFilterDescription() const;
        static FilterDescription sGetFilterDescription();

    protected:
        ReadFunction mReadFunction;
        WriteFunction mWriteFunction;
        ReadWriteCompletionCallback mReadWriteCompletionCallback;
    };

    class XorFilter : public IdentityFilter
    {
    public:
        static FilterDescription sGetFilterDescription();
        FilterDescription getFilterDescription() const;
        XorFilter();
        void read(char *buffer, std::size_t bufferLen);
        void write(const char *buffer, std::size_t bufferLen);
        void onReadWriteCompleted(std::size_t bytesTransferred, int error);
    private:
        char mMask;
        char *mBuffer;
        std::size_t mBufferLen;
        std::vector<char> mPostBuffer;
        bool mReading;
    };
    
    typedef boost::shared_ptr<Filter> FilterPtr;

    /// Interface for filter factories.
    class FilterFactory
    {
    public:

        /// Virtual destructor.
        virtual ~FilterFactory()
        {}

        /// Creates a filter.
        /// \return Pointer to newly created filter.
        virtual FilterPtr createFilter() = 0;

        /// Returns a runtime description of the filters that this factory produces.
        /// \return FilterDescription structure.
        virtual FilterDescription getFilterDescription() = 0;
    };

    typedef boost::shared_ptr<FilterFactory> FilterFactoryPtr;

    /// Factory for IdentityFilter.
    class IdentityFilterFactory : public FilterFactory
    {
    public:
        FilterPtr createFilter();
        FilterDescription getFilterDescription();
    };

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

    /// Utility for comparing filters and filter id's.
    struct FilterIdComparison
    { 
        bool operator()(FilterPtr filterPtr, int filterId) 
        { 
            return filterPtr->getFilterDescription().getId() == filterId;
        } 
    };

    /// Connects a chain of filters (hooks up read and write functions of adjacent filters).
    /// \param filters A vector of filters to connect.
    void connectFilters(const std::vector<FilterPtr> &filters);
    
    /// Connects a chain of filters (hooks up read and write functions of adjacent filters).
    /// \param filters A vector of filters to connect.
    /// \param readFunction Read function to apply to the final filter.
    /// \param writeFunction Write function to apply to the final filter.
    /// \param readWriteCompletionCallback Callback to apply to the initial filter.
    void connectFilters(
        const std::vector<FilterPtr> &filters, 
        Filter::ReadFunction readFunction, 
        Filter::WriteFunction writeFunction, 
        Filter::ReadWriteCompletionCallback readWriteCompletionCallback);

    /// Filters data through the given filters.
    /// \param unfilteredData Unfiltered data.
    /// \param filteredData Filtered data.
    /// \param filters Filter chain to filter data through.
    bool filterData(
        const std::string &unfilteredData, 
        std::string &filteredData, 
        const std::vector<FilterPtr> &filters);

    /// Unfilters data through the given filters.
    /// \param filteredData Filtered data.
    /// \param unfilteredData Unfiltered data.
    /// \param unfilteredDataLen Length of unfiltered data.
    /// \param filters Filter chain to unfilter data through.
    bool unfilterData(
        const std::string &filteredData, 
        std::string &unfilteredData, 
        std::size_t unfilteredDataLen, 
        const std::vector<FilterPtr> &filters);

} // namespace RCF

#endif // ! INCLUDE_RCF_ASYNCFILTER_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.

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