public async Task WhenTransportFails_ShouldTransitionToDisconnectedAndEmitErrorWithRetry()
        {
            _fakeTransportFactory.initialiseFakeTransport =
                transport => transport.OnConnectChangeStateToConnected = false; //this will keep it in connecting state

            ClientOptions options = null;
            var           client  = GetClientWithFakeTransport(opts =>
            {
                opts.AutoConnect = false;
                options          = opts;
            });

            client.Connect();

            await WaitForConnectingOrSuspended(client);

            ConnectionStateChange connectionArgs = null;

            client.Connection.InternalStateChanged += (sender, args) =>
            {
                connectionArgs = args;
                Done();
            };
            LastCreatedTransport.Listener.OnTransportEvent(TransportState.Closing, new Exception());

            WaitOne();
            connectionArgs.Current.Should().Be(ConnectionState.Disconnected);
            connectionArgs.RetryIn.Should().Be(options.DisconnectedRetryTimeout);
            connectionArgs.Reason.Should().NotBeNull();
        }
Esempio n. 2
0
 private void OnInternalStateChanged(object sender, ConnectionStateChange e)
 {
     if (e.Current != ConnectionState.Connected)
     {
         FinishRequest(default(TimeSpan), DefaultError);
     }
 }
Esempio n. 3
0
        public async Task WithAConnectionError_ShouldRaiseChangeStateEventWithError()
        {
            var client = GetClientWithFakeTransport();

            ConnectionStateChange stateChange = null;
            var awaiter = new TaskCompletionAwaiter();

            client.Connection.On(ConnectionEvent.Failed, state =>
            {
                stateChange = state;
                awaiter.Tick();
            });

            var expectedError = new ErrorInfo("fake error");

            client.FakeProtocolMessageReceived(
                new ProtocolMessage(ProtocolMessage.MessageAction.Error)
            {
                Error = expectedError
            });

            await awaiter.Task;

            stateChange.HasError.Should().BeTrue();
            stateChange.Reason.Should().Be(expectedError);

            // RTN14g, expect FAILED
            stateChange.Event.Should().Be(ConnectionEvent.Failed);
        }
Esempio n. 4
0
        public void Disconnect()
        {
            m_bDoDisconnect = true;

            ConnectionState = EnConnState.DISCONNECTING;
            ConnectionStateChange?.Invoke(this, null);

            return;
        }
Esempio n. 5
0
 private void conn_StateChanged(object sender, ConnectionStateChange e)
 {
     if (_awaitedStates.Contains(e.Current))
     {
         DefaultLogger.Debug($"[{_id}] Desired state was reached.");
         RemoveListener();
         _taskCompletionSource.SetResult(true);
     }
 }
Esempio n. 6
0
        public void Connect(String portName, UInt32 baudRate)
        {
            if (m_bConnected)
            {
                return;
            }

            try
            {
                m_serialPort.PortName = portName;
                m_serialPort.BaudRate = (Int32)baudRate;

                m_serialPort.DataBits  = 8;
                m_serialPort.StopBits  = System.IO.Ports.StopBits.One;
                m_serialPort.Parity    = System.IO.Ports.Parity.None;
                m_serialPort.Handshake = System.IO.Ports.Handshake.None;

                m_serialPort.ReadTimeout  = 1;
                m_serialPort.WriteTimeout = 100;

                m_serialPort.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Fehler");

                ConnectionState = EnConnState.FAILED_TO_CONNECTED;
                ConnectionStateChange?.Invoke(this, null);

                return;
            }

            ConnectionState = EnConnState.CONNECTED;
            ConnectionStateChange?.Invoke(this, null);

            m_bConnected    = true;
            m_bDoDisconnect = false;

            if (m_connectorThread != null)
            {
                if ((m_connectorThread.ThreadState == ThreadState.Unstarted) ||
                    (m_connectorThread.ThreadState == ThreadState.Stopped))
                {
                    m_connectorThread = null;
                }
            }

            m_connectorThread = new Thread(new ThreadStart(Connectorthread))
            {
                Name = "Connector_connectorthread"
            };
            m_connectorThread.Start();

            return;
        }
        public override void OnConnectionStateChange(bt.BluetoothGatt gatt, [GeneratedEnum] bt.GattStatus status, [GeneratedEnum] bt.ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);

            ConnectionStateChange?.Invoke(this, new ConnectionStateChangeEventArgs(gatt, status, newState));

            if (!_servicesDiscovered)
            {
                gatt.DiscoverServices();
            }
        }
Esempio n. 8
0
 private void connection_ConnectionStateChanged(object sender, ConnectionStateChange e)
 {
     outputBox.Items.Add(string.Format("Connection: {0}", e.Current));
     SendMessage(string.Format("Connection: {0}", e.Current));
 }
Esempio n. 9
0
        private void Connectorthread()
        {
            StringBuilder rx            = new StringBuilder(10000);
            Boolean       bRxInProgress = false;

            while (!m_bDoDisconnect)
            {
                Byte rxbyte;

                try
                {
                    rxbyte = (Byte)m_serialPort.ReadByte();
                }
                catch (TimeoutException)
                {
                    rxbyte = 0;
                }
                catch
                {
                    ConnectionState = EnConnState.BROKEN;
                    ConnectionStateChange?.Invoke(this, null);
                    break;
                }

                if (rxbyte > 0)
                {
                    if (!bRxInProgress)
                    {
                        if ((rxbyte == (Byte)':') || (rxbyte == (Byte)'\'') || (rxbyte == (Byte)'#'))
                        {
                            bRxInProgress = true;
                            rx.Clear();
                            rx.Append((Char)rxbyte);
                        }
                    }
                    else
                    {
                        if (rxbyte != 0x03)
                        {
                            rx.Append((Char)rxbyte);
                        }
                        else
                        {
                            bRxInProgress = false;

                            String msg = rx.ToString();

                            if (msg[0] == ':')
                            {
                                NewRespData?.Invoke(this, msg);

                                System.Threading.Monitor.Enter(m_txmsgs);
                                if (m_rxdebit > 0)
                                {
                                    --m_rxdebit;
                                }
                                System.Threading.Monitor.Exit(m_txmsgs);
                            }
                            else if (msg[0] == '\'')
                            {
                                NewDisplayData?.Invoke(this, msg);
                            }
                            else if (msg[0] == '#')
                            {
                                NewDAQData?.Invoke(this, msg);
                            }
                            else
                            {
                                NewCorruptData?.Invoke(this, msg);
                            }
                        }
                    }
                }

                String txmsg = null;

                System.Threading.Monitor.Enter(m_txmsgs);
                if (m_txmsgs.Count > 0)
                {
                    txmsg = m_txmsgs.Dequeue();
                }
                System.Threading.Monitor.Exit(m_txmsgs);

                if (txmsg != null)
                {
                    NewRespData?.Invoke(this, txmsg);

                    Byte[] tmp = Encoding.ASCII.GetBytes(txmsg);
                    Byte[] tx  = new Byte[tmp.Length + 1];
                    tmp.CopyTo(tx, 0);
                    tx[tx.Length - 1] = 10;

                    m_serialPort.Write(tx, 0, tx.Length);
                }
            }

            if (m_serialPort.IsOpen)
            {
                m_serialPort.Close();
            }

            m_bConnected = false;

            ConnectionState = EnConnState.DISCONNECTED;
            ConnectionStateChange?.Invoke(this, null);

            return;
        }