コード例 #1
0
ファイル: WebSocketClient.cs プロジェクト: librame/extensions
        public async Task StartAsync <TChannelHandler>(Func <Uri, TChannelHandler> channelHandlerFactory,
                                                       Action <IChannel> configureProcess, string host = null, int?port = null)
            where TChannelHandler : IChannelHandler
        {
            host = host.NotEmptyOrDefault(_clientOptions.Host);
            port = port.NotNullOrDefault(_clientOptions.Port);

            var builder = new UriBuilder
            {
                Scheme = _clientOptions.IsSsl ? "wss" : "ws",
                Host   = host,
                Port   = port.Value
            };

            if (!_clientOptions.VirtualPath.IsEmpty())
            {
                builder.Path = _clientOptions.VirtualPath;
            }

            IEventLoopGroup group = null;

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

                var address          = IPAddress.Parse(builder.Uri.Host);
                var endPoint         = new IPEndPoint(address, builder.Uri.Port);
                var webSocketHandler = channelHandlerFactory.Invoke(builder.Uri);

                var channel = await WrapperFactory
                              .CreateTcp(_clientOptions.UseLibuv, out group)
                              .AddWebSocketHandler(tlsCertificate, webSocketHandler)
                              .ConnectAsync(endPoint, _clientOptions.RetryCount).ConfigureAwait();

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

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                await group.ShutdownGracefullyAsync(_clientOptions.QuietPeriod,
                                                    _clientOptions.TimeOut).ConfigureAwait();
            }
        }
コード例 #2
0
ファイル: TelnetServer.cs プロジェクト: librame/extensions
        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());
            }
        }
コード例 #3
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();
            }
        }
コード例 #4
0
ファイル: FactorialClient.cs プロジェクト: librame/extensions
        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(_clientOptions.Host));
            var endPoint = new IPEndPoint(address, port.NotNullOrDefault(_clientOptions.Port));

            IEventLoopGroup group = null;

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

                var channel = await WrapperFactory
                              .CreateTcp(false, out group)
                              .AddFactorialHandler(tlsCertificate, channelHandler)
                              .ConnectAsync(endPoint, _clientOptions.RetryCount).ConfigureAwait();

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

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait(_clientOptions.TimeOut);
            }
        }