Click here to Skip to main content
12,626,517 members (35,687 online)
Click here to Skip to main content

Stats

577.9K views
4.2K downloads
149 bookmarked
Posted

RMI for C++

, 6 Aug 2009 CPOL
User-friendly remote method invocation in C++.
RCF
demo
vs2003
RCF
Client
Server
doc
include
RCF
Connection.inl
Marshal.inl
Multiplexer.inl
RCF.inl
RcfServer.inl
ServerStub.inl
Protocol
util
Platform
Compiler
bcc55
cw80
gcc32
icl70
msvc71
Library
bcc55
cw80
gcc32
msvc60
msvc71
Machine
SPARC
x86
OS
Cygwin
Unix
Windows
System
NonWindows
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
Jamfile
Jamrules
borland
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005, Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//
// Distributed under the so-called MIT license, see accompanying file license.txt.
//*****************************************************************************

#ifndef _RCF_PROTOCOL_PROTOCOL_HPP
#define _RCF_PROTOCOL_PROTOCOL_HPP

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

namespace RCF {

    extern const int DefaultProtocol;

    template<int N>
    class Protocol
    {
    public:

        static std::string getName()
        {
            return "";
        }

        class In
        {
        public:
            void bind(std::istream &is) 
            {}

            void unbind()
            {}
            
            template<typename T>
            In &operator>>(T &t)
            {
                RCF_THROW(RCF::SerializationException, "unsupported serialization protocol")(N);
                return *this;
            }
        };

        class Out
        {
        public:
            void bind(std::ostream &os) 
            {}

            void unbind()
            {}

            template<typename T>
            Out &operator<<(const T &t)
            {
                RCF_THROW(RCF::SerializationException, "unsupported serialization protocol")(N);
                return *this;
            }
        };
    };


    template<typename IS, typename OS>
    class ProtocolBase
    {
    public:
        class In
        {
        public:
            void bind(std::istream &is)                            
            {
                ar.reset( new IS(is) );
            }

            void unbind()
            {
                ar.reset();
            }
            
            template<typename T> In &operator>>(T &t)
            { 
                *ar >> t; 
                return *this; 
            }
        
        private:
            std::auto_ptr<IS> ar;
        };

        class Out
        {
        public:
            void bind(std::ostream &os)                            
            {
                ar.reset( new OS(os) );
            }

            void unbind()
            {
                ar.reset();
            }
            
            template<typename T> Out &operator<<(const T &t)
            { 
                *ar << t; 
                return *this; 
            }

        private:
            std::auto_ptr<OS> ar;
        };
    };
    

    // theoretically this one should be faster, since we reuse the memory instead of new'ing it again
    template<typename IS, typename OS>
    class ProtocolBaseWithPlacementNew
    {
    public:
        class In
        {
        public:
            In() : ar() 
            {
                ar = (IS *) malloc(sizeof(IS));
                valid = false;
            }

            ~In()
            {
                if (valid)
                {
                    ar->~IS();
                    valid = false;
                }
                free(ar);
            }
            
            void bind(std::istream &is)                            
            {
                RCF_ASSERT(!valid);
                new (ar) IS(is);
                valid = true;
            }

            void unbind()
            {
                if (valid)
                {
                    ar->~IS();
                    valid = false;
                }
            }
            
            template<typename T> In &operator>>(T &t)
            { 
                *ar >> t; 
                return *this; 
            }
        
        private:
            IS *ar;
            bool valid;
        };

        class Out
        {
        public:
            Out() : ar() 
            {
                ar = (OS *) malloc(sizeof(OS));
                valid = false;
            }

            ~Out()
            {
                if (valid)
                {
                    ar->~OS();
                    valid = false;
                }
                free(ar);
            }
            
            void bind(std::ostream &os)                            
            {
                RCF_ASSERT(!valid);
                new (ar) OS(os);
                valid = true;
            }

            void unbind()
            {
                if (valid)
                {
                    ar->~OS();
                    valid = false;
                }
            }
            
            template<typename T> Out &operator<<(const T &t)
            { 
                *ar << t; 
                return *this; 
            }

        private:
            OS *ar;
            bool valid;
        };
    };


} // namespace RCF

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161205.3 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid