protected override void SetState(ConnectionTrigger connectionTrigger)
        {
            base.SetState(connectionTrigger);

            if (State == ConnectionState.LinkError)
            {
                BeginConnection();
            }
        }
示例#2
0
        protected override void SetState(ConnectionTrigger connectionTrigger)
        {
            base.SetState(connectionTrigger);

            if (State == ConnectionState.LinkError && _clientConnectionSettings.AutoReconnect)
            {
                BeginConnection();
            }
        }
        public async Task OnlyRequestAllowsTransitionFromInvitedToNegotiating(ConnectionTrigger trigger)
        {
            var record = new ConnectionRecord();

            Assert.Equal(ConnectionState.Invited, record.State);

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await record.TriggerAsync(trigger));

            Assert.Equal(ConnectionState.Invited, record.State);
        }
        public async Task CannotTransitionFromAbandonedToAnotherState(ConnectionTrigger trigger)
        {
            var record = new ConnectionRecord {
                State = ConnectionState.Abandoned
            };

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await record.TriggerAsync(trigger));

            Assert.Equal(ConnectionState.Abandoned, record.State);
        }
        public async Task OnlyRespondAllowsTransitionFromNegotiatingToConnected(ConnectionTrigger trigger)
        {
            var record = new ConnectionRecord()
            {
                State = ConnectionState.Negotiating
            };

            Assert.Equal(ConnectionState.Negotiating, record.State);

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await record.TriggerAsync(trigger));

            Assert.Equal(ConnectionState.Negotiating, record.State);
        }
示例#6
0
        protected virtual void SetState(ConnectionTrigger connectionTrigger)
        {
            var currentState = State;
            var newState     = State;

            System.Diagnostics.Debug.WriteLine($"{GetType()} SetState({currentState}) -> {connectionTrigger}");

            switch (currentState)
            {
            case ConnectionState.Connecting:
            {
                if (connectionTrigger == ConnectionTrigger.Disconnect)
                {
                    _connectCancellationTokenSource?.Cancel();
                    OnDisconnect();
                    newState = ConnectionState.Disconnected;
                }
                else if (connectionTrigger == ConnectionTrigger.LinkError)
                {
                    _connectCancellationTokenSource?.Cancel();
                    OnDisconnect();
                    newState = ConnectionState.LinkError;
                }
                else if (connectionTrigger == ConnectionTrigger.Connected)
                {
                    newState = ConnectionState.Connected;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            break;

            case ConnectionState.Disconnected:
            {
                if (connectionTrigger == ConnectionTrigger.Connect)
                {
                    BeginConnection();
                    newState = ConnectionState.Connecting;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            break;

            case ConnectionState.Connected:
            {
                if (connectionTrigger == ConnectionTrigger.LinkError)
                {
                    _receiveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveResetEvent?.Set();
                    OnDisconnect();
                    newState = ConnectionState.LinkError;
                }
                else if (connectionTrigger == ConnectionTrigger.Disconnect)
                {
                    _connectCancellationTokenSource?.Cancel();
                    _receiveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveResetEvent?.Set();
                    OnDisconnect();
                    newState = ConnectionState.Disconnected;
                }
            }
            break;

            case ConnectionState.LinkError:
            {
                if (connectionTrigger == ConnectionTrigger.Disconnect)
                {
                    _connectCancellationTokenSource?.Cancel();
                    _receiveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveLoopCancellationTokenSource?.Cancel();
                    _sendKeepAliveResetEvent?.Set();
                    newState = ConnectionState.Disconnected;
                }
                else if (connectionTrigger == ConnectionTrigger.Connected)
                {
                    newState = ConnectionState.Connected;
                }
            }
            break;
            }

            if (newState == currentState)
            {
                return;
            }

            State = newState;

            System.Diagnostics.Debug.WriteLine($"{GetType()} SetState({currentState}) -> {connectionTrigger} -> {newState}");

            _connectionStateChangedAction?.Invoke(ServiceRef.Create <IConnection>(this), currentState, newState);
        }