I have moved!

I've moved my blog
CLICK HERE

Friday, 5 December 2008

Asynchronous Sockets with Yield Return of Lambdas

Update: More in-depth stuff about this

I just watched this video of Jeffrey Richter demonstrating his AsyncEnumerator class:

http://channel9.msdn.com/posts/Charles/Jeffrey-Richter-and-his-AsyncEnumerator

The key idea is the realisation that yield return takes care of turning a single function into a continuation that can be resumed under the control of some other code, but still provides a natural way to write procedurally. This is just what is needed to manage asynchronous communication. Great stuff.

But I had an idea while watching it. His code seems to still place some burden on the caller to know about the details of asynchronous socket programming - how to specify a callback, etc.

Instead, why not hide those details. The trick is to use yield return to pass back lambda expressions containing the work to do asynchronously.

Here's a quick sample - look at this first, to see how easy it is to use. It's important you see that first, because unless you're into functional programming, the behind-the-scenes stuff may seem confusing.

/* Trivial example of a protocol - send a byte containing the payload
 * length, followed by that many bytes of data.
 * 
 * This server just sends back the same data in the same way.
 */
static IEnumerable<AsyncIOPipeAction> HandleMyClient()
{
    // Start by reading a byte from the client
    byte[] singleByte = new byte[1];
    yield return pipe => pipe.Read(singleByte, 0, 1);
 
    // That byte tells us how much to read next
    int inputPayloadLength = singleByte[0];
 
    byte[] inputPayload = new byte[inputPayloadLength];
    yield return pipe => pipe.Read(inputPayload, 0, inputPayloadLength);
 
    // Send same thing back again
    yield return pipe => pipe.Write(singleByte, 0, 1);
    yield return pipe => pipe.Write(inputPayload, 0, inputPayloadLength);
}

As you can see, this function doesn't actually get passed a socket to work on. And yet it has these suspiciously convenient-looking calls on to a pipe object. Where does that pipe object come from and what is it?

Note how the whole function returns IEnumerable<AsyncIOPipeAction>, so it produces actions to be called somehow. Those actions are delegates:

public delegate void AsyncIOPipeAction(IAsyncIOPipe pipe);

That pipe object supports this interface:

/* Example of an interface with operations that are either synchronous 
 * or asynchronous depending on the context in which they are called.
 */ 
public interface IAsyncIOPipe
{
    void Read(byte[] buffer, int offset, int size);
    void Write(byte[] buffer, int offset, int size);
}

You could follow the same pattern with any facility that supports asynchronous usage, but here I just provide two obvious operations on a socket.

So in the client-handling function, whenever you want to make use of the socket, you do a yield return to hand off some code that can be called back to. Your code must accept a pipe (which is just what you wanted). Then it can call a method on it.

The implementation IAsyncIOPipe is then very simple indeed:

class SocketPipe : IAsyncIOPipe
{
    private readonly Socket _socket;
    private readonly IEnumerator<AsyncIOPipeAction> _actions;
    
    public SocketPipe(Socket socket, 
                      IEnumerable<AsyncIOPipeAction> actions)
    {
        _socket = socket;
        _actions = actions.GetEnumerator();
 
        Step();
    }
 
    private void Step()
    {
        if (!_actions.MoveNext())
            return;
 
        _actions.Current(this);
    }
 
    private void Callback(IAsyncResult result)
    {
        _socket.EndReceive(result);
        Step();
    }
 
    public void Read(byte[] buffer, int offset, int size)
    {
        _socket.BeginReceive(buffer, offset, size, 
                             SocketFlags.None, Callback, this);
    }
 
    public void Write(byte[] buffer, int offset, int size)
    {
        _socket.BeginSend(buffer, offset, size, 
                          SocketFlags.None, Callback, this);
    }
}

The constructor is passed a bunch of actions to be carried out asynchronously. It doesn't actually care how that collection of actions is implemented. But of course we can call HandleMyClient to produce a suitable collection.

SocketPipe starts the ball rolling immediately, by starting the enumeration and then calling Step. This executes the first portion of code in the function up to the first yield return, after which we get control back. We then just execute the lambda giving it a pipe to work on - our implementation, in fact.

This means that when the lambda calls Read or Write, we can simply begin the right kind of asynchronous operation on our concealed socket, and we take care of specifying the callback. All our callback has to do is end the asynchronous operation and run the next Step.

Exercises for the reader:

  • There isn't much consideration given to error handling, mainly because I never even looked at the asynchronous socket APIs until just now, so I'm not sure where it needs to go. It should be possible to put error handling into this approach without putting any mess in the application code (e.g. HandleMyClient).
  • It should be possible to launch multiple asynchronous calls on multiple objects, where that makes sense (I can't see how it does in this simple example, but it might if you have multiple sockets for some reason). The lambda would need to be passed multiple parameters, one for each object currently "in play", and the management object would need to ensure that all outstanding asynchronous calls had completed before it called Step again.

Solution zip file is here:

http://www.earwicker.com/downloads/asyncsockets.zip

Wednesday, 19 November 2008

COIL and COILettes

Douglas Crockford started a minor war when he observed that JavaScript's object initialisation syntax provides a neat way to persist hierarchical data, maybe more suitable to some situations than XML.

Since C# 3.0 we've had a similar facility in C#. This means an opportunity for another war.

I've started to see little object initialisation trees appearing in my code, and they start to look like a declarative internal DSL. I should post a real example some time. Anyway, this can only mean one thing: we need a snappy name for these things.

JSON stands for JavaScript Object Notation, and it sounds sort of like the name Jason, which makes it catchy. So we need a four letter acronym that starts with CS (for C-Sharp) where you pronounce the first letter as a syllable and it all sounds like a person's name. This immediately lead me to:

CSIL (pronounced "Cecil" in the American way):

C-Sharp Initialisation Language.

Sadly somebody already used that for something else.

Ah well, it was just too perfect.

But not to be discouraged, I relaxed the rules so it just has to be a four letter acronym that sounds like a word, and went for:

COIL (pronounced "coil", amazingly):

C# Object Initialisation Language.

The beauty of this becomes apparent when you consider that we also need a way to describe those little islands of COIL that appear embedded in our C# programs.

They are called COILettes.

Thursday, 13 November 2008

Chain – a LINQ operator for dealing with Linked Lists

I don’t think there’s anything in LINQ that will do this, though I expect I’m wrong - I have a tendency to write my own extension method to do something and then discover that LINQ already provides a general version of it. But in the mean time, here’s my Chain method:

public static IEnumerable<T> Chain<T>(this T first, Func<T, T> follow) where T : class
{
    for (T item = first; item != null; item = follow(item))
        yield return item;
}

It’s designed to be used in any situation where a traditional linked list exists. I used it in my AutoDisposal library to get a list of all the types inherited by a given type. It chains a bunch of objects together into an enumerable list by following links between them.

The irritation that provoked this minor burst of creativity is that if you use the GetFields method of Type, there doesn’t seem to be a simple way to get all the instance fields in that type, including inherited ones. By default, GetFields does include inherited fields, but only public ones. To get all public and non-public instance fields, including all inherited ones, you have to walk the chain of base types and get the fields declared in each type. So you’d better use BindingFlags.DeclaredOnly, or you’ll end up with duplicates of the public fields.

I wanted to write a LINQ expression that would get me a single flat list of all the fields in a type, so this is how I used Chain as part of it:

instance.GetType()
    .Chain(type => type.BaseType)
    .SelectMany(type => type.GetFields(BindingFlags.Instance | 
                                       BindingFlags.Public | 
                                       BindingFlags.NonPublic | 
                                       BindingFlags.DeclaredOnly))
    

In this case, the BaseType property of Type is the “next item” pointer in the linked list I’m interested in, but to use LINQ effectively we need it in the form of an IEnumerable<Type>, and so Chain provides a nifty way to do that.

Shouldn’t something like Chain be in the BCL (if it isn’t already?)

Wednesday, 12 November 2008

AutoDisposal using PostSharp

I complain to anyone who will listen about the poor language support in C# for the IDisposable interface. Yeah, we’ve got the using statement, and that’s fine as far as it goes.

But compare that to what C++/CLI has: the most complete support of any .NET language. Not just the equivalent of the using statement, but also automatic implementation of IDisposable that takes care of disposing of nested owned objects, including inherited ones, like magic (relatively speaking, unless you’re a C++ programmer in which case you’ve taken it for granted for a decade or two).

The relationship between deterministic clean-up (i.e. destructors) and garbage collection (i.e. finalizers) was quite vaguely understood until Herb Sutter clarified as part of his work on C++/CLI. But now it’s all very clear how it should work – the only problem is, there doesn’t seem to be any movement towards fixing it in any future version of C#.

Anyway, by now you’re probably bored of the ranting and wondering where the code snippets are. So here’s one:

class FunkyResource : IDisposable
{
    public string Label { get; set; }
 
    public void Dispose()
    {
        Console.WriteLine("Disposing " + Label);
    }
}

Not particularly impressive, I grant you, but it serves as a dummy example of a class that represents some resource that needs to be cleaned up. Here we just log the fact that a given instance is being cleaned up, identifying it with a label string.

Here’s where it gets interesting:

[AutoDisposable]
class FunkyOwner
{
    [AutoDispose] 
    private FunkyResource _resource1 = new FunkyResource { Label = "resource1" };
 
    [AutoDispose] 
    private FunkyResource _resource2 = new FunkyResource { Label = "resource2" };
}

This class owns a couple of FunkyResource instances. By “own” I simply mean that when an instance of this class is disposed of, those two resource instances must also be disposed of.

But wait a second – how does FunkyOwner implement IDisposable? The answer is that it automatically does so, because of that [AutoDisposable] attribute.

This is all thanks to the wonderfully powerful PostSharp, which effectively allows you to extend any CLR-based language through custom attributes. And because PostSharp builds on the CLR in a language-independent way, this means that the extensions you create should work in any CLR-based language that supports attributes.

Very briefly, PostSharp registers an extra step in the compilation process of Visual Studio: after your output assembly is built, a program called PostSharp.exe takes a look at it, and performs additional processing on the raw IL in the assembly. And hey presto, extra features make their way into your assembly.

Before we see how this example works, what about inheritance?

class DerivedOwner : FunkyOwner
{
    [AutoDispose] 
    private FunkyResource _resource3 = new FunkyResource { Label = "resource3" };
}

Note that because we’re deriving from a class that has the [AutoDisposable] attribute, we don’t need to specify it again (it will be ignored if we do). If we call Dispose on an instance of DerivedOwner, we get this output:

Disposing resource3
Disposing resource1
Disposing resource2

That is, the resources owned by DerivedOwner are disposed of first, then the base class’s resources are also disposed of. So inheritance works fine.

And what about a mixed scenario, where I want automatic clean-up of owned resources but I also want to run some custom code of my own during disposal?

class MixedOwner : IDisposable
{
    [AutoDispose]
    private DerivedOwner _owner2 = new DerivedOwner();
 
    public void Dispose()
    {
        Console.WriteLine("Disposing MixedOwner");
 
        this.TryDisposeFields();
    }
}

Note the call to TryDisposeFields(), an extension method I cooked up (see below). The reason for the prefix ‘Try’ is that you can call it on anything and it will look for fields marked with the [AutoDispose] attribute. If it finds any, it will dispose of them. If it doesn’t find any, that’s okay – nothing happens.

When manually implementing IDisposable like this, consider making it virtual, so that derived classes can override it, although they must of course call the base class’s version after performing their custom cleanup. It might be good practice in some circumstances to follow the pattern where you have a separate virtual method Dispose(bool) instead. However, that is really intended to allow you to implement a finalizer and a Dispose method together, and these days there are almost no circumstances in which it is recommended that you write a finalizer (unfortunately a lot of old books give out-of-date advice here).

Also note that we don’t need the [AutoDisposable] attribute on the class, as we already implement IDisposable (again, it would have been harmless to add it).

Unsurprisingly, the output of disposing an instance of MixedOwner is:

Disposing MixedOwner
Disposing resource3
Disposing resource1
Disposing resource2

All very nice. And extremely easy to implement, using the “easy” mode of PostSharp which is known as Laos. No need to directly manipulate IL, just write classes to represent your attributes, deriving from base classes that take care of the messy details.

My [AutoDispose] attribute is actually completely trivial because it’s just a simple marker on fields that I look for using reflection. So this isn’t inherently anything to do with PostSharp:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public sealed class AutoDisposeAttribute : Attribute { }

Where PostSharp makes an appearance is in the [AutoDisposable] attribute:

[Serializable]
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class AutoDisposableAttribute : CompositionAspect
{
    public override object CreateImplementationObject(InstanceBoundLaosEventArgs eventArgs)
    {
        return new AutoDisposableImpl(eventArgs.Instance);
    }
 
    public override Type GetPublicInterface(Type containerType)
    {
        return typeof(IDisposable);
    }
 
    public override CompositionAspectOptions GetOptions()
    {
        return CompositionAspectOptions.IgnoreIfAlreadyImplemented;
    }
}

By inheriting the attribute from PostSharp.Laos.CompositionAspect, I’m saying that I want to add an extra interface to any class marked with my attribute. The implementation is created and returned from the CreateImplementationObject method. The other overrides are pretty self-explanatory.

Here’s what my AutoDisposableImpl class looks like:

public sealed class AutoDisposableImpl : IDisposable
{
    private readonly object _instance;
 
    public AutoDisposableImpl(object instance)
    {
        _instance = instance;
    }
 
    public void Dispose()
    {
        _instance.TryDisposeFields();
    }
}

Pretty simple, eh? It just stores a “back pointer” to the object we are extending, so it can call the TryDisposeFields extension method on whatever that object happens to be. In a way, it’s just like MixedOwner except it doesn’t do anything extra.

Actually most of the complicated mess is hidden in that extension method. So here’s the gory detail:

public static void TryDisposeFields(this object instance)
{
    instance.GetType()
        .Chain(type => type.BaseType)
        .SelectMany(type => type.GetFields(BindingFlags.Instance | 
                                           BindingFlags.Public | 
                                           BindingFlags.NonPublic | 
                                           BindingFlags.DeclaredOnly))
        .Where(fieldInfo => fieldInfo.GetCustomAttributes(typeof(AutoDisposeAttribute), false)
                                     .OfType<AutoDisposeAttribute>().Any())
        .Select(fieldInfo => fieldInfo.GetValue(instance))
        .OfType<IDisposable>()
        .ForEach(field => field.Dispose());
}

As you can see, I enjoy chaining a lot of LINQ operators together. The first one, Chain, is a little custom gadget of mine that turns any linked list of T into an IEnumerable<T>. I’ll describe it in a separate post.

After that, I get all the fields of all the types in the inheritance chain into a flat list, using the marvelous SelectMany method. Then I filter them according to whether they have the [AutoDispose] attribute, and select the values of the remaining fields, then filter again based on whether they support IDisposable, and finally I dispose of each one. (That last ForEach method is another one of mine, and I’ve seen a lot of people suggesting the same thing).

So what are the drawbacks of this marvelous scheme? The one major issue with the PostSharp approach is that the Visual Studio IDE does its own compilation of your source to provide auto-completion and other kinds of “intellisense”. It doesn’t look at the assembly on disk, because there might not be one (e.g. if the code doesn’t completely compile without errors yet, as is often the case when you are adding new code, which is precisely the time when you require intellisense features).

This means that the IDE doesn’t know that [AutoDisposable] classes support IDisposable. Nor does the real compilation stage that happens during the build. PostSharp doesn’t kick in until after the compilation has completed. The upshot is that we cannot do this:

using (new FunkyOwner())
{
    Console.WriteLine("Using FunkyOwner...");
}

The using statement needs to statically verify that the object supports IDisposable. It won’t try to resolve this at runtime.

This would seem to be not so much a drawback, more a total friggin’ disaster. However, there is a solution, in the form of another generally applicable extension method:

public static void TryUsing<T>(this T instance, Action<T> action)
{
    try
    {
        action(instance);
    }
    finally
    {
        IDisposable disp = instance as IDisposable;
        if (disp != null)
            disp.Dispose();
    }
}

Now we can write:

new FunkyOwner().TryUsing(funkyOwner =>
{
    Console.WriteLine("Using FunkyOwner...");
});

I arranged the interface of the TryUsing method carefully in order to mimic the characteristic of the using statement. In particular, the object to be disposed of is only given a name by the lambda parameter. This means that it cannot be accidentally used outside of the block where it is “in scope” (i.e. still not yet disposed), unless the programmer makes a special effort to circumvent this protection by storing a reference to the object in a variable declared outside the lambda.

And with that, I have now discussed every single part of the AutoDisposal library. You can download the complete source along with a demonstration program here:

http://www.earwicker.com/AutoDisposalSource.zip

You will of course also need the PostSharp system, which you can get here:

http://www.postsharp.org/download/