/// <summary> /// Init Bootstrap /// </summary> private void InitBootstrap() { _bootstrap.Group(_group) .Channel <TcpSocketChannel>(); if (_clientOption.TcpNodelay) { _bootstrap.Option(ChannelOption.TcpNodelay, true); } if (_clientOption.SoKeepalive) { _bootstrap.Option(ChannelOption.SoKeepalive, true); } if (_clientOption.ConnectTimeout > 0) { _bootstrap.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(_clientOption.ConnectTimeout)); } X509Certificate2 cert = null; string targetHost = null; if (!string.IsNullOrEmpty(_clientOption.Certificate)) { cert = new X509Certificate2(_clientOption.Certificate, _clientOption.CertificatePassword); targetHost = cert.GetNameInfo(X509NameType.DnsName, false); } _bootstrap.Handler(new ActionChannelInitializer <IChannel>(c => { var pipeline = c.Pipeline; if (cert != null) { pipeline.AddLast("tls", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost))); } pipeline.AddLast(new LoggingHandler("CLT-CONN")); foreach (var kv in _handlerPipeline.BuildPipeline(false)) { pipeline.AddLast(kv.Key, kv.Value); } pipeline.AddLast(new TcpClientChannelHandlerAdapter <TMessage>(this)); })); }
public async Task StartAsync(CancellationToken cancellationToken) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.SustainedLowLatency; } //if (_options.UseLibuv) //{ // var dispatcher = new DispatcherEventLoopGroup(); // _bossGroup = dispatcher; // _workerGroup = new WorkerEventLoopGroup(dispatcher); //} //else { // 主的线程 _bossGroup = new MultithreadEventLoopGroup(1); // 工作线程,默认根据CPU计算 _workerGroup = new MultithreadEventLoopGroup(); } var bootstrap = new ServerBootstrap() .Group(_bossGroup, _workerGroup); //if (_options.UseLibuv) { //bootstrap.Channel<TcpServerChannel>(); } //else { bootstrap.Channel <TcpServerSocketChannel>(); } bootstrap.Option(ChannelOption.SoBacklog, _options.SoBacklog); //NOTE: 是否可以公开更多Netty的参数 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { bootstrap .Option(ChannelOption.SoReuseport, true) .ChildOption(ChannelOption.SoReuseaddr, true); } X509Certificate2 tlsCertificate = null; if (!string.IsNullOrEmpty(_options.Certificate)) { tlsCertificate = new X509Certificate2(_options.Certificate, _options.CertificatePassword); } _ = bootstrap.Handler(new LoggingHandler("LSTN")) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { var pipeline = channel.Pipeline; if (tlsCertificate != null) { pipeline.AddLast("tls", TlsHandler.Server(tlsCertificate)); } pipeline.AddLast(new LoggingHandler("CONN")); foreach (var kv in _handlerPipeline.BuildPipeline(true)) { pipeline.AddLast(kv.Key, kv.Value); } //业务处理Handler,即解码成功后如何处理消息的类 pipeline.AddLast(new TcpServerChannelHandlerAdapter <TMessage>(_socketService)); })); if (_options.BindType == AddressBindType.Any) { _channel = await bootstrap.BindAsync(_options.Port); } else if (_options.BindType == AddressBindType.InternalAddress) { var localPoint = IPUtility.GetLocalIntranetIP(); if (localPoint == null) { this._logger.LogWarning("there isn't an avaliable internal ip address,the service will be hosted at loopback address."); _channel = await bootstrap.BindAsync(IPAddress.Loopback, _options.Port); } else { //this._logger.LogInformation("TcpServerHost bind at {0}",localPoint); _channel = await bootstrap.BindAsync(localPoint, this._options.Port); } } else if (_options.BindType == AddressBindType.Loopback) { _channel = await bootstrap.BindAsync(IPAddress.Loopback, _options.Port); } else { _channel = await bootstrap.BindAsync(IPAddress.Parse(_options.SpecialAddress), _options.Port); } Console.Write(_options.StartupWords, _channel.LocalAddress); }