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
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 .
//******************************************************************************

#include <RCF/UdpClientTransport.hpp>
#include <RCF/UdpEndpoint.hpp>

#include <boost/static_assert.hpp>

#include <RCF/Tools.hpp>

namespace RCF {

    UdpClientTransport::UdpClientTransport(const std::string &ip, int port) :
        mIp(ip),
        mPort(port),
        mSock(-1),
        mSrcAddr(),
        mDestAddr()
    {
        memset(&mSrcAddr, 0, sizeof(mSrcAddr));
        memset(&mDestAddr, 0, sizeof(mDestAddr));
    }

    UdpClientTransport::UdpClientTransport(const sockaddr &dest) :
        mIp(),
        mPort(),
        mSock(-1),
        mSrcAddr(),
        mDestAddr()
    {
        memset(&mSrcAddr, 0, sizeof(mSrcAddr));
        memset(&mDestAddr, 0, sizeof(mDestAddr));

        BOOST_STATIC_ASSERT(sizeof(sockaddr) == sizeof(sockaddr_in));
        memcpy(&mDestAddr, &dest, sizeof(dest));
    }

    UdpClientTransport::UdpClientTransport(const UdpClientTransport &rhs) :
        I_ClientTransport(rhs),
        mIp(rhs.mIp),
        mPort(rhs.mPort),
        mSock(-1),
        mSrcAddr(),
        mDestAddr()
    {
        memset(&mSrcAddr, 0, sizeof(mSrcAddr));
        memset(&mDestAddr, 0, sizeof(mDestAddr));
    }

    UdpClientTransport::~UdpClientTransport()
    {
        RCF_DTOR_BEGIN
            close();
        RCF_DTOR_END
    }

    ClientTransportAutoPtr UdpClientTransport::clone() const
    {
        return ClientTransportAutoPtr( new UdpClientTransport(*this));
    }

    EndpointPtr UdpClientTransport::getEndpointPtr() const
    {
        return EndpointPtr( new UdpEndpoint(mIp, mPort) );
    }

    void UdpClientTransport::connect(unsigned int timeoutMs)
    {
        RCF_TRACE("")(mSock)(mIp)(mPort);
        RCF_UNUSED_VARIABLE(timeoutMs);

        // TODO: replace throw with return value
        if (mSock == -1)
        {
            int ret = 0;
            int err = 0;

            mSock = static_cast<int>(socket(AF_INET,SOCK_DGRAM,0));
            err = Platform::OS::BsdSockets::GetLastError();
            RCF_VERIFY(
                mSock != -1,
                Exception(
                    RcfError_Socket, err, RcfSubsystem_Os,
                    "socket() failed"));

            // local address
            memset(&mSrcAddr, 0, sizeof(mSrcAddr));
            mSrcAddr.sin_family = AF_INET;
            mSrcAddr.sin_port = htons(0);
            mSrcAddr.sin_addr.s_addr = INADDR_ANY;

            // remote address
            if (mIp != "")
            {
                RCF_ASSERT(mIp != "");
                RCF_ASSERT(mPort > 0);

                unsigned long ul_addr = ::inet_addr(mIp.c_str());
                if (ul_addr == INADDR_NONE)
                {
                    hostent *hostDesc = ::gethostbyname( mIp.c_str() );
                    if (hostDesc)
                    {
                        char *szIp = ::inet_ntoa(
                            * (in_addr*) hostDesc->h_addr_list[0]);

                        ul_addr = ::inet_addr(szIp);
                    }
                }

                memset(&mDestAddr,0,sizeof(mDestAddr));
                mDestAddr.sin_family = AF_INET;
                mDestAddr.sin_port = htons( static_cast<u_short>(mPort) );
                mDestAddr.sin_addr.s_addr = ul_addr;
            }

            ret = bind(mSock, (sockaddr *) &mSrcAddr, sizeof(mSrcAddr));
            err = Platform::OS::BsdSockets::GetLastError();
            RCF_VERIFY(
                ret == 0,
                Exception(
                    RcfError_Socket, err, RcfSubsystem_Os,
                    "bind() failed"));
        }

    }

    int UdpClientTransport::send(
        const std::vector<ByteBuffer> &data,
        unsigned int timeoutMs)
    {
        RCF_TRACE("")(mSock)(mIp)(mPort);
        RCF_UNUSED_VARIABLE(timeoutMs);

        connect(timeoutMs);
        // TODO: optimize for case of single byte buffer with left margin

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

        std::vector<char> &buffer = *mWriteVecPtr;
        buffer.resize(lengthByteBuffers(data)+4);
       
        *(unsigned int*)(&buffer[0]) =
            static_cast<unsigned int>(lengthByteBuffers(data)); // TODO: byte ordering

        copyByteBuffers(data, &buffer[4]);

        int len = sendto(
            mSock,
            &buffer[0],
            static_cast<int>(buffer.size()),
            0,
            (struct sockaddr *) &mDestAddr, sizeof(mDestAddr));

        int err = Platform::OS::BsdSockets::GetLastError();
        RCF_VERIFY(
            len > 0,
            Exception(
                RcfError_Socket,
                err,
                RcfSubsystem_Os,
                "sendto() failed"));
        return 1;
    }

    int UdpClientTransport::receive(
        ByteBuffer &byteBuffer,
        unsigned int timeoutMs)
    {
        // try to receive a UDP message from server, within the current timeout
        RCF_TRACE("")(mSock)(mIp)(mPort);

        unsigned int startTimeMs = getCurrentTimeMs();
        unsigned int endTimeMs = startTimeMs + timeoutMs;

        while (true)
        {
            unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
            fd_set fdSet;
            FD_ZERO(&fdSet);
            FD_SET( static_cast<SOCKET>(mSock), &fdSet);
            timeval timeout;
            timeout.tv_sec = timeoutMs/1000;
            timeout.tv_usec = 1000*(timeoutMs%1000);

            int ret = Platform::OS::BsdSockets::select(
                mSock+1,
                &fdSet,
                NULL,
                NULL,
                &timeout);

            int err = Platform::OS::BsdSockets::GetLastError();

            RCF_ASSERT(-1 <= ret && ret <= 1)(ret);
            if (ret == -1)
            {
                RCF_THROW(
                    Exception(
                        RcfError_Socket,
                        err,
                        RcfSubsystem_Os,
                        "udp client select() failed"));
            }
            else if (ret == 0)
            {
                RCF_THROW(Exception(
                    RcfError_ClientReadTimeout));
            }
            RCF_ASSERT(ret == 1)(ret);

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

            // TODO: optimize
            std::vector<char> &buffer = *mReadVecPtr;
            buffer.resize(4);
            sockaddr_in fromAddr;
            memset(&fromAddr, 0, sizeof(fromAddr));
            int fromAddrLen = sizeof(fromAddr);

            int len = Platform::OS::BsdSockets::recvfrom(
                mSock,
                &buffer[0],
                4,
                MSG_PEEK,
                (sockaddr *) &fromAddr,
                &fromAddrLen);

            err = Platform::OS::BsdSockets::GetLastError();
            if (len == 4 ||
                (len == -1 && err == Platform::OS::BsdSockets::ERR_EMSGSIZE))
            {
                if (fromAddr.sin_addr.s_addr == mDestAddr.sin_addr.s_addr &&
                    fromAddr.sin_port == mDestAddr.sin_port)
                {
                    // TODO: byte ordering
                    unsigned int dataLength = *(unsigned int *)(&buffer[0]);
                    RCF_VERIFY(
                        dataLength <= getMaxMessageLength(),
                        Exception(RcfError_ClientMessageLength))
                        (dataLength)(getMaxMessageLength());
                   
                    RCF_ASSERT(
                        dataLength <= getMaxMessageLength())
                        (dataLength)(getMaxMessageLength());

                    buffer.resize(4+dataLength);
                    memset(&fromAddr, 0, sizeof(fromAddr));
                    fromAddrLen = sizeof(fromAddr);

                    len = Platform::OS::BsdSockets::recvfrom(
                        mSock,
                        &buffer[0],
                        dataLength+4,
                        0,
                        (sockaddr *) &fromAddr,
                        &fromAddrLen);

                    if (len == static_cast<int>(dataLength+4))
                    {
                        byteBuffer = ByteBuffer(
                            &buffer[4],
                            dataLength,
                            4,
                            mReadVecPtr);

                        return 1;
                    }
                }
            }
        }
    }

    void UdpClientTransport::close()
    {
        if (mSock != -1)
        {
            int err = Platform::OS::BsdSockets::closesocket(mSock);
            if (err < 0)
            {
                RCF_ASSERT(0)(mSock)(err);
            }
            mSock = -1;
        }
    }

    bool UdpClientTransport::isConnected()
    {
        return true;
    }

    void UdpClientTransport::disconnect(unsigned int timeoutMs)
    {
        RCF_UNUSED_VARIABLE(timeoutMs);
    }

    void UdpClientTransport::setTransportFilters(
        const std::vector<FilterPtr> &filters)
    {
        if (!filters.empty())
        {
            RCF_ASSERT(0);
        }
    }

    void UdpClientTransport::getTransportFilters(
        std::vector<FilterPtr> &filters)
    {
        RCF_UNUSED_VARIABLE(filters);
    }

} // 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)

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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid