Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 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(_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);
            }
        }