I'm presenting: Node.Net a real alternative to Node.JS (Video Tutorial included!) #csharp #nodejs #dotnet #async

I have been working a couple of days on Node.Net. A Node.JS alternative that uses the brand new features from .NET 4.5

Put the video at 720p and full screen to watch it fine.

Project: https://github.com/soywiz/nodenet

What's the point on making a Node.JS clone?

Node.JS has several problems that won't be solved:
  • Fully dynamic typing (IDEs can't make a good work on autocompleting) and you would end working without autocompleting at all, and detecting errors when executing and not while coding.
  • There is no great IDEs for javascript
  • The flow is event-driven
  • Javascript don't support real classes, and lacks lots of programming features
  • The way you should extend javascript is problemating (.NET's extension methods are much better)
  • Spreading exceptions is a pain and you can't between async methods
What offers Node.NET?

Node.NET works with any .NET language and even when it presents the problem that allows to to make async wrong, the IDE helps you to doing it right as you can see in the video below.

  • .NET and VS11 offeres the best static typing out there
  • Has lots of very powerful APIs
  • It's a modern OO programming language with all the stuff you could expect and like: operator overloading, class inheritance, extension methods, LINQ, events, dynamic support...
  • Visual Studio Express is free, and you can use MonoDevelop too
  • The async/await model converts asynchronous methods into very efficient machine states when executing.
  • Allows you to create async code with a sequential look and flow
  • Allows you to use try/catch with async methods, so you can spread exceptions in an easy way
  • All the .NET advantages and it's great speed
  • It can work on linux and make using Mono >= 2.12 that already supports .NET 4.5
Hello World Http using Node.JS:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write('Hello World\n', function() {

ab -n 10000 -c 200
Requests per second:    7644.83 [#/sec] (mean)

Hello World Http using Node.Net:

await HttpServer.Create(async (Request, Response) =>
  Response.Buffering = true;

  Response.Code = 200;
  Response.Headers["Content-Type"] = "text/plain";
  await Response.WriteChunkAsync("Hello World!");
  await Response.WriteChunkAsync("");

ab -n 10000 -c 200 
Requests per second:    8374.73 [#/sec] (mean)

Notice that while debugging using Visual Studio the RPS will be about 1000 as much. That's normal because the debug execution is much slower. When launching as a normal executable, RPS will achieve those values.


Load .OGG files on XNA (or silverlight) #dotnet #xna

Currently I'm trying to reduce to the maxium a set of resources for a XNA port. Embedding image and sound files as XNA Content, the size of the files ends being much greater than their equivalent png, jpg or ogg.

I discovered that XNA allows to load gif, png and jpg files directly:

Image = Texture2D.FromStream(GraphicsDevice, TitleContainer.OpenStream("image.jpg"));

After that I saw the SoundEffect equivalent. SoundEffect.FromStream requires the input file to be a WAV file. A few weeks ago I started a port of the Jorbis library (that is based on libvorbis/libogg) to managed .NET.
I have performed a few changes in order to make it very easy to use on XNA:

SoundEffect = SoundEffect.FromStream(new OggToWavStream(TitleContainer.OpenStream("file.ogg")));

You can use on your XNA projects just including the NVorbis project. You can grab the source code here:


Project doesn't have any dependency and it is a portable library. In order to be able to open it (portable libraries) on VisualStudio you need the following plugin:



C# : Big/Little Endian Integers using struct + implicit operator

Using binary data with C/C++ or D is pretty easy. Those languages have pointers and structs and fields on structs have a fixed memory offset related to the start of the struct. That way you can read a bunch of bytes and get a pointer to a struct on those bytes without extracting or reinserting the struct fields on the array.

On C# using the unsafe + fixed keywords we can use pointers and even pointers to simple structs. Only if those structs contains just primitive values or structs satifying this condition. This allows mapping a byte array to a struct pointer, and that simplifies a lot working with structures and binary data.

Lately I have been working on stuff where i had to work with binary data that contained Big Endian and in some cases mixed with other fields in Little Endian. How to work with this stuff easily?

Some people says that using BitConverter. Other even using bit shifting directly.

I have a much better alternative that will allow us to have structures/fields/variables or parameters using BigEndian and work with them seamlessly. Though the real utility is for serializing/deserializing and mapping pointer's memory.

We can achieve that creating an structure containing a single private integer of the desired width, and using the implicit operator.

For a 32 bits integer:

public struct uint_be
 private uint _InternalValue;

 public uint NativeValue
   _InternalValue = MathUtils.ByteSwap(value);
   return MathUtils.ByteSwap(_InternalValue);

 public static implicit operator uint(uint_be that)
  return that.NativeValue;

 public static implicit operator uint_be(uint that)
  return new uint_be()
   NativeValue = that,

With this we will be able to use the uint_be type as if it were a normal uint value. This will make an automatical cast to the uint type. The field on memory will be as a big endian integer. But we will be able to get the value as a little endian with the property or just using the value as if it were a normal uint. This struct could be improved detecting the endianness of the platform and avoiding to perform the ByteSwap if it is already big endian.


PlayStation SUITE GDC Online 2011 (Slides) : C# Development on PSVita and more


C# : FastCGI async server using async CTP

Since the launch of the Ascyn Programming Content Preview for .NET, I wanted to use it. And now that the GIT version of Mono supports async, I have given it a try.

Since a long time I was looking for change to .NET for web development, but I wanted to have more control on what's going on than using ASP.NET or ASP.NET MVC. Now I have a FastCGI server as a library supporting FastCGI as well as a templating system compatible with django/twig that I'm going to port to Roslyn CTP.

Using async FastCGI + nginx works really fine, much better and somehow simpler than a FastCGI implementation I have done using Threads.

The point of using async FastCGI is to handle all the requests in a async way.

class Program
    class MyFastcgiServerAsync : FastcgiServerAsync
        public override async Task HandleRequestAsync(FastcgiRequestAsync Request)
            var StreamWriter = new StreamWriter(Request.Stdout);
            await StreamWriter.WriteAsync("Content-type: text/html\r\n");
            await StreamWriter.WriteAsync("\r\n");
            await StreamWriter.WriteAsync("Hello World!");
            await StreamWriter.FlushAsync();

    static void Main(string[] args)
        new MyFastcgiServerAsync().Listen(8000);

Source code: Async FastCGI Request Handler, Async FastCGI Library


Meta-programming - 0. Introduction

In all my years programming some times I have been working with metaprogramming on several programming languages and using different approaches. And I would like to use all the knowledge adquired through this time, to write an article about this issue.

Wikipedia quote:

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.
The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity.
On most of the programming languages there are some ways to make use of metaprogramming:
  • In C, C++ and assembler there is a preprocessor (macros) (metalanguage)
  • In C++, D, Java and C# you have templates and generic types
  • In D you have template mixins and pure functions that can be executed on compiling time
  • In PHP, ActionScript 3, Java and C# you have Reflexion
  • In Python, Squirrel, ActionScript 3, Java and C# you have Annotations or Custom Attributes
  • In PHP and Javascript you can use eval or includes to dynamically-generated files (templating systems)
  • In C#, Java and Actionscript (and all the other languages running in a Virtual Machine) you can emit bytecodes of that Virtual Machine and execute them
  • On lots of programming languages, there is a way to generate native code and execute it JIT (Just In Time) and dynarec (Dynamic Recompilation).
  • In C#, J# and other languages, you can access the code of delegates as abstract trees (Expression Trees). Very useful for LINQ (Language INtegrated Query)

I will write about those meta-programming topics in coming articles. Their uses, good points and problems and practical cases where I have used them.

Metaprogramming is one of the most advanced topics on programming, so these coming articles are targeted to the more experienced programmers.


X-SendFile or how to send a big static file to the client

X-SendFile is a header that can be sended from a scripting language like PHP, Python, Perl, Ruby through CGI (or equivalent) or in a FastCGI applications that is preprocessed by the http server (and it won't be sended to the client) and specify to the server that have to serve a static file instead of sending the CGI output.

In the case we want to send a big file from a CGI using authentication. The traditional way was yo establish the header Content-Type and send all the binary data. Some sophisticated programs even parsed HTTP headers in order to send only chunks of the file. However, each one of those request use memory, handles and processes during all the transfer, communicating with the http server that is performing as a bridge, sending all the data to the client.

Using X-SendFile you only need to send that header specifying the path to the file we want to send, and to finalize the CGI/FastCGI request. The script is in memory only the time that you need to verify the authentication and to send only a single header to the server. This is much more efficient. Even we can store in a session that the user have permissions to downlaod the file and we will be able to access that session in a fast way. The http server will be in charge of handling the headers to send the data the user request.

header('X-SendFile: /path/to/my/file');

The X-SendFile header is implemented on the most populars http servers, in the main core: lighttpd and nginx, or with a mod mod_xsendfile for apache.

Each server handles the header in a way, allowing to specify configuration parameters and security restrictions per configuration. You will have to read the documentation of your server for more information.