Click here to Skip to main content
15,892,927 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.6M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef INCLUDE_RCF_TCPASIOSYNCHRONIZEDSOCKET_HPP
#define INCLUDE_RCF_TCPASIOSYNCHRONIZEDSOCKET_HPP

#include <boost/asio.hpp>

#include <boost/shared_ptr.hpp>

#include <RCF/ThreadLibrary.hpp>

namespace RCF {
    namespace asio = boost::asio;

    typedef asio::io_service Demuxer;
    typedef boost::shared_ptr<Demuxer> DemuxerPtr;
    typedef boost::shared_ptr<ReadWriteMutex> ReadWriteMutexPtr;
    typedef std::auto_ptr<Mutex> MutexAutoPtr;

    class TcpAsioSynchronizedSocket
    {
    public:
        TcpAsioSynchronizedSocket(DemuxerPtr demuxerPtr, ReadWriteMutexPtr readWriteMutexPtr = ReadWriteMutexPtr());
        
        // async functions
        template<typename Handler>
        void connect(const asio::ip::tcp::endpoint &endpoint, const Handler &handler);

        template<typename Handler>
        void accept(asio::ip::tcp::acceptor &acceptor, const Handler &handler);

        template<typename Handler>
        void read(char *buffer, std::size_t bufferLen, const Handler &handler);

        template<typename Handler>
        void write(const char *buffer, std::size_t bufferLen, const Handler &handler);

        // sync functions
        std::size_t read(char *buffer, std::size_t bufferLen, asio::error &error);
        std::size_t write(const char *buffer, std::size_t bufferLen, asio::error &error);
        void open();
        void close();
        void getRemoteEndpoint(asio::ip::tcp::endpoint &endpoint) const;
        void setSynchronized(bool synchronized);
        void setCloseFlag();
        bool getCloseFlag();
        bool isConnected();
        asio::ip::tcp::socket::native_type impl();

    private:
        template<typename Handler>
        void connectSynchronized(const asio::ip::tcp::endpoint &endpoint, const Handler &handler);

        template<typename Handler>
        void connectUnsynchronized(const asio::ip::tcp::endpoint &endpoint, const Handler &handler);

        template<typename Handler>
        void acceptSynchronized(asio::ip::tcp::acceptor &acceptor, const Handler &handler);

        template<typename Handler>
        void acceptUnsynchronized(asio::ip::tcp::acceptor &acceptor, const Handler &handler);

        template<typename Handler>
        void readSynchronized(char *buffer, std::size_t bufferLen, const Handler &handler);

        template<typename Handler>
        void readUnsynchronized(char *buffer, std::size_t bufferLen, const Handler &handler);

        template<typename Handler>
        void writeSynchronized(const char *buffer, std::size_t bufferLen, const Handler &handler);
        
        template<typename Handler>
        void writeUnsynchronized(const char *buffer, std::size_t bufferLen, const Handler &handler);

        void onError(asio::error &myError, const asio::error &error);
        bool isConnectedSynchronized();
        bool isConnectedUnsynchronized();
        std::size_t writeSynchronized(const char *buffer, std::size_t bufferLen, asio::error &error);
        std::size_t writeUnsynchronized(const char *buffer, std::size_t bufferLen, asio::error &error);
        std::size_t readSynchronized(char *buffer, std::size_t bufferLen, asio::error &error);
        std::size_t readUnsynchronized(char *buffer, std::size_t bufferLen, asio::error &error);
        void openSynchronized();
        void openUnsynchronized();
        void closeSynchronized();
        void closeUnsynchronized();
        void getRemoteEndpointSynchronized(asio::ip::tcp::endpoint &endpoint) const;
        void getRemoteEndpointUnsynchronized(asio::ip::tcp::endpoint &endpoint) const;
        asio::ip::tcp::socket::native_type implSynchronized();
        asio::ip::tcp::socket::native_type implUnsynchronized();

    private:
        ReadWriteMutexPtr           mReadWriteMutexPtr;
        MutexAutoPtr                mMutexAutoPtr;
        //DemuxerPtr                  mDemuxerPtr;
        asio::ip::tcp::socket         mSocket;
        volatile bool               mCloseFlag;
    };

} // namespace RCF

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