Пример #1
0
Файл: Ssh.cs Проект: 4vz/Aveezo
        private async Task Main()
        {
            IsReconnect = true;

            while (true)
            {
                try
                {
                    if (client != null)
                    {
                        client.Dispose();
                    }

                    await OnBeforeConnect();

                    BeforeConnect?.Invoke(this, new EventArgs());

                    client = new SshClient(Host, User, Password);

                    await OnConnecting();

                    Connecting?.Invoke(this, new EventArgs());

                    client.Connect();
                }
                catch (Exception ex)
                {
                    var connectionFailArgs = new SshConnectionFailEventArgs(ex.Message switch
                    {
                        "No such host is known" => SshConnectionFailReason.HostUnknown,
                        "A socket operation was attempted to an unreachable host." => SshConnectionFailReason.HostUnreachable,
                        string b when b.IndexOf("connected party did not properly") > -1 => SshConnectionFailReason.TimeOut,
                        "Permission denied (password)." => SshConnectionFailReason.AuthenticationFailed,
                        _ => SshConnectionFailReason.Unknown
                    }, ex.Message);
Пример #2
0
        private void _listener_Accepted(IChannelListener <TChannel> sender, TChannel channel)
        {
            channel.OnDisconnect += Channel_OnDisconnect;

            if (!channel.IsConnected)
            {
                return;
            }
            var connection = _connectionBuilder.UseChannel(channel).Build();

            var beforeConnectEventArgs = new BeforeConnectEventArgs <TContract, TChannel>(connection);

            BeforeConnect?.Invoke(this, beforeConnectEventArgs);
            if (!beforeConnectEventArgs.AllowConnection)
            {
                return;
            }

            channel.AllowReceive = true;
            _connections.TryAdd(channel, connection);
            AfterConnect?.Invoke(this, connection);
        }