看了“菜鸟耕地”的”.NET开源高性能Socket通信中间件Helios介绍及演示“,觉得这个东西不错。但是由于没有网络编程知识,所以高性能部分我就讲不出来了,主要是想根据开源代码跟大家分享下Helios的架构。
源代码下载地址:https://github.com/helios-io/helios
首先我们献上服务器端结构图:
这样的一个大图片,估计很多地方都挺迷糊的,我们就详细的讲解下期中的逻辑。
ServerBootstrap类
该类是服务器端的核心类,服务器端提供服务的就是ServerBootstrap对象(实际上是它的子类,并且子类是由这个对象创建的)。
创建代码时我们会使用代码
var serverFactory =
new ServerBootstrap()
.SetTransport(TransportType.Tcp)
.Build();
- 该类有三个核心属性:IExecutor 、IServerFactory(IConnectionFactory)、NetworkEventLoop。
public class ServerBootstrap : AbstractBootstrap { protected IExecutor InternalExecutor { get; set; } protected NetworkEventLoop EventLoop { get { return EventLoopFactory.CreateNetworkEventLoop(Workers, InternalExecutor); } } protected override IConnectionFactory BuildInternal() { switch (Type) { case TransportType.Tcp: return new TcpServerFactory(this); case TransportType.Udp: return new UdpServerFactory(this); default: throw new InvalidOperationException("This shouldn't happen"); } } public new IServerFactory Build() { return (IServerFactory) BuildInternal(); } }