Beispiel #1
0
        public async Task Throws_When_Initial_Connect_Fails()
        {
            var connectParams = new ConnectParameters();

            // Make the initial connect fail.
            _transportConnectorMock.Setup(c => c.ConnectAsync(It.IsAny <CancellationToken>())).Throws <TimeoutException>();

            var rfbConnection = new RfbConnection(_protocolMock.Object, new NullLoggerFactory(), connectParams);

            // Start should throw.
            await Assert.ThrowsAsync <TimeoutException>(() => rfbConnection.StartAsync());

            // Connection should still be uninitialized
            Assert.Equal(ConnectionState.Uninitialized, rfbConnection.ConnectionState);
        }
Beispiel #2
0
        /// <inhertitdoc />
        public async Task <TInput> ProvideAuthenticationInputAsync <TInput>(RfbConnection connection, ISecurityType securityType, IAuthenticationInputRequest <TInput> request)
            where TInput : class, IAuthenticationInput
        {
            if (typeof(TInput) == typeof(PasswordAuthenticationInput))
            {
                string?password = await Dispatcher.UIThread.InvokeAsync(async() => await EnterPasswordInteraction.Handle(Unit.Default)).ConfigureAwait(false);

                // TODO: Implement canceling of authentication input requests instead of passing an empty password!
                if (password == null)
                {
                    password = string.Empty;
                }

                return((TInput)Convert.ChangeType(new PasswordAuthenticationInput(password), typeof(TInput)));
            }

            throw new InvalidOperationException("The authentication input request is not supported by the interactive authentication handler.");
        }
Beispiel #3
0
        private async Task ReconnectLoop(string host, int port, string?password, IEnumerable <SecurityType> securityTypes, MonitorSnippet?section, CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                try
                {
                    Connection = await RfbConnection.ConnectAsync(host, port, password, securityTypes, section, token);

                    Section = section;
                    await Connection.Start();
                }
                catch (OperationCanceledException) { }
                catch (Exception e)
                {
                    Debug.WriteLine($"ReconnectLoop caught {e.Message}\n{e.StackTrace}");
                }
                await Task.Delay(1000);
            }
        }
Beispiel #4
0
        public async Task Updates_ConnectionState_On_Connect_Reconnect_Close()
        {
            var connectParams = new ConnectParameters {
                ReconnectDelay       = TimeSpan.FromSeconds(1),
                MaxReconnectAttempts = 1
            };

            var rfbConnection = new RfbConnection(_protocolMock.Object, new NullLoggerFactory(), connectParams);

            Assert.Equal(ConnectionState.Uninitialized, rfbConnection.ConnectionState);

            // Start connection.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => rfbConnection.StartAsync());

            Assert.Equal(ConnectionState.Connected, rfbConnection.ConnectionState);

            // Receive and send loops should have been started.
            _messageReceiverMock.Verify(receiver => receiver.StartReceiveLoop());
            _messageSenderMock.Verify(sender => sender.StartSendLoop());

            // Status should update when connection is interrupted
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => {
                // Let's simulate a failure
                _messageReceiverMock.Raise(receiver => receiver.Failed += null, new BackgroundThreadFailedEventArgs(new Exception("Shit happens.")));

                // Wait a bit because the event handler might run on a different thread
                return(Task.Delay(500));
            });

            Assert.Equal(ConnectionState.Interrupted, rfbConnection.ConnectionState);

            // Reconnect should succeed after 1 second.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => Task.Delay(TimeSpan.FromSeconds(1.5)));

            Assert.Equal(ConnectionState.Connected, rfbConnection.ConnectionState);

            // Close connection.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => rfbConnection.CloseAsync());

            Assert.Equal(ConnectionState.Closed, rfbConnection.ConnectionState);
        }
Beispiel #5
0
 public async Task Attach(RfbConnection preEstablishedConnection, MonitorSnippet?section)
 {
     Section = section;
     PreEstablishedConnection = preEstablishedConnection;
     await PreEstablishedConnection.Attach(this);
 }
 internal RfbConnectionContext(RfbConnection connection)
 {
     Connection        = connection;
     ConnectionDetails = new ConnectionDetailsAccessor(connection);
 }
 internal ConnectionDetailsAccessor(RfbConnection connection)
 {
     _connection = connection;
 }