Пример #1
0
        public async Task StartAsync <TChannelHandler>(TChannelHandler channelHandler,
                                                       Action <IChannel> configureProcess, string host = null, int?port = null)
            where TChannelHandler : IChannelHandler
        {
            var address  = IPAddress.Parse(host.NotEmptyOrDefault(_serverOptions.Host));
            var endPoint = new IPEndPoint(address, port.NotNullOrDefault(_serverOptions.Port));

            IEventLoopGroup bossGroup   = null;
            IEventLoopGroup workerGroup = null;

            try
            {
                X509Certificate2 tlsCertificate = null;
                if (_serverOptions.IsSsl)
                {
                    var credentials = SigningCredentials.GetSigningCredentials(_serverOptions.SigningCredentialsKey);
                    tlsCertificate = credentials.ResolveCertificate();
                }

                var channel = await WrapperFactory
                              .CreateTcpServer(_serverOptions.UseLibuv, out bossGroup, out workerGroup)
                              .Configure(bootstrap =>
                {
                    bootstrap
                    .Option(ChannelOption.SoBacklog, 100)
                    .Handler(new LoggingHandler(DotNettyLogLevel.INFO));
                })
                              .AddTelnetHandler(tlsCertificate, channelHandler)
                              .BindAsync(endPoint).ConfigureAwait();

                Logger.LogInformation($"Bind ip end point: {endPoint}");

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
            }
        }
Пример #2
0
        public async Task StartAsync <TChannelHandler>(TChannelHandler channelHandler,
                                                       Action <IChannel> configureProcess, string host = null, int?port = null)
            where TChannelHandler : IChannelHandler
        {
            var address  = IPAddress.Parse(host.NotEmptyOrDefault(_serverOptions.Host));
            var endPoint = new IPEndPoint(address, port.NotNullOrDefault(_serverOptions.Port));

            IEventLoopGroup bossGroup   = null;
            IEventLoopGroup workerGroup = null;

            try
            {
                X509Certificate2 tlsCertificate = null;
                if (_serverOptions.IsSsl)
                {
                    var credentials = SigningCredentials.GetSigningCredentials(_serverOptions.SigningCredentialsKey);
                    tlsCertificate = credentials.ResolveCertificate();
                }

                var channel = await WrapperFactory
                              .CreateTcpServer(_serverOptions.UseLibuv, out bossGroup, out workerGroup)
                              .AddWebSocketHandler(tlsCertificate, channelHandler)
                              .BindAsync(endPoint).ConfigureAwait();

                Logger.LogInformation("Open your web browser and navigate to "
                                      + $"{(_serverOptions.IsSsl ? "https" : "http")}"
                                      + $"://{channel.LocalAddress.ToString()}:{port}/");
                Logger.LogInformation("Listening on "
                                      + $"{(_serverOptions.IsSsl ? "wss" : "ws")}"
                                      + $"://{channel.LocalAddress.ToString()}:{port}/websocket");

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                workerGroup.ShutdownGracefullyAsync().Wait();
                bossGroup.ShutdownGracefullyAsync().Wait();
            }
        }