Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C++

RMI for C++

Rate me:
Please Sign up or sign in to vote.
4.87/5 (113 votes)
6 Aug 2009CPOL8 min read 815.3K   4.6K   153  
User-friendly remote method invocation in C++.
// OS-specific details - Windows

#ifndef _UTIL_PLATFORM_OS_WINDOWS_OS_HPP_
#define _UTIL_PLATFORM_OS_WINDOWS_OS_HPP_

// With Codewarrior we get multiple redefinition problems when including <winsock2.h> and <windows.h>.
// Easiest way aound seems to be a project-wide definition of WIN32_LEAN_AND_MEAN.

#ifdef WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#else
//#define WIN32_LEAN_AND_MEAN
//#include <winsock2.h>
#include <windows.h>
//#undef WIN32_LEAN_AND_MEAN
#endif

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>

#ifndef __WINDOWS__
#define __WINDOWS__
#endif

#if !defined(NDEBUG) && !defined(_DEBUG)
#define _DEBUG
#endif

namespace Platform {

    namespace OS {

        inline void OutputDebugString(const char *sz) 
        { 
            ::OutputDebugString(sz); 

#ifdef UTIL_OUTPUTDEBUGSTRING_TO_STDERR
            fprintf(stderr, "%s", sz);
#endif

#ifdef UTIL_OUTPUTDEBUGSTRING_TO_STDOUT
            fprintf(stdout, "%s", sz);
#endif

#ifdef UTIL_OUTPUTDEBUGSTRING_TO_FILE
            static FILE *file = fopen( "OutputDebugString.txt", "w" );
            fprintf(file, "%s", sz);
#endif

        }

        inline std::string GetErrorString(int err)
        {
            std::string errorString = "Error string lookup failed";
            LPVOID lpMsgBuf;
            if (FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                err/*GetLastError()*/,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                (char *) &lpMsgBuf,
                0,
                NULL ))
            {
                errorString = (const char *) lpMsgBuf;
                LocalFree( lpMsgBuf );
            }

            return errorString;
        }

        inline std::string GetErrorString() { return GetErrorString( ::GetLastError() ); }

        inline int GetCurrentThreadId() { return ::GetCurrentThreadId(); }

        inline void Sleep(unsigned int seconds) { return ::Sleep(1000*seconds); }
        
        namespace Socket {

            typedef ::SOCKET SOCKET;

            inline int recv(int fd, char *buf, int len, int flags)               
            { 
                return ::recv(fd, buf, len, flags); 
            }

            inline int send(int fd, const char *buf, int len, int flags)
            { 
                return ::send(fd, buf, len, flags); 
            }

            inline int accept(int fd, sockaddr *addr, int *addrlen)
            { 
                return (int) ::accept(fd, addr, addrlen); 
            }

            inline int connect(int fd, const sockaddr *name, int namelen)        
            { 
                return ::connect(fd, name, namelen); 
            }

            inline int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout) 
            { 
                return ::select(nfds, readfds, writefds, exceptfds, timeout ); 
            }

            inline int closesocket(int fd)                                       
            { 
                return ::closesocket(fd); 
            }

            inline void setblocking(int fd, bool bBlocking)                      
            { 
                u_long arg = bBlocking ? 0 : 1; 
                ioctlsocket(fd, FIONBIO, &arg); 
            }

            inline int GetLastError()                                               
            { 
                return ::WSAGetLastError(); 
            }

            static const int ERR_EWOULDBLOCK = WSAEWOULDBLOCK;
            static const int ERR_EINPROGRESS = WSAEINPROGRESS;
            static const int ERR_ECONNRESET = WSAECONNRESET;
            static const int ERR_ECONNABORTED = WSAECONNABORTED;
            static const int ERR_ECONNREFUSED = WSAECONNREFUSED;

            
        } //namespace Socket

    } // namespace OS;

} // namespace Platform

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