Exemple #1
0
        public static PingWorker StartNewWorker(AsyncRWLock commLock, CommHelperBase sharedCommHelper, LCGen sharedReqLCG, LCGen sharedAnsLCG, int pingInterval, Action <Exception> pingThreadFailedCallback)
        {
            var result = new PingWorker(commLock, sharedCommHelper, sharedReqLCG, sharedAnsLCG, pingInterval, pingThreadFailedCallback);

            result.pingThread.Start();
            return(result);
        }
Exemple #2
0
 private void Worker(AsyncRWLock commLock, CommHelperBase commHelper, LCGen reqLCG, LCGen ansLCG, int pingInterval, CancellationToken token, Action <Exception> pingThreadFailedCallback)
 {
     using (var taskRunner = new AsyncRunner())
     {
         var nullBuff = new byte[0];
         while (!token.IsCancellationRequested)
         {
             commLock.EnterWriteLock();
             try
             {
                 Answer answer = Answer.Invalid;
                 taskRunner.AddTask(() => commHelper.SendRequest(ReqType.Ping, reqLCG.GenerateValue(), nullBuff, 0, 0));
                 taskRunner.AddTask(commHelper.ReceiveAnswer, (obj) => answer = obj);
                 taskRunner.RunPendingTasks();
                 if (answer.ansType != AnsType.Pong || answer.seq != ansLCG.GenerateValue())
                 {
                     throw new Exception();
                 }
             }
             catch (Exception)
             {
                 //try to resync
                 try { taskRunner.ExecuteTask(commHelper.Resync); }
                 catch (Exception ex) { pingThreadFailedCallback?.Invoke(ex); return; }
             }
             finally
             {
                 commLock.ExitWriteLock();
             }
             try { taskRunner.ExecuteTask(() => Task.Delay(pingInterval, token)); }
             catch (TaskCanceledException) { return; }
             catch (Exception ex) { pingThreadFailedCallback?.Invoke(ex); return; }
         }
     }
 }
Exemple #3
0
 private PingWorker(AsyncRWLock commLock, CommHelperBase sharedCommHelper, LCGen sharedReqLCG, LCGen sharedAnsLCG, int pingInterval, Action <Exception> pingThreadFailedCallback)
 {
     this.CTS        = new CancellationTokenSource();
     this.pingThread = new Thread(() => Worker(commLock, sharedCommHelper, sharedReqLCG, sharedAnsLCG, pingInterval, CTS.Token, pingThreadFailedCallback));
 }