Click here to Skip to main content
15,896,330 members
Articles / Programming Languages / C++

RCF - Interprocess Communication for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (147 votes)
25 Oct 2011CPOL20 min read 4.7M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
//******************************************************************************
// 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_METHODINVOCATION_HPP
#define INCLUDE_RCF_METHODINVOCATION_HPP

#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>

#include <RCF/ByteBuffer.hpp>
#include <RCF/AsyncFilter.hpp>
#include <RCF/Token.hpp>

namespace RCF {

    class RcfServer;
    class StubEntry;
    typedef boost::shared_ptr<StubEntry> StubEntryPtr;
    class RcfSession;
    typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
    class SerializationProtocolIn;
    class SerializationProtocolOut;

    class MethodInvocationResponse;

    // message types
    static const int Descriptor_Error               = 0;
    static const int Descriptor_Request             = 1;
    static const int Descriptor_Response            = 2;
    static const int Descriptor_FilteredPayload     = 3;

    // encoding and decoding
    void encodeBool(bool value, std::vector<char> &vec, std::size_t &pos);
    void encodeInt(int value, std::vector<char> &vec, std::size_t &pos);
    void encodeString(const std::string &value, std::vector<char> &vec, std::size_t &pos);

    void encodeBool(bool value, const ByteBuffer &byteBuffer, std::size_t &pos);
    void encodeInt(int value, const ByteBuffer &byteBuffer, std::size_t &pos);
    void encodeString(const std::string &value, const ByteBuffer &byteBuffer, std::size_t &pos);

    void decodeBool(bool &value, const ByteBuffer &byteBuffer, std::size_t &pos);
    void decodeInt(int &value, const ByteBuffer &byteBuffer, std::size_t &pos);
    void decodeString(std::string &value, const ByteBuffer &byteBuffer, std::size_t &pos);

    class MethodInvocationRequest
    {
    public:
        MethodInvocationRequest();

        void init(
            const Token &token,
            const std::string &service,
            const std::string &subInterface,
            int fnId,
            int serializationProtocol,
            bool oneway,
            bool close,
            int rcfRuntimeVersion,
            bool ignoreRcfRuntimeVersion);

        Token getToken() const;
        std::string getSubInterface() const;
        int getFnId() const;
        bool getOneway() const;
        bool getClose() const;
        std::string getService() const;
        void setService(const std::string &service);

        ByteBuffer encodeRequestHeader();

        void encodeRequest(
            const std::vector<ByteBuffer> &byteBuffersIn,
            std::vector<ByteBuffer> &byteBuffersOut,
            const std::vector<FilterPtr> &filters);

        bool decodeRequest(
            const ByteBuffer &byteBufferIn,
            RcfSessionPtr rcfSessionPtr,
            RcfServer &rcfServer);

        void encodeResponse(
            const std::vector<ByteBuffer> &byteBuffersIn,
            std::vector<ByteBuffer> &byteBuffersOut,
            const std::vector<FilterPtr> &filters,
            bool isException);

        void decodeResponse(
            const ByteBuffer &byteBufferIn,
            ByteBuffer &byteBufferOut,
            MethodInvocationResponse &response,
            const std::vector<FilterPtr> &filters);

        StubEntryPtr locateStubEntryPtr(RcfServer &rcfServer);

    private:

        void decodeFiltered(
            const ByteBuffer &encodedByteBuffer,
            ByteBuffer &byteBufferClear,
            ByteBuffer &byteBufferUnfiltered,
            RcfServer *pRcfServer,
            RcfSessionPtr rcfSessionPtr,
            const std::vector<FilterPtr> &filters);

        void encodeFiltered(
            std::vector<ByteBuffer> &byteBuffersIn,
            const std::vector<ByteBuffer> &byteBuffersOut,
            const std::vector<FilterPtr> &filters);

        Token           mToken;
        std::string     mSubInterface;
        int             mFnId;
        int             mSerializationProtocol;
        bool            mOneway;
        bool            mClose;
        std::string     mService;
        int                mRcfRuntimeVersion;
        bool            mIgnoreRcfRuntimeVersion;

        boost::shared_ptr<std::vector<char> > mVecPtr;
    };

    class MethodInvocationResponse
    {
    public:
        MethodInvocationResponse();

        bool isException() const;
        bool isError() const;
        int getError() const;
        int getArg0() const;

    private:
        friend class MethodInvocationRequest;

        bool            mException;
        bool            mError;
        int             mErrorCode;
        int                mArg0;
    };

} // namespace RCF

#endif // ! INCLUDE_RCF_METHODINVOCATION_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)


Written By
Australia Australia
Software developer, from Sweden and now living in Canberra, Australia, working on distributed C++ applications. When he is not programming, Jarl enjoys skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

Comments and Discussions