Simple generic IPC

I will show the simple IPC example in this post using the generic implementation. The IPC consist of two parts:

  1. Server(s) sharing the data;
  2. Client(s) reading the data.

Use this at your own risk. If you don’t like it – don’t use it. Mainly for demonstration purpose. No guarantees.

Server

Imagine we want to share the certain data with other applications. Let’s declare the record type for that like following:

type
  TIPCSharedData = packed record
    Int: Integer;
    Float: Double;
    Text: ShortString;
  end;

To create the server and share the data we need to write the following code:

uses
  uFunctions.IPC;
const
 SERVER_ID: '{23559359-c028-41cb-a33d-450097a602d8}';
var
 LServer: IIPCAsyncDataServer<TIPCSharedData>;
 LData: TIPCSharedData;
begin
 LData.Int := 10;
 LData.Float := 0.5;
 LData.Text := 'Hello!';
 LServer := TIPCAsyncDataServer<TIPCSharedData>.Create(SERVER_ID);
 LServer.Data := LData;
 ...
end;

If you create multiple servers with the same ID then clients will read the data of the server which wrote the data last. Also take a note that you can only use the non-managed types for data interchange (so no String, Interface, dynamic array etc).

Client

Now in other application to access the shared data we need to use the following construction:

uses
  uFunctions.IPC;
const
 SERVER_ID: '{23559359-c028-41cb-a33d-450097a602d8}';
var
 LClient: IIPCAsyncDataClient<TIPCSharedData>;
 LData: TIPCSharedData;
begin
 LClient := TIPCAsyncDataClient<TIPCSharedData>.Create(SERVER_ID);
 if LClient.ReadData(LData) then
   Writeln(LData.Text);
 ...
end;

Enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s