Click here to Skip to main content
15,891,846 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_TIMEDBSDSOCKETS_HPP
#define INCLUDE_RCF_TIMEDBSDSOCKETS_HPP

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


namespace RCF {

    //******************************************************
    // nonblocking socket routines

    int timedConnect(unsigned int timeoutMs, int fd, sockaddr *addr, int addrLen);
    /*
    {
        unsigned int startTimeMs = getCurrentTimeMs();
        unsigned int endTimeMs = startTimeMs + timeoutMs;
        int ret = Platform::OS::BsdSockets::connect(fd, addr, addrLen);
        if ((ret == -1 && Platform::OS::BsdSockets::GetLastError() == Platform::OS::BsdSockets::ERR_EWOULDBLOCK) ||
            (ret == -1 && Platform::OS::BsdSockets::GetLastError() == Platform::OS::BsdSockets::ERR_EINPROGRESS))
        {
            fd_set fdSet;
            FD_ZERO(&fdSet);
            FD_SET(fd, &fdSet);
            unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
            timeval timeout;
            timeout.tv_sec = timeoutMs/1000;
            timeout.tv_usec = 1000*(timeoutMs%1000);
            if (timeout.tv_usec < 0)
            {
                timeout.tv_usec = 0;
            }
            ret = Platform::OS::BsdSockets::select(fd+1, NULL, &fdSet, NULL, &timeout);
            if (ret == 1)
            {
                return 0;
            }
            else if (ret == 0)
            {
                RCF_TRACE("timedConnect - timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
                return -1;
            }
            else
            {
                int err = Platform::OS::BsdSockets::GetLastError();
                RCF_TRACE("timedConnect - error")(fd)(timeout.tv_sec)(timeout.tv_usec)(err)(Platform::OS::GetErrorString(err));
                return -1;
            }
        }
        else if (ret == 0)
        {
            return 0;
        }
        else
        {
            int err = Platform::OS::BsdSockets::GetLastError();
            RCF_TRACE("timedConnect - error")(fd)(err)(Platform::OS::GetErrorString(err));
            return -1;
        }

    }*/

    // returns -2 for timeout, -1 for error, otherwise number of bytes sent (> 0)
    int timedSend(unsigned int timeoutMs, int fd, const char *buffer, std::size_t length, int flags);
    /*
    {
        unsigned int startTimeMs = getCurrentTimeMs();
        unsigned int endTimeMs = startTimeMs + timeoutMs;
        int ret = 0;
        int err = 0;
        while (true)
        {
            ret = Platform::OS::BsdSockets::send(fd, buffer, static_cast<int>(length), flags);
            err = Platform::OS::BsdSockets::GetLastError();

            RCF_ASSERT(ret != 0);
            if (ret > 0)
            {
                return ret;
            }
            else if (
                ret == -1 && 
                err == Platform::OS::BsdSockets::ERR_EWOULDBLOCK)
            {
                unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
                fd_set fdSet;
                FD_ZERO(&fdSet);
                FD_SET(fd, &fdSet);
                timeval timeout;
                timeout.tv_sec = timeoutMs/1000;
                timeout.tv_usec = 1000*(timeoutMs%1000);
                ret = Platform::OS::BsdSockets::select(fd+1, NULL, &fdSet, NULL, &timeout);
                RCF_ASSERT(ret == -1 || ret == 0 || ret == 1)(ret);
                if (ret == 1)
                {
                    continue;
                }
                else if (ret == 0)
                {
                    RCF_TRACE("timedSend - select() timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
                    return -2;
                }
                else if (ret == -1)
                {
                    err = Platform::OS::BsdSockets::GetLastError();
                    RCF_TRACE("timedSend - select() error")(fd)(timeout.tv_sec)(timeout.tv_usec)(ret)(err)(Platform::OS::GetErrorString(err));
                    return -1;
                }
            }
            else
            {
                RCF_ASSERT(ret == -1);
                RCF_TRACE("timedSend - send() error")(fd)(ret)(err)(Platform::OS::GetErrorString(err));
                return -1;
            }
        }
    }*/

    // returns -2 for timeout, -1 for error, 0 for peer closure, otherwise size of packet read
    int timedRecv(unsigned int timeoutMs, int fd, char *buffer, std::size_t bufferLen, int flags);
    /*
    {
        unsigned int startTimeMs = getCurrentTimeMs();
        unsigned int endTimeMs = startTimeMs + timeoutMs;
        int ret = 0;
        int err = 0;

        while (true)
        {
            ret = Platform::OS::BsdSockets::recv(fd, buffer, static_cast<int>(bufferLen), flags);
            err = Platform::OS::BsdSockets::GetLastError();
            if (ret >= 0)
            {
                return ret;
            }
            else if (
                ret == -1 && 
                err == Platform::OS::BsdSockets::ERR_EWOULDBLOCK)
            {
                unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
                fd_set fdSet;
                FD_ZERO(&fdSet);
                FD_SET(fd, &fdSet);
                timeval timeout;
                timeout.tv_sec = timeoutMs/1000;
                timeout.tv_usec = 1000*(timeoutMs%1000);
                ret = Platform::OS::BsdSockets::select(fd+1, &fdSet, NULL, NULL, &timeout);
                RCF_ASSERT(ret == -1 || ret == 0 || ret == 1)(ret);
                if (ret == 1)
                {
                    continue;
                }
                else if (ret == 0)
                {
                    RCF_TRACE("timedRecv - select() timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
                    return -2;
                }
                else if (ret == -1)
                {
                    err = Platform::OS::BsdSockets::GetLastError();
                    RCF_TRACE("timedRecv - select() error")(fd)(timeout.tv_sec)(timeout.tv_usec)(ret)(err)(Platform::OS::GetErrorString(err));
                    return -1;
                }
            }
            else if (ret == -1)
            {
                err = Platform::OS::BsdSockets::GetLastError();
                RCF_TRACE("timedRecv - recv() error")(fd)(ret)(err)(Platform::OS::GetErrorString(err));
                return -1;
            }
        }
    }*/

} // namespace RCF


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