Ras*_*ber 24 .net c# frameworks nio
是否有适用于.NET的非阻塞IO框架?
我正在寻找类似于Apache Mina和JBoss Netty为Java提供的东西:一个用于实现高度可伸缩服务器的框架 - 而不仅仅是.NET框架提供的低级支持.
编辑:为了更好地解释我想看到的内容,这里有一个基本的例子,你可以用Mina做什么:
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
像这样的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.
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过调用启动服务器
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();
Run Code Online (Sandbox Code Playgroud)
我会得到一个非阻塞服务器.
它将运行单个(或至少几个)线程,循环遍历所有传入连接,从套接字收集数据,并调用SimpleDecoder.doDecode()以查看它是否在连接上有完整的命令.然后它将命令传递给CommandHandler.messageReceived(),我可以接管处理.