Click here to Skip to main content
11,417,750 members (50,913 online)
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
//******************************************************************************
// 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_TCPASIOSERVERTRANSPORT_HPP
#define INCLUDE_RCF_TCPASIOSERVERTRANSPORT_HPP

#include <vector>

#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#include <RCF/IpAddress.hpp>
#include <RCF/IpServerTransport.hpp>
#include <RCF/ServerTransport.hpp>
#include <RCF/Service.hpp>
#include <RCF/ThreadLibrary.hpp>

namespace RCF {

    class RcfServer;
    class TcpAsioSynchronizedSocket;

    typedef boost::asio::io_service             Demuxer;
    typedef boost::shared_ptr<Demuxer>          DemuxerPtr;
    typedef boost::asio::ip::tcp::acceptor      SocketAcceptor;
    typedef boost::shared_ptr<SocketAcceptor>   SocketAcceptorPtr;
    typedef boost::asio::deadline_timer         DeadlineTimer;
    typedef boost::shared_ptr<DeadlineTimer>    DeadlineTimerPtr;

    class TcpAsioServerTransport :
        public I_ServerTransport,
        public I_ServerTransportEx,
        public I_IpServerTransport,
        public I_Service
    {
    public:

        TcpAsioServerTransport(int port);
        ServerTransportPtr clone();

        typedef boost::asio::ip::tcp::socket    Socket;
        typedef boost::shared_ptr<Socket>       SocketPtr;

    private:

        typedef boost::weak_ptr<I_Session>              SessionWeakPtr;
        typedef TcpAsioSynchronizedSocket               SynchronizedSocket;
        typedef boost::shared_ptr<SynchronizedSocket>   SynchronizedSocketPtr;

        class TcpAsioProactor;

        class SessionState :
            public boost::enable_shared_from_this<SessionState>,
            boost::noncopyable
        {
        public:

            typedef boost::weak_ptr<SessionState>       SessionStateWeakPtr;
            typedef boost::shared_ptr<SessionState>     SessionStatePtr;

            SessionState(
                TcpAsioServerTransport &transport,
                DemuxerPtr demuxerPtr);

            ~SessionState();

            void            setSessionPtr(SessionPtr sessionPtr)    { mSessionPtr = sessionPtr; }
            SessionPtr      getSessionPtr()                         { return mSessionPtr; }

            typedef TcpAsioServerTransport::SocketPtr SocketPtr;
            void            forceClose();
            SocketPtr       getSocketPtr() { return mSocketPtr; }
            void            invokeAsyncAccept();

        private:

            void            read(const ByteBuffer &byteBuffer, std::size_t bytesRequested);
            void            write(const std::vector<ByteBuffer> &byteBuffers);

            void            onReadCompletion(const boost::asio::error &error, size_t bytesTransferred);
            void            onWriteCompletion(const boost::asio::error &error, size_t bytesTransferred);

            void            setTransportFilters(const std::vector<FilterPtr> &filters);
            const std::vector<FilterPtr> &getTransportFilters();
            void            invokeAsyncRead();
            void            invokeAsyncWrite();
            void            onAccept(const boost::asio::error& error);
            void            onReadWrite(size_t bytesTransferred, const boost::asio::error& error);
            void            onReflectedReadWrite(const boost::asio::error& error, size_t bytesTransferred);

            // TODO: too many friends
            friend class    TcpAsioServerTransport;
            friend class    TcpAsioProactor;
            friend class    FilterAdapter;

            enum State
            {
                Ready,
                Accepting,
                ReadingDataCount,
                ReadingData,
                WritingData
            };

            State                               mState;
            std::size_t                         mReadBufferRemaining;
            std::size_t                         mWriteBufferRemaining;
            SessionPtr                          mSessionPtr;
            std::vector<FilterPtr>              mTransportFilters;
            DemuxerPtr                          mDemuxerPtr;

            SocketPtr                           mSocketPtr;
            IpAddress                           mIpAddress;
            TcpAsioServerTransport &            mTransport;



            std::vector<ByteBuffer>             mWriteByteBuffers;

            std::vector<char> &         getReadBuffer();
            std::vector<char> &         getUniqueReadBuffer();
            ByteBuffer                  getReadByteBuffer() const;

            std::vector<char> &         getReadBufferSecondary();
            std::vector<char> &         getUniqueReadBufferSecondary();
            ByteBuffer                  getReadByteBufferSecondary() const;

            boost::shared_ptr<std::vector<char> > mReadBufferPtr;
            boost::shared_ptr<std::vector<char> > mReadBufferSecondaryPtr;

            ByteBuffer                  mTempByteBuffer;

            FilterPtr                   mFilterAdapterPtr;

            MutexAutoPtr                mMutexAutoPtr;
            bool                        mClosed;
            bool                        mSynchronized;
            SessionStateWeakPtr         mReflecteeWeakPtr;
            SessionStatePtr             mReflecteePtr;
            bool                        mReflecting;
        };

        typedef boost::shared_ptr<SessionState> SessionStatePtr;
        typedef boost::weak_ptr<SessionState> SessionStateWeakPtr;

        class TcpAsioProactor : public I_Proactor
        {
        private:
            // TODO: too many friends
            friend class SessionState;
            friend class TcpAsioServerTransport;

            TcpAsioProactor(TcpAsioServerTransport &tcpAsioServerTransport);

            void                        postRead();
            ByteBuffer                  getReadByteBuffer();
            void                        postWrite(const std::vector<ByteBuffer> &byteBuffers);
            void                        postClose();

            I_ServerTransport &         getServerTransport();
            const I_RemoteAddress &     getRemoteAddress();
            void                        setTransportFilters(const std::vector<FilterPtr> &filters);
            const std::vector<FilterPtr> &getTransportFilters();

            SessionStateWeakPtr         mSessionStateWeakPtr;
            TcpAsioServerTransport &    mTcpAsioServerTransport;
        };

        typedef boost::shared_ptr<TcpAsioProactor> TcpAsioProactorPtr;

        SessionStatePtr createSessionState();
        void forceCloseSession(SessionStateWeakPtr sessionStateWeakPtr, SocketPtr socketPtr);

        // I_IpServerTransport implementation
        int                 getPort() const;

        // I_ServerTransportEx implementation
        ClientTransportAutoPtr          createClientTransport(const I_Endpoint &endpoint);
        SessionPtr                      createServerSession(ClientTransportAutoPtr clientTransportAutoPtr);
        ClientTransportAutoPtr          createClientTransport(SessionPtr sessionPtr);
        bool                            reflect(const SessionPtr &sessionPtr1, const SessionPtr &sessionPtr2);
        bool                            isConnected(const SessionPtr &sessionPtr);

        // I_Service implementation
        void                open();
        void                close();
        bool                cycle(int timeoutMs, const volatile bool &stopFlag, bool returnEarly);
        void                stop();
        void                stopCycle(const boost::asio::error &error);
        void                onServiceAdded(RcfServer &server);
        void                onServiceRemoved(RcfServer &server);
        void                onServerOpen(RcfServer &server);
        void                onServerClose(RcfServer &server);
        void                onServerStart(RcfServer &server);
        void                onServerStop(RcfServer &server);
        void                setServer(RcfServer &server);

        RcfServer &         getServer();
        I_SessionManager &  getSessionManager();

    private:

        friend class        SessionState;
        friend class        FilterAdapter;

        DemuxerPtr          mDemuxerPtr;
        ReadWriteMutexPtr   mReadWriteMutexPtr;
        int                 mPort;
        SocketAcceptorPtr   mAcceptorPtr;
        DeadlineTimerPtr    mCycleTimerPtr;
        bool                mInterrupt;
        volatile bool       mStopFlag;
        RcfServer *         pServer;

    };

} // namespace RCF


#endif // ! INCLUDE_RCF_TCPASIOSERVERTRANSPORT_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 | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid