本文介绍了.NET 的任何 NIO 框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 是否有任何非阻塞 IO 框架?

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

我正在寻找类似于 Apache MinaJBoss Netty 为 Java 提供:一个用于实现高度可扩展的服务器的框架——而不仅仅是 .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.

为了更好地解释我希望看到的内容,这里有一个基本示例,说明您可以使用 Mina 做什么:

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

在 Mina 中,我可以像这样实现 ProtocolDecoder:

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);
  }
}

还有一个像这样的 CommandHandler:

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.

它将运行一个(或至少几个)线程,循环遍历所有传入连接,从套接字收集数据,并调用 SimpleDecoder.doDecode() 以查看它是否具有连接上的完整命令.然后它将命令传递给CommandHandler.messageReceived(),我可以接管处理.

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.

推荐答案

你可以看看SuperSocket,http:///supersocket.codeplex.com/它可能不像 Mina 和 Netty 那样强大,但它是一种简单的框架,您可以轻松使用.

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.

这篇关于.NET 的任何 NIO 框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 16:40