Skip to content

Latest commit

 

History

History
79 lines (70 loc) · 4.43 KB

README.md

File metadata and controls

79 lines (70 loc) · 4.43 KB

Description

InterprocessMsg is a C++11 library for bidirectional communication based on messages between two processes on the same computer. The main dependency is boost interprocess. InterprocessMsg is cross-platform compatible and was tested on Windows 10 (Intel processor) and Ubuntu Mate 18.04 (ARM processor).

Prerequisites

This repository consists of the source code (.cpp and .hpp files) of the lib, which may be compiled with any compiler capabale of C++11, and Visual Studio project files. The InterprocessMsg-Linux.vcxproj file may be used for a remote build on a ARM processor (like an Odroid or Raspberry-PI) InterprocessMsg.vcxproj is the project file for a standard Windows built with Visual Studio.

Dependencies Ubuntu

Use

sudo apt-get install libboost-all-dev

to install boost on your ubuntu system, if it was not installed already.

Dependencies Windows

I recommend vcpkg to handle your dependencies on Windows. Just clone vcpkg on your PC and build it. After that run vcpkg from commandline and install boost:

vcpkg install boost-interprocess

Example usage

  1. Instantiate a ClInterproceMsgManager within each process.

  2. Subscribe handler functions to the manager to handle messages with specific topics:

EInterprocError ClInterprocMsgManager::Subscribe(const std::string sAddressName, ClInterprocListener::fnNewMessage fnCallbackOnNewMsg, void * pCBCtx, const  std::string sTopic = "default");

The handler function prototype is defined as

typedef long(*fnNewMessage)(StMessage &, void*);

with StMessage being a simple struct, defining the message structure.

  1. start listening and wait for messages from process 2
EInterprocError ClInterprocMsgManager::Start(const std::string sAddressName = "");
  1. send a message from process 1 to process 2
StMessage stMsg("process 1", "process 2", "default", "hello world!");
nErr = m_oInterProcMsgManager.Send(stMsg);

Example code for process 1

//1.)
#include "ClInterprocMsgManager.hpp"
#include <iostream>
ClInterprocMsgManager m_oInterProcMsgManager("process1"); // this process is process 1
//2.)
m_oInterProcMsgManager.Subscribe("process 2", fndefaultMsgHandler, this); //fndefaultMsgHandler is a function, which will handle incoming messages from process 2 with the topic "default"
m_oInterProcMsgManager.Subscribe("process 2", fnstatusMsgHandler, this, "status"); //fnstatusMsgHandler is a function, which will handle incoming messages from process 2 with the topic "status"
//3.)
EInterprocError nErr =  m_oInterProcMsgManager.Start(); //this will spawn a thread, which will listen for new messages from process 2. If new messages are received, one of the subscribed handlers (2.) will be called
if (nErr == INTERPROC_SUCCESS)
{
	std::cout << "start listening to process 2" << std::endl;
}
else
{
	std::cout << "Error: failed listening to process 2!" << std::endl;
}
//4.
StMessage stMsg("process 1", "process 2", "default", "hello world!");
nErr = m_oInterProcMsgManager.Send(stMsg);

Notes

  • Internally one of the two communicating processes creates shared memory and both processes will access this memory block. This shared memory is structured as a message queue (including a mutex to be thread safe, as both processes obviously run in parallel).
  • The subscribing process will create the shared memory, the sender will only open it.
  • In case both processes listen and receive, two shared memory blocks will be created. Both processes will create one block and open another one.
  • This lib was designed for interprocess communication between two processes, only.
  • Boost itsself provides the class boost::interprocess::message_queue. Unfortunatly it cannot handle a bidirectional communication, only unidirectional. Also undefined behaviour occurs when instantiating two boost::interprocess::message_queue (one in each of the processes). This is the actual reason for the developement of InterprocessMsg to solve this (unexpected and unexplained) shortcoming.