Channels 110a: IChannelFactory

Sorry for the radio silence, I was on holiday in Alaska. Now I’m back with extra vigor to continue our evolving tutorial. 🙂

NOTE: Source code for all of the files and functionality referenced in the next few posts is available here.

At the root of a custom transport implementation is the creation of your channels. The channel layer uses a factory pattern for constructing channels. We are implementing the Datagram message exchange pattern. So our IChannelFactory (client/proxy) will support creating IOutputChannels and our IListenerFactory (service) will support listening for IInputChannels.

Only the factory classes need to be public, not the IChannel and IListener implementations that they generate. Indigo provides some base class helpers for this process.

  • CommunicationObject is a base class that implements ICommunicationObject and enforces the common state machine.
  • ChannelManagerBase(:CommunicationObject) is a base class that implements IChannelManager and takes care of managing a collection of channels that are created (IChannelFactory) or accepted (IListenerFactory). ChannelManagerBase works in conjunction with ChannelBase, which is a base class that implements IChannel.
  • ChannelFactoryBase(:ChannelManagerBase) is a base class that implements IChannelFactory and consolidates the myriad CreateChannel() overloads into a single OnCreateChannel() abstract method. It also handles the concept of an InnerChannelFactory.
  • ListenerFactoryBase(:ChannelManagerBase) is a base class that implements IListenerFactory. It takes care of managing a collection of listeners that are created, and works in conjunction with ListenerBase, which is a base class that implements IListener. It also handles the concept of an InnerListenerFactory.

In our sample code, the Factory implementations are contained in UdpChannelFactory.cs and UdpListenerFactory.cs. The IChannel implementations are in UdpOutputChannel.cs and UdpInputChannel.cs. The IListener implementation is in UdpInputListener.cs. There are some implementation details of the factories (such as IBufferManager and MessageEncoderFactory) deserving of their own future topics. For the moment I will simply note that these utilities are very helpful in managing serialized message buffers and performing conversions between Message and serialized byte arrays.

UdpChannelFactory 

UdpChannelFactory derives from ChannelFactoryBase. It overrides the following properties and methods to accomplish the bulk of its work:

public override MessageVersion MessageVersion { get; }
For our implementation we hardcode the Message Version to SOAP 1.2+WS-Addressing 1.0.

public override string Scheme { get; }
For Udp we’ve chosen "soap.udp" as our addressing scheme.

public override bool CanCreateChannel<TChannel>()
This is where we declare that we only support IOutputChannel.

protected override TChannel OnCreateChannel<TChannel>(EndpointAddress remoteAddress)
This is where we create our channel implementation (UdpOutputChannel).

We also override OnOpen() and OnClosed() so that we can setup and tear-down our IBufferManager at the appropriate points in the state machine.

UdpOutputChannel 

UdpOutputChannel is our internal implementation of IOutputChannel. In the constructor we validate our arguments and, based on the EndpointAddress passed in, construct a System.Net.EndPoint for our destination .
In our OnOpen() override we create the Socket that we will use for sending data to this EndPoint:

this.socket = new Socket(this.remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

We have 2 places where we need to clean up the socket, as the Channel can close gracefully using Close(), or ungracefully using Abort(). In the Abort() case we want to ensure that our base method gets called, which is why it’s in a finally block:

try
{
this.socket.Close(0);

}
finally
{

base.OnAbort();

}

For Close(), we simply cleanup the socket and call our base class (if this throws, the infrastructure will call Abort() to ensure our channel is cleaned up):

this.socket.Close();
base.OnClose();

We then implement Send() and BeginSend()/EndSend(). This breaks down into two main sections. First we serialize the message into a byte array:

ArraySegment<byte> messageBuffer = EncodeMessage(message);

Then we send the resulting data on the wire:

this.socket.SendTo(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count, SocketFlags.None, this.remoteEndPoint);

At this point our Send() call has finished its transmission task and we have sent a UDP datagram containing a text encoding serialization of a SOAP message. Next I will cover the IListenerFactory implementation to receive this message.

3 thoughts on “Channels 110a: IChannelFactory

  1. Pingback: kennyw.com » Blog Archive » Channels 103: Addressing the World

  2. Pingback: kennyw.com » Blog Archive » Channels 110a: IChannelFactory

  3. Pingback: kennyw.com » Blog Archive » Channels 110b: IListenerFactory

Leave a Reply

Your email address will not be published. Required fields are marked *