Thursday, March 31, 2022

Simple Managed Inter Process Communication(IPC) Framework



If the need is just to do a cross process communication within the box, it'd be bit heavy to use frameworks such as WCF as it comes with a learning curve and complex setup. However  home grown solutions such as messaged based implementation suffer from flexibility and heavy maintenance.
 The simpleIPC framework tries to strike right balance with the interface based programming coupled with zero setup.

IPC Overview
A lightweight Inter Process Communication (IPC) across process within the same PC in .Net can be implemented using named event object or Window kernel object and shared memory.
  • Interface based programming
  • Duplex communication support
  • Servers are identified by a unique string to which clients can generate a proxy to communicate. Some of the features:
  • Servers/ Clients generate Stubs/ Proxy based on an interface for communication using reflection.
  • As the proxy is based on the RealProxy object, intellisense is also supported in the Visual Studio IDE editor. Easy to debug, just a single file of code.
  • To access a managed server from an unmanaged client, reversePinvoke can be used.
As shown in  the diagram above. IPC consists of a Client, Proxy, Stub and Server as described below

  • Client uses proxy based on RealProxy to make API call
  • Proxy serializes the input data using binary formatter to shared memory and signals stub
  • Stub deserializes the input data using binary formatter from shared memory and makes the call on the server using reflection
  • Stub serializes the results to shared memory and signals Proxy
  • Proxy deserializes the results from shared memory and returns it to the client
Implementation
Comes in two flavors windows kernel object based or named event kernel object based.
Windows based uses a window kernel object for implementing the server


As shown above multiple Windows are hosted in a thread. Each thread hosts a server. 
  • A Window based container can supports multiple servers per thread
  • A window based server cannot work across window stations
Named object based uses a named event object for implementing the server.
  • A Named event object based container supports only one server
  • A named event object based server can work across window stations
Example
The interface  ICallInterface shown below is implemented by the server and ICallbackInterface is implemented by the client.
namespace Example
{
    [Serializable]
    public class regdata
    {
        public string name;
        public string regid;
    }

    public interface ICallInterface
    {
        string current { get; }
        string register(string name, string cbservername, out int ticket);
    }

    public interface ICallbackInterface
    {
        void update(int ticket, regdata data);
    }

}


Windows Simple IPC  Demo

Server Process
Simple IPC Server name for ICallback : winserver


Client Process
Simple IPC server name for ICallbackInterface : winclient




Named Object Demo

Server Process
Simple IPC Server name for ICallback : namederver


Client Process
Simple IPC server name for ICallbackInterface :  namedclient


Operation

Server process starts
client process starts

Client:
creates proxy for server
calls register() on server using proxy

Server:
executes method register() 
server returns success with Ticket #100

Client:
results are received from proxy : success, Ticket:100

Server:
creates proxy for client implementing callback interface
calls update() method on the proxy
Client:
executes update() method

Source and Binaries can be found here.


No comments:

Post a Comment