Click here to Skip to main content
12,454,965 members (57,142 online)
Click here to Skip to main content

Stats

1.7M views
7.5K downloads
325 bookmarked
Posted

RCF - Interprocess Communication for C++

, 25 Oct 2011 CPOL
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
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-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 .
//******************************************************************************

#include <RCF/UdpServerTransport.hpp>

#include <RCF/MethodInvocation.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/ThreadLocalData.hpp>
#include <RCF/Tools.hpp>

namespace RCF {

    UdpServerTransport::UdpServerTransport(int port) :
        mpSessionManager(RCF_DEFAULT_INIT),
        mPort(port),
        mFd(-1),
        mPollingDelayMs(RCF_DEFAULT_INIT),
        mStopFlag(RCF_DEFAULT_INIT)
    {
    }

    ServerTransportPtr UdpServerTransport::clone()
    {
        return ServerTransportPtr( new UdpServerTransport(mPort) );
    }

    void UdpServerTransport::setSessionManager(I_SessionManager &sessionManager)
    {
        mpSessionManager = &sessionManager;
    }

    I_SessionManager &UdpServerTransport::getSessionManager()
    {
        RCF_ASSERT(mpSessionManager);
        return *mpSessionManager;
    }

    void UdpServerTransport::setPort(int port)
    {
        this->mPort = port;
    }

    int UdpServerTransport::getPort() const
    {
        return mPort;
    }

    void UdpServerTransport::open()
    {
        RCF_TRACE("")(mPort)(getNetworkInterface());

        // create and bind a socket for receiving UDP messages
        if (mFd == -1 && mPort > 0)
        {
            int ret = 0;
            int err = 0;

            // create the socket
            mFd = static_cast<int>(socket(AF_INET, SOCK_DGRAM, 0));
            if (mFd == -1)
            {
                err = Platform::OS::BsdSockets::GetLastError();
                RCF_THROW(Exception(
                    RcfError_Socket, err, RcfSubsystem_Os, "socket() failed"))
                    (mFd);
            }

            // setup the address
            std::string networkInterface = getNetworkInterface();
            sockaddr_in serverAddr;
            memset(&serverAddr, 0, sizeof(serverAddr));
            serverAddr.sin_family = AF_INET;
            serverAddr.sin_port = htons( static_cast<u_short>(mPort) );
            if (networkInterface.size() > 0)
            {
                if (isdigit(networkInterface.at(0)))
                {
                    serverAddr.sin_addr.s_addr = inet_addr(
                        networkInterface.c_str());
                }
                else
                {
                    hostent *h = gethostbyname(networkInterface.c_str());
                    if (h)
                    {
                        serverAddr.sin_addr = * (in_addr *) h->h_addr_list[0];
                    }
                }
            }
            else
            {
                serverAddr.sin_addr.s_addr = INADDR_ANY;
            }

            // bind the socket
            ret = bind(mFd, (struct sockaddr*) &serverAddr, sizeof(serverAddr));
            if (ret < 0)
            {
                err = Platform::OS::BsdSockets::GetLastError();
                RCF_THROW(Exception(
                    RcfError_Socket, err, RcfSubsystem_Os, "bind() failed"))
                    (mFd)(mPort)(networkInterface)(ret);
            }
            RCF_ASSERT( mFd != -1 )(mFd);

            // set the socket to nonblocking mode
            Platform::OS::BsdSockets::setblocking(mFd, false);
        }
    }

    void UdpServerTransport::close()
    {
        if (mFd != -1)
        {
            int ret = Platform::OS::BsdSockets::closesocket(mFd);
            int err = Platform::OS::BsdSockets::GetLastError();
            RCF_VERIFY(
                ret == 0,
                Exception(
                    RcfError_SocketClose, err, RcfSubsystem_Os,
                    "closesocket() failed"))(mFd);
            mFd = -1;
        }
    }

    void discardPacket(int fd)
    {
        char buffer[1];
        int len = recvfrom(fd, buffer, 1, 0, NULL, NULL);
        int err = Platform::OS::BsdSockets::GetLastError();
        RCF_VERIFY(
            len == 1 ||
            (len == -1 && err == Platform::OS::BsdSockets::ERR_EMSGSIZE) ||
            (len == -1 && err == Platform::OS::BsdSockets::ERR_ECONNRESET),
            Exception(
                RcfError_Socket,
                err,
                RcfSubsystem_Os,
                "recvfrom() failed"));
    }

    void UdpServerTransport::cycle(
        int timeoutMs,
        const volatile bool &stopFlag)
    {
        // poll the UDP socket for messages, and read a message if one is available

        RCF_UNUSED_VARIABLE(stopFlag);
        fd_set fdSet;
        FD_ZERO(&fdSet);
        FD_SET( static_cast<SOCKET>(mFd), &fdSet);
        timeval timeout;
        timeout.tv_sec = timeoutMs/1000;
        timeout.tv_usec = 1000*(timeoutMs%1000);

        int ret = Platform::OS::BsdSockets::select(
            mFd+1,
            &fdSet,
            NULL,
            NULL,
            timeoutMs < 0 ? NULL : &timeout);

        int err = Platform::OS::BsdSockets::GetLastError();
        if (ret == 1)
        {
            SessionPtr sessionPtr = getCurrentUdpSessionPtr();
            if (sessionPtr.get() == NULL)
            {
                SessionStatePtr sessionStatePtr(new SessionState());
                ProactorPtr proactorPtr( new UdpProactor(*this, sessionStatePtr));
                sessionPtr = getSessionManager().createSession();
                sessionPtr->setProactorPtr(proactorPtr);
                setCurrentUdpSessionPtr(sessionPtr);
            }


            SessionStatePtr sessionStatePtr =
                boost::static_pointer_cast<UdpProactor>(
                    sessionPtr->getProactorPtr())->mSessionStatePtr;
            {
                // read a message

                boost::shared_ptr<std::vector<char> > &readVecPtr =
                    sessionStatePtr->mReadVecPtr;

                if (readVecPtr.get() == NULL || !readVecPtr.unique())
                {
                    readVecPtr.reset( new std::vector<char>());
                }
                std::vector<char> &buffer = *readVecPtr;

                sockaddr from;
                int fromlen = sizeof(from);
                memset(&from, 0, sizeof(from));
                buffer.resize(4);

                int len = Platform::OS::BsdSockets::recvfrom(
                    mFd,
                    &buffer[0],
                    4,
                    MSG_PEEK,
                    &from,
                    &fromlen);

                err = Platform::OS::BsdSockets::GetLastError();
                if (isClientAddrAllowed( *(sockaddr_in *) &from ) &&
                    (len == 4 || (len == -1 && err == Platform::OS::BsdSockets::ERR_EMSGSIZE)))
                {
                    sockaddr_in *remoteAddr = reinterpret_cast<sockaddr_in*>(&from);
                    sessionStatePtr->remoteAddress = IpAddress(*remoteAddr);
                    // TODO: byte ordering
                    unsigned int dataLength = *(unsigned int *)(&buffer[0]);
                    if (dataLength <= static_cast<int>(getMaxMessageLength()))
                    {
                        buffer.resize(4+dataLength);
                        memset(&from, 0, sizeof(from));
                        fromlen = sizeof(from);

                        len = Platform::OS::BsdSockets::recvfrom(
                            mFd,
                            &buffer[0],
                            4+dataLength,
                            0,
                            &from,
                            &fromlen);

                        if (static_cast<unsigned int>(len) == 4+dataLength)
                        {
                            getSessionManager().onReadCompleted(sessionPtr);
                        }
                    }
                    else
                    {
                        boost::shared_ptr<std::vector<char> > vecPtr(
                            new std::vector<char>(4+1+1+4));

                        std::size_t pos = 4;
                        RCF::encodeInt(Descriptor_Error, *vecPtr, pos);
                        RCF::encodeInt(0, *vecPtr, pos);
                        RCF::encodeInt(RcfError_ServerMessageLength, *vecPtr, pos);

                        *(boost::uint32_t *)(&vecPtr->front()) =
                            static_cast<boost::uint32_t>(pos-4);

                        char *buffer = &vecPtr->front();
                        std::size_t bufferLen = pos;

                        const sockaddr_in &remoteAddr =
                            sessionStatePtr->remoteAddress.getSockAddr();

                        int len = sendto(
                            mFd,
                            buffer,
                            static_cast<int>(bufferLen),
                            0,
                            (const sockaddr *) &remoteAddr,
                            sizeof(remoteAddr));

                        RCF_UNUSED_VARIABLE(len);
                        discardPacket(mFd);
                    }
                }
                else
                {
                    // discard the message (sender ip not allowed, or message format bad)
                    discardPacket(mFd);
                }
            }
        }
        else if (ret == 0)
        {
            RCF_TRACE("server udp poll - no messages")(mFd)(mPort);
        }
        else if (ret == -1)
        {
            RCF_THROW(
                Exception(
                    RcfError_Socket,
                    err,
                    RcfSubsystem_Os,
                    "udp server select() failed "))
                (mFd)(mPort)(err);
        }

    }

    void UdpServerTransport::postWrite(
        const SessionStatePtr &sessionStatePtr,
        const std::vector<ByteBuffer> &byteBuffers)
    {
        // prepend data length and send the data

        boost::shared_ptr<std::vector<char> > &writeVecPtr =
            sessionStatePtr->mWriteVecPtr;

        if (writeVecPtr.get() == NULL || !writeVecPtr.unique())
        {
            writeVecPtr.reset( new std::vector<char>());
        }

        std::vector<char> &writeBuffer = *writeVecPtr;
        unsigned int dataLength = static_cast<unsigned int>(lengthByteBuffers(byteBuffers));
        writeBuffer.resize(4+dataLength);
        *(int *)(&writeBuffer[0]) = dataLength; // TODO: byte ordering
        copyByteBuffers(byteBuffers, &writeBuffer[4]);

        const sockaddr_in &remoteAddr =
            sessionStatePtr->remoteAddress.getSockAddr();
       
        int len = sendto(
            mFd,
            &writeBuffer[0],
            static_cast<int>(writeBuffer.size()),
            0,
            (const sockaddr *) &remoteAddr,
            sizeof(remoteAddr));

        if (len != static_cast<int>(writeBuffer.size()))
        {
            int err = Platform::OS::BsdSockets::GetLastError();
            RCF_THROW(Exception(
                RcfError_Socket, err, RcfSubsystem_Os, "sendto() failed"))
                (mFd)(len)(writeBuffer.size());
        }
    }

    void UdpServerTransport::postRead(const SessionStatePtr &sessionStatePtr)
    {
        RCF_UNUSED_VARIABLE(sessionStatePtr);
    }

    bool UdpServerTransport::cycleTransportAndServer(
        RcfServer &server,
        int timeoutMs,
        const volatile bool &stopFlag)
    {
        if (!stopFlag && !mStopFlag)
        {
            cycle(timeoutMs/2, stopFlag);
            server.cycleSessions(timeoutMs/2, stopFlag);
        }
        return stopFlag || mStopFlag;
    }

    void UdpServerTransport::onServiceAdded(RcfServer &server)
    {
        setSessionManager(server);
        WriteLock writeLock( getTaskEntriesMutex() );
        getTaskEntries().clear();

        getTaskEntries().push_back(
            TaskEntry(
                boost::bind(
                    &UdpServerTransport::cycleTransportAndServer,
                    this,
                    boost::ref(server),
                    _1,
                    _2),
                StopFunctor(),
                "RCF udp server"));

        mStopFlag = false;
    }

    void UdpServerTransport::onServiceRemoved(RcfServer &)
    {}

    UdpServerTransport::SessionState::SessionState()
    {}

    void UdpServerTransport::onServerOpen(RcfServer &)
    {
        open();
    }

    void UdpServerTransport::onServerClose(RcfServer &)
    {
        close();
    }

    void UdpServerTransport::onServerStart(RcfServer &)
    {
    }

    void UdpServerTransport::onServerStop(RcfServer &)
    {
        mStopFlag = false;
    }
   
    const I_RemoteAddress &UdpServerTransport::SessionState::getRemoteAddress() const
    {
        return remoteAddress;
    }

    UdpServerTransport::UdpProactor::UdpProactor(
        UdpServerTransport &transport,
        const SessionStatePtr &sessionStatePtr) :
            mTransport(transport),
            mSessionStatePtr(sessionStatePtr)
    {}

    void UdpServerTransport::UdpProactor::postRead()
    {
        mTransport.postRead(mSessionStatePtr);
    }

    void UdpServerTransport::UdpProactor::postWrite(
        const std::vector<ByteBuffer> &byteBuffers)
    {
        mTransport.postWrite(mSessionStatePtr, byteBuffers);
    }

    void UdpServerTransport::UdpProactor::postClose()
    {}

    ByteBuffer UdpServerTransport::UdpProactor::getReadByteBuffer()
    {
        return ByteBuffer(
            &mSessionStatePtr->mReadVecPtr->front() + 4,
            mSessionStatePtr->mReadVecPtr->size() - 4,
            4,
            mSessionStatePtr->mReadVecPtr);
    }

    I_ServerTransport &UdpServerTransport::UdpProactor::getServerTransport()
    {
        return mTransport;
    }

    UdpServerTransport::SessionStatePtr UdpServerTransport::UdpProactor::getSessionStatePtr() const
    {
        return mSessionStatePtr;
    }

    const I_RemoteAddress &UdpServerTransport::UdpProactor::getRemoteAddress()
    {
        return mSessionStatePtr->getRemoteAddress();
    }

    void UdpServerTransport::UdpProactor::setTransportFilters(const std::vector<FilterPtr> &)
    {
        RCF_ASSERT(0);
    }

    const std::vector<FilterPtr> &UdpServerTransport::UdpProactor::getTransportFilters()
    {
        RCF_ASSERT(0);
        static std::vector<FilterPtr> dummy;
        return dummy;
    }

} // namespace RCF

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.160826.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid