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
//******************************************************************************
// 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_TCPIOCPSERVERTRANSPORT_HPP
#define INCLUDE_RCF_TCPIOCPSERVERTRANSPORT_HPP

#include <map>
#include <memory>
#include <vector>

#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

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

namespace RCF {

    class RcfServer;

    namespace TcpIocp {

        class Iocp;

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

        typedef int                                     Fd;
        typedef std::vector<ByteBuffer>                 ByteBuffers;
        typedef std::vector<FilterPtr>                  Filters;

        // Iocp

        class Iocp
        {
        public:
            Iocp(int nMaxConcurrency = -1);
            ~Iocp();

            BOOL Create(
                int nMaxConcurrency = 0);

            BOOL AssociateDevice(
                HANDLE hDevice,
                ULONG_PTR CompKey);

            BOOL AssociateSocket(
                SOCKET hSocket,
                ULONG_PTR CompKey);

            BOOL PostStatus(
                ULONG_PTR CompKey,
                DWORD dwNumBytes = 0,
                OVERLAPPED* po = NULL) ;

            BOOL GetStatus(
                ULONG_PTR* pCompKey,
                PDWORD pdwNumBytes,
                OVERLAPPED** ppo,
                DWORD dwMilliseconds = INFINITE);

        private:
            HANDLE m_hIOCP;
        };

        // FilterProxy

        class FilterProxy : public RCF::IdentityFilter
        {
        public:
            FilterProxy(
                SessionState &sessionState,
                Filter &filter,
                bool top);

        private:
            void read(
                const ByteBuffer &byteBuffer,
                std::size_t bytesRequested);

            void write(
                const std::vector<ByteBuffer> &byteBuffers);

            void onReadCompleted(
                const ByteBuffer &byteBuffer,
                int error);

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

            const FilterDescription &getFilterDescription() const
            {
                RCF_ASSERT(0);
                return * (const FilterDescription *) NULL;
            }

            SessionState &                      mSessionState;
            Filter &                            mFilter;
            bool                                mTop;
        };

        // SessionState
        class SessionState :
            public OVERLAPPED,
            public boost::enable_shared_from_this<SessionState>
        {
        public:

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

            enum PostState
            {
                Reading,
                Writing
            };

            SessionState(
                ServerTransport &transport,
                Fd fd);

            ~SessionState();

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

            const std::vector<FilterPtr> &getTransportFilters();

            void clearOverlapped();

            int read(
                ByteBuffer &byteBuffer,
                std::size_t bufferLen);

            int write(
                const std::vector<ByteBuffer> &byteBuffers);

            void onReadWriteCompleted(
                std::size_t bytesTransferred,
                int error);

            void onFilteredReadCompleted(
                const ByteBuffer &byteBuffer,
                int error);

            void onFilteredWriteCompleted(
                std::size_t bytesTransferred,
                int error);

            void onAcceptCompleted();

        private:

            friend class ServerTransport;
            friend class Proactor;
            friend class FilterProxy;

            int                         mError;
            std::vector<WSABUF>         mWsabufs;

            SessionStateWeakPtr         mReflectionSessionStateWeakPtr;

            State                       mState;
            PostState                   mPostState;
            SessionPtr                  mSessionPtr;

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

            ByteBuffer                  mTempByteBuffer;
            std::vector<ByteBuffer>     mWriteByteBuffers;
            std::size_t                 mReadBufferRemaining;
            std::vector<char>           mWriteBuffer;
            std::size_t                 mWriteBufferRemaining;
            const Fd                    mFd;
            std::vector<FilterPtr>      mTransportFilters;
            ServerTransport &           mTransport;
            IpAddress                   mLocalAddress;
            IpAddress                   mRemoteAddress;

            SessionStateWeakPtr         mWeakThisPtr;
            SessionStatePtr             mThisPtr;

            void wsaRecv(
                const ByteBuffer &,
                std::size_t);

            void wsaSend(
                const std::vector<ByteBuffer> &);

            void reflect(
                std::size_t bytesRead);

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

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

        private:
            bool                        mCloseAfterWrite;
            bool                        mReflected;

            bool                        mSynchronized;
            bool                        mOwnFd;
            bool                        mHasBeenClosed;
            MutexPtr                    mMutexPtr;
        };

        // Proactor

        class Proactor : public I_Proactor
        {
        public:
            Proactor(
                ServerTransport &transport,
                const SessionStatePtr &sessionStatePtr);

            void                        postRead();
            void                        postWrite(const ByteBuffers &byteBuffers);
            void                        postClose();
            ByteBuffer                  getReadByteBuffer();
            I_ServerTransport &         getServerTransport();
            SessionState &              getSessionState();
            SessionStatePtr             getSessionStatePtr() const;
            const I_RemoteAddress &     getRemoteAddress();
            void                        setTransportFilters(const Filters &filters);
            const std::vector<FilterPtr> &getTransportFilters();

        private:
            // need to break the cycle SessionState->Session->Proactor->SessionState
            boost::weak_ptr<SessionState>   sessionStatePtr;
            ServerTransport &               transport;
        };


        // ServerTransport

        class ServerTransport :
            public I_ServerTransport,
            public I_ServerTransportEx,
            public I_IpServerTransport,
            public I_Service,
            boost::noncopyable
        {
        private:

            typedef TcpIocp::SessionState               SessionState;
            typedef TcpIocp::Proactor                   Proactor;
            typedef TcpIocp::FilterProxy                FilterProxy;
            typedef TcpIocp::Iocp                       Iocp;
            typedef TcpIocp::SessionStatePtr            SessionStatePtr;
            typedef TcpIocp::SessionStateWeakPtr        SessionStateWeakPtr;
            typedef TcpIocp::Fd                         Fd;

            friend class TcpIocp::SessionState;
            friend class TcpIocp::Proactor;

        public:
            ServerTransport(int port = 0);
            ServerTransport(const std::string &networkInterface, int port = 0);

            ServerTransportPtr clone();

            void open();

            void close();

            void cycle(
                int timeoutMs,
                const volatile bool &stopFlag);

            bool cycleTransportAndServer(
                RcfServer &server,
                int timeoutMs,
                const volatile bool &stopFlag);

            void setPort(
                int port);

            int getPort() const;

            // this is the size going into wsasend()/wsarecv(), not the max message size!
            void setMaxSendRecvSize(
                std::size_t maxSendRecvSize);

            std::size_t getMaxSendRecvSize() const;

            void setMaxPendingConnectionCount(
                std::size_t maxPendingConnectionCount);

            std::size_t getMaxPendingConnectionCount() const;

            void setSessionManager(
                I_SessionManager &sessionManager);

            I_SessionManager &getSessionManager();

        private:

            SessionStatePtr createSession(
                int fd);

            void closeSession(
                const SessionStateWeakPtr &sessionStateWeakPtr,
                int fd = -1);

            void transition(
                const SessionStatePtr &sessionStatePtr);

            void stopAccepts();

            bool cycleAccepts(
                int timeoutMs,
                const volatile bool &stopFlag);

            void generateAccepts();

            void onReadWriteCompleted(
                const SessionStateWeakPtr &sessionStateWeakPtr,
                std::size_t bytesTransferred,
                int error);

            void onFilteredReadCompleted(
                const SessionStateWeakPtr &sessionStateWeakPtr,
                const ByteBuffer &byteBuffer,
                int error);

            void onFilteredWriteCompleted(
                const SessionStateWeakPtr &sessionStateWeakPtr,
                std::size_t bytesTransferred,
                int error);

            void postWrite(
                const SessionStatePtr &sessionStatePtr);

            void postWrite(
                const SessionStatePtr &sessionStatePtr,
                const std::vector<ByteBuffer> &byteBuffers);

            void postRead(
                const SessionStatePtr &sessionStatePtr);

            void flushIocp() const;

            // I_ServerTransportEx implementation
        private:

            ClientTransportAutoPtr createClientTransport(
                const I_Endpoint &endpoint);

            ClientTransportAutoPtr createClientTransport(
                SessionPtr sessionPtr);

            SessionPtr createServerSession(
                ClientTransportAutoPtr clientTransportAutoPtr);

            bool reflect(
                const SessionPtr &sessionPtr1,
                const SessionPtr &sessionPtr2);

            bool reflect(
                const SessionStatePtr &sessionStatePtr1,
                const SessionStatePtr &sessionStatePtr2);

            bool isConnected(const SessionPtr &sessionPtr);

            // I_Service implementation
        private:
            void onServiceAdded(RcfServer &server);
            void onServiceRemoved(RcfServer &server);
            void onServerStart(RcfServer &server);
            void onServerStop(RcfServer &server);
            void onServerOpen(RcfServer &server);
            void onServerClose(RcfServer &server);
            bool mOpen;

            // member variables
        private:

            I_SessionManager *          mpSessionManager;
            std::string                 mAcceptorInterface;
            int                         mAcceptorPort;
            Fd                          mAcceptorFd;
            int                         mPort;
            std::size_t                 mMaxPendingConnectionCount;
            std::size_t                 mMaxSendRecvSize;
            std::auto_ptr<Iocp>         mIocpAutoPtr;

            Mutex                       mQueuedAcceptsMutex;
            Condition                   mQueuedAcceptsCondition;

            // access via InterlockedIncrement()/InterlockedDecrement()
            unsigned int                mQueuedAccepts;

            const unsigned int          mQueuedAcceptsThreshold;
            const unsigned int          mQueuedAcceptsAugment;

            LPFN_ACCEPTEX               mlpfnAcceptEx;
            LPFN_GETACCEPTEXSOCKADDRS   mlpfnGetAcceptExSockAddrs;

            volatile bool               mStopFlag;
        };

    } // namespace TcpIocp

    typedef TcpIocp::ServerTransport                    TcpIocpServerTransport;

    typedef boost::shared_ptr<TcpIocpServerTransport>   TcpIocpServerTransportPtr;

} // namespace RCF

#endif // ! INCLUDE_RCF_TCPIOCPSERVERTRANSPORT_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
Web03 | 2.8.150327.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid