亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

helios架構詳解(一)服務器端架構

2019-11-14 14:28:15
字體:
來源:轉載
供稿:網友

看了“菜鳥耕地”的”.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();        }    }
核心屬性和方法
  • 另外一個有特點的地方就是鏈式編程(可能借鑒于jquery),設置對象都返回個this指針。
    public class ServerBootstrap : AbstractBootstrap    {       public ServerBootstrap WorkersshareFiber(bool shareFiber)        {            UseSharedFiber = shareFiber;            SetOption("proxiesShareFiber", UseSharedFiber);            return this;        }        public new ServerBootstrap SetTransport(TransportType type)        {            base.SetTransport(type);            return this;        }        public ServerBootstrap WorkerThreads(int workerThreadCount)        {            if (workerThreadCount < 1) throw new ArgumentException("Can't be below 1", "workerThreadCount");            Workers = workerThreadCount;            return this;        }        public ServerBootstrap BufferSize(int bufferSize)        {            if (bufferSize < 1024) throw new ArgumentException("Can't be below 1024", "bufferSize");            BufferBytes = bufferSize;            return this;        }        public ServerBootstrap WorkersAreProxies(bool useProxies)        {            UseProxies = useProxies;            return this;        }        public ServerBootstrap Executor(IExecutor executor)        {            if (executor == null) throw new ArgumentNullException("executor");            InternalExecutor = executor;            return this;        }        public new ServerBootstrap SetConfig(IConnectionConfig config)        {            base.SetConfig(config);            return this;        }        public new ServerBootstrap SetDecoder(IMessageDecoder decoder)        {            base.SetDecoder(decoder);            return this;        }        public new ServerBootstrap SetEncoder(IMessageEncoder encoder)        {            base.SetEncoder(encoder);            return this;        }        public new ServerBootstrap SetAllocator(IByteBufAllocator allocator)        {            base.SetAllocator(allocator);            return this;        }        public new ServerBootstrap OnConnect(ConnectionEstablishedCallback connectionEstablishedCallback)        {            base.OnConnect(connectionEstablishedCallback);            return this;        }        public new ServerBootstrap OnDisconnect(ConnectionTerminatedCallback connectionTerminatedCallback)        {            base.OnDisconnect(connectionTerminatedCallback);            return this;        }        public new ServerBootstrap OnReceive(ReceivedDataCallback receivedDataCallback)        {            base.OnReceive(receivedDataCallback);            return this;        }        public new ServerBootstrap OnError(ExceptionCallback exceptionCallback)        {            base.OnError(exceptionCallback);            return this;        }        public new ServerBootstrap SetOption(string optionKey, object optionValue)        {            base.SetOption(optionKey, optionValue);            return this;        }}
鏈式編程

我們調用最后,肯定是使用build方法,而build方法實際上調用的是BuildInternal內部方法,而該這又是一個工廠模式(和后滿ServerFactory組成抽象工廠??),會返回TcpServerFactory或者UdpServerFactory。

TcpServerFactory和UdpServerFactory

這倆個類其實沒有什么核心代碼,但是你網上追溯父類的時候你會發現TcpServerFactory(UdpServerFactory)=>ServerFactoryBase => ServerBootstrap。它們依舊是ServerBootstrap對象。不過不同的地方就是,他們除了爹還有了一個媽媽ServerFactoryBase =>IServerFactory =>IConnectionFactory。

我們看下ServerFactoryBase 源碼

    public abstract class ServerFactoryBase : ServerBootstrap, IServerFactory    {        protected ServerFactoryBase(ServerBootstrap other)            : base(other)        {        }        protected abstract ReactorBase NewReactorInternal(INode listenAddress);        public IReactor NewReactor(INode listenAddress)        {            var reactor = NewReactorInternal(listenAddress);            reactor.Configure(Config);            if (ReceivedData != null)                reactor.OnReceive += (ReceivedDataCallback)ReceivedData.Clone();            if (ConnectionEstablishedCallback != null)                reactor.OnConnection += (ConnectionEstablishedCallback)ConnectionEstablishedCallback.Clone();            if (ConnectionTerminatedCallback != null)                reactor.OnDisconnection += (ConnectionTerminatedCallback)ConnectionTerminatedCallback.Clone();            if (ExceptionCallback != null)                reactor.OnError += (ExceptionCallback) ExceptionCallback.Clone();            return reactor;        }        public IConnection NewConnection()        {            return NewConnection(Node.Any());        }        public IConnection NewConnection(INode localEndpoint)        {            var reactor = (ReactorBase)NewReactor(localEndpoint);            return reactor.ConnectionAdapter;        }        public IConnection NewConnection(INode localEndpoint, INode remoteEndpoint)        {            return NewConnection(localEndpoint);        }    }

 

發現它們母親(IConnectionFactory)要做的事都是通過IReactor來完成的。而它們(TcpServerFactory和UdpServerFactory)只是找到合適的IReactor對象而已,另一方面我們也可以看出真正負責網絡連接的就是IReactor對象。它就是保證底層通訊的邏輯。

    public sealed class TcpServerFactory : ServerFactoryBase    {        public TcpServerFactory(ServerBootstrap other)            : base(other)        {        }        protected override ReactorBase NewReactorInternal(INode listenAddress)        {            if (UseProxies)                return new TcpProxyReactor(listenAddress.Host, listenAddress.Port, EventLoop, Encoder, Decoder,                    Allocator, BufferBytes);            else                throw new NotImplementedException("Have not implemented non-TCP proxies");        }    }
TcpServerFactory
    public sealed class UdpServerFactory : ServerFactoryBase    {        public UdpServerFactory(ServerBootstrap other) : base(other)        {        }        protected override ReactorBase NewReactorInternal(INode listenAddress)        {            return new UdpProxyReactor(listenAddress.Host, listenAddress.Port, EventLoop, Encoder, Decoder, Allocator, BufferBytes);        }    }
UdpServerFactory

 IReactor們

 這里包含TcpServerFactory內部使用的TcpProxyReactor、UdpServerFactory使用的UdpProxyReactor,以及他們的基類ProxyReactorBase、ReactorBase。他們之間的關系為:

  • TcpProxyReactor => ProxyReactorBase => ReactorBase =>IReactor
  • UdpProxyReactor => ProxyReactorBase => ReactorBase =>IReactor
  • ReactorConnectionAdapter =>IConnection(適配器模式,內部封裝IReactor)

*嚴格說ReactorConnectionAdapter 不算是IReactor,它只是適配器模式,使得IReactor對象能夠和IConnection對象模式適配 

雖然類不是很多,但估計helios的高效可能核心就和這部分有關系。但是我不太了解通訊相關內容,只能從構建的方式大致的講下,有興趣的人可以自己深入研究。

  • ReactorBase :定義了基本操作、事件。對于接收,發送提供默認操作
  • ProxyReactorBase :增加了ReactorResponseChannel對象,重載接收方法(ReceivedData,調用的是ReactorResponseChannel的OnReceive)
  • TcpProxyReactor :重載StartInternal方法,使用TcpReactorResponseChannel進行數據接收
  • UdpProxyReactor :重載StartInternal方法,使用ReactorProxyResponseChannel進行數據接收

ReactorResponseChannel們

 此處包含三個類ReactorResponseChannel、TcpReactorResponseChannel、ReactorProxyResponseChannel。

  • ReactorResponseChannel 基類,定義基礎操作。主要是Send方法
  • TcpReactorResponseChannel,TCP協議下的ReactorResponseChannel實現。
  • ReactorProxyResponseChannel,ReactorResponseChannel的代理,實際上就是把ReactorResponseChannel虛方法變成空方法而已。

ReactorResponseChannel中OnReceive方法調用的是”NetworkEventLoop.Receive(data, this);“,將數據發送到EventLoop消息隊列)中。

EventLoop(消息隊列)

消息隊列一共有三個層次繼承,分別是:NetworkEventLoop、ThreadedEventLoop、AbstractEventLoop

繼承關系為:NetworkEventLoop=> ThreadedEventLoop=> AbstractEventLoop。

  • AbstractEventLoop:內部使用IFiber對象,進行消息處理。所有的處理方法最終走的都是IFiber對象(實際上IFiber中維護一個列表,之后由IFiber對象決定如何處理)
  • ThreadedEventLoop:構造函數構建自己的IFiber對象(默認使用的是:DedicatedThreadPoolFiber)
  • NetworkEventLoop:將網絡事件、數據接收事件用IFiber對象處理

IFiber們

 IFiber的作用不是處理接收的數據,而是在乎用什么樣的方式處理數據,比如起幾個線程,同步還是異步的處理。IFiber對象有好幾個,但是實際上真正用的只有1個(DedicatedThreadPoolFiber),但是不妨礙我們去看看這些對象。

  • DedicatedThreadPoolFiber使用hebios自己的線程池技術(DedicatedThreadPool),底層通過線程池來處理數據。
  • SynchronousFiber同步處理,當一個操作進入消息隊列的時候立即處理
  • ThreadPoolFiber使用線程池技術,底層通過線程池來處理數據
  • SharedFiber共享Fiber,當NetworkEventLoop.clone()的時候,只是簡單的將NetworkEventLoop的IFiber對象傳遞過來,以達到多個NetworkEventLoop共享IFiber的目的

 最后的處理類:BasicExecutor/TryCatchExecutor

在IFiber里面,我們會默認構造BasicExecutor對象(TryCatchExecutor繼承自BasicExecutor,可以catch住異常),這個類會最終處理服務器端的數據請求。

總結:服務器端處理數據的順序為:創建ServerBootstrap對象,構建出它的子類(IConnectionFactory),之后分別進行網絡通訊(IReactor),通訊管道(ReactorResponseChannel)對數據接收發送管理,之后數據進入消息隊列(EventLoop),服務器端決定處理數據的線程技術(IFiber),最終將數據處理(BasicExecutor

*這不是真實的接送邏輯,而是我們沿著源代碼求索邏輯的順序。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩成人在线免费观看| 亚洲综合在线小说| 欧美性一区二区三区| 91久久国产综合久久91精品网站| 欧美高清视频在线观看| 91超碰中文字幕久久精品| 日韩大陆毛片av| 97在线免费观看视频| 亚洲激情国产精品| 久久久999精品免费| 日韩精品极品毛片系列视频| 九九热视频这里只有精品| 91色琪琪电影亚洲精品久久| 亚洲精品狠狠操| 精品呦交小u女在线| 九九精品视频在线观看| 欧美人与性动交a欧美精品| 午夜精品理论片| 亚洲国产精品久久久久| 91国产精品91| 国产精品美女在线| 国产精品免费看久久久香蕉| 欧美黄色片视频| 日韩电影视频免费| 一本一道久久a久久精品逆3p| 日韩一区视频在线| 久久69精品久久久久久国产越南| 欧美一级电影免费在线观看| 91美女片黄在线观| 国产免费一区二区三区在线能观看| 国产在线精品播放| 国产精品久久一区主播| 国产精品一区二区久久| 国产精品久久久91| 亚洲精品www久久久| 一夜七次郎国产精品亚洲| 国产精品丝袜久久久久久高清| 这里只有精品丝袜| 日韩中文字幕在线播放| 亚洲性生活视频在线观看| 中日韩美女免费视频网站在线观看| 日韩视频欧美视频| 亚洲国产欧美日韩精品| 成人精品久久一区二区三区| 亚洲第一av在线| 精品无人国产偷自产在线| 精品动漫一区二区| 91久久国产婷婷一区二区| 91精品免费视频| 欧美精品一二区| 国产精品福利久久久| 欧美极品欧美精品欧美视频| 色琪琪综合男人的天堂aⅴ视频| 欧美精品videos性欧美| 日本成人精品在线| www.日韩免费| 国产精品久久久一区| 国产综合香蕉五月婷在线| 国产成+人+综合+亚洲欧洲| 日韩国产在线看| 国产视频丨精品|在线观看| 久久91精品国产| 欧美人与性动交a欧美精品| 91精品啪在线观看麻豆免费| 国产精品尤物福利片在线观看| 欧美亚洲另类在线| 国产精品夫妻激情| 亚洲精品国产精品自产a区红杏吧| 日韩欧美在线国产| 日韩精品久久久久| 亚洲美女免费精品视频在线观看| 疯狂做受xxxx高潮欧美日本| 一夜七次郎国产精品亚洲| 亚洲国产第一页| 欧美巨猛xxxx猛交黑人97人| 欧美极品少妇xxxxx| 国产欧美日韩视频| 91久久久久久久久久久久久| 久久精品久久久久久国产 免费| 欧美成人国产va精品日本一级| 亚洲国产成人在线播放| 久久久久久美女| 91sao在线观看国产| 亚洲国产精品999| 亚洲美女又黄又爽在线观看| 亚洲电影第1页| 国产欧美 在线欧美| 精品magnet| 久久久久久97| 国产精品精品一区二区三区午夜版| 日韩久久午夜影院| 亚洲理论片在线观看| 国产日韩欧美91| 欧美一级片免费在线| 在线观看亚洲区| 精品欧美一区二区三区| 国产精品青青在线观看爽香蕉| 亚洲女同精品视频| 成人欧美一区二区三区在线湿哒哒| 91丝袜美腿美女视频网站| 国产精品一区二区三区久久| 亚洲第一精品久久忘忧草社区| 狠狠躁夜夜躁人人爽天天天天97| 日韩视频在线免费| 日韩美女视频中文字幕| 成人伊人精品色xxxx视频| 亚洲va久久久噜噜噜| 国产精品日韩久久久久| 国产精品久久久久久久久久免费| 国产精品va在线播放我和闺蜜| 精品女同一区二区三区在线播放| 成人网在线免费看| 国产欧亚日韩视频| 色偷偷偷综合中文字幕;dd| 91网站免费看| 色综合久久天天综线观看| 欧美亚洲在线观看| 欧美日本中文字幕| 日韩精品在线免费播放| 一区二区国产精品视频| 一本色道久久88亚洲综合88| 久久伊人免费视频| 日韩电影在线观看中文字幕| 欧美日韩国产精品一区二区三区四区| 久久91精品国产91久久久| 国产欧美精品xxxx另类| 91精品国产高清久久久久久91| 亚洲精品99久久久久中文字幕| 2019中文字幕在线| 亚洲欧洲高清在线| 欧美午夜精品久久久久久浪潮| 亚洲最大av在线| 欧美日韩第一页| 国产精品美女www爽爽爽视频| 国产欧美精品xxxx另类| 免费不卡在线观看av| 国产一区二区视频在线观看| 日韩精品欧美激情| 日韩欧美高清视频| 亚洲在线一区二区| 在线看欧美日韩| 成人高清视频观看www| 日韩av在线一区二区| 国产精品美女午夜av| 欧美丰满少妇xxxxx| 亚洲人成免费电影| 国产www精品| 成人妇女淫片aaaa视频| 欧美极品美女电影一区| 日韩经典一区二区三区| 亚洲国产91精品在线观看| 国产精品美女999| 国产欧美精品一区二区三区-老狼| 精品magnet| 91在线色戒在线| 国产一区二区三区在线看| 欧美极品第一页| 国产精品一区二区电影| 97色在线视频观看| 在线精品91av| 日韩三级影视基地| 97精品免费视频| 欧美黄色三级网站| 狠狠色香婷婷久久亚洲精品|