Recent Posts

Community

Email Notifications

Archives

"Oleg Mihailik, Kyiv, Ukraine"

Developer Security MVP

December 2005 - Posts

Asynchronous iterators

There are some new ideas in C# 2.0 that has the fleur of eye candy. Iterators and anonymous methods are not very useful in real projects.

 

Can you find any target scenario where iterators help thing done with great benefit? In presentations iterators do its work enumerating deep object trees. But have you deep object trees in C#? I think no. You may be store deep trees in XML or SQL database. But in C# they are rare cases. By the way deep tree hierarchy make GC performs slower.

 

Anonymous delegates are such eye candy feature too. You can write simple HelloWorld application with anonymous delegate, but where to use it in real life? All that List<T>.ForEach methods are nearly no benefit over plain easy cycle.

 

The idea!

 

I found it about a month ago. So today I present great application of these new features power. They may be really useful. In special case, of course. This case is…

 

Asynchronous execution

 

To make story more concrete I’ll speak about HTTP server. It receives requests and respond some content out. If you write it as single-threaded application, it will perform REALLY ugly. It is completely unacceptable to run read/process/respond cycle for HTTP server.

 

Of course, multithreading adds some piece of work. You can write single-threaded HTTP server in about a hour or half. The algorithm itself is not too hard. But when you start to deal with multithreading and asynchronous processing it starts to be crazy.

 

There is a pair of methods in .NET for asynchronous execution: BeginReceive/EndReceive. In other places such methods may be BeginRead/EndRead or something like BeginABC/EndABC. And the problem with asynchronous algorithms is BeginReceive and callback should live in different methods.

 

You never can write sequential linear logic to receive and process  HTTP request. Because HTTP request are received in chunks and you should be prepared to do some part of work and wait for next chunk received. It is really hard!

 

The problem is internal state. When you complete some piece of work, you should pass this state to place, where next piece will be run. It is really hard to do it right. As always with multithreading and thread state sharing.

 

Solution

 

You can see my solution in C# source code form (link below). By the power of new C# 2.0 iterator I built the solution to un-roll synchronous algorithm to asynchronously executed pieces. And the problem of thread state sharing and passing between steps is managed by C# compiler. I think it is some acrobatic use of eye-candy feature.

 

Anonymous methods are used too, to make code more laconic.

 

Don’t you think it is really nice?

 

Invoke.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=aeea7891-84e7-4b00-bddb-3e393a335170
   
Simple delegate, void Invoke().

AsyncSteppingProcess.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=5106bf37-01c2-4ddf-bc1f-e193e25f0018
   
Complex delegate. Asynchronous algorithm should adhere to this delegate to be un-rolled in my solution.

AsyncStepping.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=1ee3fa00-973f-4201-bf48-752ea39c2546
   
Heart, core of solution. Here the synchronous algorithm is un-rolled to asynchronously executed chunks.

AcceptSocketProcess.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=a9557a71-8e52-48ef-b867-d0f78e636e77
   
First of two algorithms had un-rolled. Receipting the HTTP connections by BeginAccept/EndAccept of Socket class.

ParseRequestProcess.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=b7dac2c7-1c89-49ee-8756-88c8ecac2263
   
Second of two algorithms had un-rolled. Receiving the HTTP request and parsing. I show very simplified, minimalistic parser. I am even sorry to call it “parser”. Just read request to string field.

HttpRequest.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=49ae0bed-acff-44c9-bb4a-05e64bca07e4
   
Object to store HTTP request details. In my current implementation it have just one string field for complete request text.

Program.cs
http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=27f3203d-e0f3-4f0a-a48e-c9822d6c4af0
   
Main program. Initiates the server and responds to requests. Some kind of “Hello World” in HTTP.

I am here :-)

Hello, World!

My name is Oleg Mihailik, I am Developer Security MVP (former C# MVP) and I am living in Kyiv, Ukraine.

This blog I intend primarily for projects that may be interested to international community. I have more localized blog at blogs.gotdotnet.ru/personal.mihailik, where I place Russian posts.

My first posts will be about one of my proudests projects — HttpListener. It is re-creating of Microsoft's System.Net.HttpListener API for pure Socket-based implementation. See it soon at this blog.

Language

My mother's tongue is Russian. And I am living is Ukraine, so my second fluent-spoken language is Ukrainian. As you can see, I do speak English too. At least a little bit. I hope my language mistakes will not break your heart or political/religious feelings.