26

Are there any non-blocking IO frameworks for .NET?

I am looking for something similar to what Apache Mina and JBoss Netty provides for Java: a framework for implementing highly scalable servers - not just the low-level support that the .NET framework provides.

EDIT: To better explain what I would like to see, here is a basic example of what you can do with Mina:

In Mina I can implement a ProtocolDecoder like this:

public class SimpleDecoder extends CumulativeProtocolDecoder {
  protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    if (in.remaining() < 4) 
      return false;
    int length = in.getInt();
    if(in.remaining() < 4 + length)
      return false;
    Command command = new Command(in.asInputStream());
    out.write(command);
  }
}

And a CommandHandler like this:

public abstract class CommandHandler extends IoHandlerAdapter{
  public void messageReceived(IoSession session, Object message) throws IOException, CloneNotSupportedException {
    Command command = (Command) message;
    // Handle command. Probably by putting it in a workqueue.
  }
}

If I start the server by calling

CommandHandler handler = new CommandHandler();
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new SimpleDecoder(false)));
acceptor.setLocalAddress(new InetSocketAddress(port));
acceptor.setHandler(handler);
acceptor.bind();

I will get a non-blocking server.

It will run a single (or at least just a few) threads, cycling through all incoming connections, gathering data from the sockets, and call SimpleDecoder.doDecode() to see if it has a complete command on the connection. Then it will pass the command to CommandHandler.messageReceived(), and I can take over the processing.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • Here is a well designed NIO framework for Java: http://www.jboss.org/netty. I'm looking for an equivalent for .Net (and I supposed that Rasmus do too). It's not just asynchronous IO but a framework on top which simplifies development greatly. – jgauffin Mar 23 '11 at 13:53
  • 1
    Non-blocking I/O has been part of .NET since day 1. What are you looking for,exactly? – John Saunders Mar 15 '09 at 18:26
  • I am looking for a server-framework on top of the low-level calls in .NET. – Rasmus Faber Mar 15 '09 at 18:35
  • 1
    I don't know enough about Java to know why something like MINA would be helpful. But async access is built into every level of .NET. It's in WCF, ADO.NET, Workflow - everywhere. I looked at the MINA page and see no value in .NET for anything they said on that page. – John Saunders Mar 15 '09 at 18:41
  • Maybe you could give us some idea of the kind of program you need to write. Don't assume that something like MINA is required to do that. – John Saunders Mar 15 '09 at 18:42
  • Are you looking for ASP.NET's support for asynchronous pages? Allowing an async-operation to complete while allowing the original processing thread to process other HTTP requests and then picking up the original request when the async-operation completes? – Richard Mar 16 '09 at 17:27
  • If you're looking for a high-performance socket server for .NET, there is Helios https://github.com/helios-io/helios which is very good, however it is now deprecated and the latest work is on DotNetty https://github.com/azure/dotnetty which is a full port of Java's Netty and started by the Microsoft Azure team. – Mani Gandham Jul 05 '16 at 13:53
  • Havent tried it but look at https://github.com/Azure/DotNetty/blob/dev/README.md – Peter Sep 22 '16 at 19:56

3 Answers3

7

You can take a look at SuperSocket, http://supersocket.codeplex.com/ It may not be strong like Mina and Netty, but it is kind of simple framework you can use easily.

Kerry Jiang
  • 1,168
  • 13
  • 10
3

There is the XF.Server, which this question says is flaky. This last question offers advice about how to write high performing networking code in .NET (use async sockets, etc.)

There's also a preview of C# Network Programming on Google Books which discusses, among other things, asynchronous socket calls.

This MSDN article is also interesting, but gets you no closer to an actual framework.

Community
  • 1
  • 1
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • Thanks for your info, especially the link: http://stackoverflow.com/questions/319732/tips-techniques-for-high-performance-c-server-sockets – Dodd Oct 04 '09 at 09:22
0

You can use Mina directly in .Net via ikvm.

Todd Stout
  • 3,687
  • 3
  • 24
  • 29
  • People using Mina are looking for high performance. Do you think mina used with ikvm will perform well ? – bokan Oct 10 '13 at 08:28
  • 4
    Generally non-blocking IO does not necessarily give better performance, but better scalability. Non-blocking socket IO prevents the need for a thread per connection. Servicing many connections on a single thread reduces the memory and context switch overhead of multiple threads. Since nio operations are IO bound and not CPU bound, I suspect, but have not evidence, that any overhead introduced by ivkm is negligible. – Todd Stout Oct 11 '13 at 22:57