private void ScanThreadDoWork(object sender, DoWorkEventArgs e) { while (ConnectionManagerState != ConnectionManagerStates.Stop) { // Check if thread is being canceled var worker = sender as BackgroundWorker; if (worker != null && worker.CancellationPending) { ConnectionManagerState = ConnectionManagerStates.Stop; break; } // Switch between waiting, device scanning and watchdog switch (ConnectionManagerState) { case ConnectionManagerStates.Scan: DoWorkScan(); break; case ConnectionManagerStates.Watchdog: DoWorkWatchdog(); break; default: Thread.Sleep(1000); break; } } }
/// <summary> /// Start watchdog. Will check if connection gets interrupted /// </summary> public void StartWatchDog() { Log(1, "Starting Watchdog"); _lastCheckTime = TimeUtils.Millis; _nextTimeOutCheck = _lastCheckTime + WatchdogTimeOut; _watchdogTries = 0; ConnectionManagerState = ConnectionManagerStates.Watchdog; }
/// <summary> /// Start connection manager. Will set up thread, but will not start scanning /// </summary> public void StartConnectionManager() { ConnectionManagerState = ConnectionManagerStates.Wait; if (_scanThread.IsBusy != true) { // Start the asynchronous operation. _scanThread.RunWorkerAsync(); } }
/// <summary> /// Stop connection manager. /// </summary> public void StopConnectionManager() { ConnectionManagerState = ConnectionManagerStates.Stop; if (_scanThread.WorkerSupportsCancellation) { // Cancel the asynchronous operation. _scanThread.CancelAsync(); } _scanThread.DoWork -= ScanThreadDoWork; }
private void ConnectionWatchDog() { var lastLineTimeStamp = CmdMessenger.LastReceivedCommandTimeStamp; var currentTimeStamp = TimeUtils.Millis; // If timeout has not elapsed, wait till next watch time if (currentTimeStamp < _nextTimeOutCheck) { return; } // if a command has been received recently, set next check time if (lastLineTimeStamp > _lastCheckTime) { Log(3, "Successful watchdog response"); _lastCheckTime = currentTimeStamp; _nextTimeOutCheck = _lastCheckTime + WatchdogTimeOut; _watchdogTries = 0; return; } _lastCheckTime = currentTimeStamp; // Apparently, other side has not reacted in time // If too many tries, notify and stop if (_watchdogTries >= MaxWatchdogTries) { Log(3, "No watchdog response after final try"); _watchdogTries = 0; ConnectionManagerState = ConnectionManagerStates.Wait; InvokeEvent(ConnectionTimeout); } // We'll try another time // We queue the command in order to not be intrusive, but put it in front to get a quick answer CmdMessenger.SendCommand(new SendCommand(_challengeCommandId), SendQueue.InFrontQueue, ReceiveQueue.Default); _watchdogTries++; _lastCheckTime = currentTimeStamp; _nextTimeOutCheck = _lastCheckTime + WatchdogRetryTimeOut; Log(3, "No watchdog response, performing try #" + _watchdogTries); }
/// <summary> /// Start scanning for devices /// </summary> public void StartScan() { Log(1, "Starting device scan"); ConnectionManagerState = ConnectionManagerStates.Scan; }
/// <summary> /// Stop scanning for devices /// </summary> public void StopScan() { Log(1, "Stopping device scan"); ConnectionManagerState = ConnectionManagerStates.Wait; }
/// <summary> /// Stop watchdog. /// </summary> public void StopWatchDog() { Log(1, "Stopping Watchdog"); ConnectionManagerState = ConnectionManagerStates.Wait; }