C# 任何用于 .NET 的 NIO 框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/648282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any NIO frameworks for .NET?
提问by Rasmus Faber
Are there any non-blocking IO frameworks for .NET?
.NET 是否有任何非阻塞 IO 框架?
I am looking for something similar to what Apache Minaand JBoss Nettyprovides for Java: a framework for implementing highly scalable servers - not just the low-level support that the .NET framework provides.
我正在寻找类似于Apache Mina和JBoss Netty为 Java 提供的东西:一个用于实现高度可扩展服务器的框架 - 而不仅仅是 .NET 框架提供的低级支持。
EDIT:To better explain what I would like to see, here is a basic example of what you can do with Mina:
编辑:为了更好地解释我想看到的内容,以下是您可以使用 Mina 执行的操作的基本示例:
In Mina I can implement a ProtocolDecoder like this:
在 Mina 中,我可以实现这样的 ProtocolDecoder:
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:
还有一个像这样的 CommandHandler:
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.
它将运行单个(或至少几个)线程,循环遍历所有传入连接,从套接字收集数据,并调用SimpleDecoder.doDecode()
以查看它是否具有连接的完整命令。然后它将命令传递给CommandHandler.messageReceived()
,我可以接管处理。
回答by Vinko Vrsalovic
There is the XF.Server, which this questionsays is flaky. This last question offers advice about how to write high performing networking code in .NET (use async sockets, etc.)
有XF.Server,这个问题说它是片状的。最后一个问题提供了有关如何在 .NET 中编写高性能网络代码的建议(使用异步套接字等)
There's also a preview of C# Network Programmingon Google Books which discusses, among other things, asynchronous socket calls.
Google Books 上还有C# Network Programming的预览版,其中讨论了异步套接字调用等内容。
This MSDN articleis also interesting, but gets you no closer to an actual framework.
这篇MSDN 文章也很有趣,但并没有让您更接近实际的框架。
回答by Kerry Jiang
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.
你可以看看 SuperSocket,http://supersocket.codeplex.com/ 它可能不像 Mina 和 Netty 那样强大,但它是一种你可以轻松使用的简单框架。