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
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 - 2007. All rights reserved.
// Consult your license for conditions of use.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//******************************************************************************

#ifndef INCLUDE_RCF_BYTEBUFFER_HPP
#define INCLUDE_RCF_BYTEBUFFER_HPP

#include <strstream>
#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>

#include <RCF/Tools.hpp>
#include <RCF/TypeTraits.hpp>

#include <RCF/ThreadLocalData.hpp>
#include <RCF/util/Platform/OS/BsdSockets.hpp> // WSABUF

namespace RCF {

    // ByteBuffer class for facilitating zero-copy transmission and reception

    class ByteBuffer
    {
    public:

        ByteBuffer();

        ByteBuffer(std::size_t pvlen);

        ByteBuffer(
            const boost::shared_ptr<std::vector<char> > &spvc,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            std::size_t leftMargin,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            const boost::shared_ptr<std::ostrstream> &spos,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            std::size_t leftMargin,
            const boost::shared_ptr<std::ostrstream> &spos,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            const boost::shared_ptr<std::vector<char> > &spvc,
            bool readOnly = false);

        ByteBuffer(
            char *pv,
            std::size_t pvlen,
            std::size_t leftMargin,
            const boost::shared_ptr<std::vector<char> > &spvc,
            bool readOnly = false);

        ByteBuffer(
            const ByteBuffer &byteBuffer,
            std::size_t offset = 0,
            std::size_t len = -1);

        char *getPtr() const;
        std::size_t getLength() const;
        std::size_t getLeftMargin() const;
        bool getReadOnly() const;
        bool isEmpty() const;
        void expandIntoLeftMargin(std::size_t len);
        std::string string() const;
        ByteBuffer release();
        void clear();

        operator bool()
        {
            return getLength() != 0;
        }

        bool operator !()
        {
            return getLength() == 0;
        }

    private:
        // sentries
        boost::shared_ptr< std::vector<char> >    mSpvc;
        boost::shared_ptr< std::ostrstream >    mSpos;

        char *                                    mPv;
        std::size_t                                mPvlen;
        std::size_t                                mLeftMargin;
        bool                                    mReadOnly;

    };

    inline bool operator==(const ByteBuffer &lhs, const ByteBuffer &rhs)
    {
        return
            lhs.getPtr() == rhs.getPtr() &&
            lhs.getLength() == rhs.getLength();
    }

    std::size_t lengthByteBuffers(const std::vector<ByteBuffer> &byteBuffers);

    template<typename Functor>
    inline void forEachByteBuffer(
        const Functor &functor,
        const std::vector<ByteBuffer> &byteBuffers,
        std::size_t offset,
        std::size_t length = -1)
    {
        std::size_t pos0        = 0;
        std::size_t pos1        = 0;
        std::size_t remaining   = length;

        for (std::size_t i=0; i<byteBuffers.size(); ++i)
        {
            pos1 = pos0 + byteBuffers[i].getLength() ;

            if (pos1 <= offset)
            {
                pos0 = pos1;
            }
            else if (pos0 <= offset && offset < pos1)
            {
                std::size_t len = RCF_MIN(pos1-offset, remaining);

                ByteBuffer byteBuffer(
                    byteBuffers[i],
                    offset-pos0,
                    len);

                functor(byteBuffer);
                pos0 = pos1;
                remaining -= len;
            }
            else if (remaining > 0)
            {
                std::size_t len = RCF_MIN(pos1-pos0, remaining);

                ByteBuffer byteBuffer(
                    byteBuffers[i],
                    0,
                    len);

                functor(byteBuffer);
                pos1 = pos0;
                remaining -= len;
            }
        }
    }

    void sliceByteBuffers(
        std::vector<ByteBuffer> &slicedBuffers,
        const std::vector<ByteBuffer> &byteBuffers,
        std::size_t offset,
        std::size_t length = -1);

    void copyByteBuffers(
        const std::vector<ByteBuffer> &byteBuffers,
        char *pch);

    void copyByteBuffers(
        const std::vector<ByteBuffer> &byteBuffers,
        ByteBuffer &byteBuffer);

    // Thread local caching

    template<typename T>
    struct CacheType
    {
        typedef
            std::vector<
                std::pair<
                    boost::shared_ptr< bool>,
                    boost::shared_ptr< T > > > Val;
    };

#ifndef BOOST_WINDOWS
    typedef iovec WSABUF;
#endif

    class ObjectCache
    {
    public:

        typedef CacheType< std::vector<RCF::ByteBuffer> >::Val  VectorByteBufferCache;
        typedef CacheType< std::vector<int> >::Val              VectorIntCache;
        typedef CacheType< std::vector<WSABUF> >::Val           VectorWsabufCache;

        VectorByteBufferCache &getCache(VectorByteBufferCache *)
        {
            return mVectorByteBufferCache;
        }

        VectorIntCache &getCache(VectorIntCache *)
        {
            return mVectorIntCache;
        }

        VectorWsabufCache &getCache(VectorWsabufCache *)
        {
            return mVectorWsabufCache;
        }

        void clear()
        {
            mVectorByteBufferCache.clear();
            mVectorIntCache.clear();
            mVectorWsabufCache.clear();
        }

    private:
        VectorByteBufferCache   mVectorByteBufferCache;
        VectorIntCache          mVectorIntCache;
        VectorWsabufCache       mVectorWsabufCache;
    };

    //typedef boost::shared_ptr<ObjectCache> ObjectCachePtr;
    // TODO: in its own header
    //class ThreadLocalData;
    //typedef boost::shared_ptr<ThreadLocalData> ThreadLocalDataPtr;
    //ThreadLocalDataPtr getThreadLocalDataPtr();

    //ObjectCache &getThreadLocalObjectCache();

    template<typename T>
    class ThreadLocalCached
    {
    public:

        typedef typename CacheType<T>::Val TCache;

        ThreadLocalCached()
        {
            //ObjectCachePtr objectCachePtr = getThreadLocalObjectCachePtr();
            ObjectCache &objectCache = getThreadLocalObjectCache();

            TCache &tCache = objectCache.getCache( (TCache *) NULL);
            for (std::size_t i=0; i<tCache.size(); ++i)
            {
                if (*tCache[i].first == false)
                {
                    mMarkPtr = tCache[i].first;
                    mtPtr = tCache[i].second;
                    break;
                }
            }
            if (!mtPtr)
            {
                typedef typename TCache::value_type ValueType;
                tCache.push_back( ValueType());
                tCache.back().first.reset( new bool());
                tCache.back().second.reset( new T());
                mMarkPtr = tCache.back().first;
                mtPtr = tCache.back().second;
            }
            *mMarkPtr = true;
        }

        ~ThreadLocalCached()
        {
            *mMarkPtr = false;
            mtPtr->resize(0);
        }

        T &get()
        {
            return *mtPtr;
        }

    private:

        boost::shared_ptr< bool> mMarkPtr;
        boost::shared_ptr< T > mtPtr;

    };

} // namespace RCF

RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(RCF::ByteBuffer)

#endif // ! INCLUDE_RCF_BYTEBUFFER_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
Web03 | 2.8.140827.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid