XDMessaging 2.0

XDMessaging .Net Library 2.0

Introduction

The XDMessaging library provides an easy-to-use, zero configuration solution to same-box communications. It provides a simple API for broadcasting and receiving messages across application domain, process, and network boundaries.

The library allows the use of user-defined pseudo channels through which messages may be sent and received. Any application can send a message to any channel, but it must register as a listener with the channel in order to receive. In this way developers can quickly and programmatically devise how their applications will communicate with each other best to work in harmony. The messages may optionally be propagated to other processes on the local network automatically.

Advantages

The XDMessaging library offers some advantages over other IPC technologies like WCF, .Net Remoting, Sockets, NamedPipes and MailSlots. To begin with the library does not require a server-client relationship as there is no physical connection between processes.

With XDMessaging messages can be broadcast by multiple applications and instantly received by multiple listeners in a disconnected fashion. It's also worth noting that most of the existing IPC implementations require the opening of specific ports and somewhat painful configuration of settings to make work. With XDMessaging there is no configuration, the API determines where messages are sent, and which messages are received using pseudo channels. The library now also includes a simple MailSlot implementation for propagating messages across the local network.

Using the Library

To use the library, create an instance of a IXDBroadcast and use this to send a message to a named channel. You can then create an instance of IXDListener to receive messages on a particular channel. The channels are arbitrary strings chosen to represent a channel, and are not case sensitive.

Before creating the broadcast and listener instances you must first decide which transport mode you want to use for the library. There are 3 modes as follows and each has advantages over the other.

Transport Modes

  • IOStream: This uses file based IO to broadcast messages via a shared directory. A FileSystemWatcher is used within listener classes to monitor changes and trigger the MessageReceived event containing the broadcast message. This mode can be used in Windows Services, console applications, and Windows Forms based applications. Channels are created as separate directories on the file system for each channel. The temporary directories should be accessible by all processes, and there should be no need for manual configuration.
  • WindowsMessaging: This uses the WM_COPYDATA Windows message to copy data between applications. The broadcast implementation sends the Windows messages directly to a hidden window on the listener instance, which dispatches the MessageReceived event with the copied data. Channels are created by adding/removing Windows properties. This offers the most performant solution for Windows Forms based applications, but does not work for Windows Services, console apps, or other applications without a message pump.
  • MailSlot: This uses a MailSlot to broadcast messages over the local network and interprocess. A limitation of the MailSlot is that only one process may read the message within the scope of a single machine. The library uses a Mutex to synchronize access to the MailSlot across the system, and ensures that when one process terminates another listener can pick up further messages. This mode is used internally to send messages from other transport modes over the local network. Machines must be a member of the same Domain or Workgroup in order to send and receive MailSlot messages.

Note: Messages broadcast using a particular mode can only be read by applications using a listener in the same mode. For example, IOStream listeners cannot read messages broadcast in the WindowsMessaging mode. It is possible to broadcast in several modes at the same time, see documentation for further details.

See the included sample applications for more information on using the library in Windows Forms applications or within a Windows Service.

Examples

Example: Sending a message in a particular mode:

// Create an instance of IXDBroadcast using IOStream mode
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.IOStream);
 
// or create an instance of IXDBroadcast using WindowsMessaging mode
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.WindowsMessaging);
 
// Send shutdown message to a channel named commands
broadcast.SendToChannel("commands", "shutdown");

Example: Enable network propagation:

// Create an instance of IXDBroadcast, and enable network propagation
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.WindowsMessaging, true);

Example: Listening for messages on a particular channel:

// Create our listener instance using IOStream mode
IXDListener listener = XDListener.CreateListener(XDTransportMode.IOStream);
 
// or create our listener using WindowsMessaging mode
IXDListener listener = XDListener.CreateListener(XDTransportMode.WindowsMessaging);
 
// Register channels to listen on
listener.RegisterChannel("events");
listener.RegisterChannel("status");
listener.RegisterChannel("commands");
 
// Stop listening on a specific channel
listener.UnRegisterChannel("status");

Example: Handling the messages:

// Attach an event handler to a listener instance
listener.MessageReceived+=XDMessageHandler(this.listener_MessageReceived);
 
// process the message
private void listener_MessageReceived(object sender, XDMessageEventArgs e)
{
 // e.DataGram.Message is the message
 // e.DataGram.Channel is the channel name
 switch(e.DataGram.Message)
 {
  case "shutdown":
      this.Close();
      break;
 }
}