/// <summary> /// Wait the desidered time and update DNS /// </summary> public void WaitAndUpdate() { UpdateStatus ret; timerWait tmrTimedUpdate = new timerWait(); timerWait tmrIpChangeDetect = new timerWait(); timerWait tmrIpChangeUpdate = new timerWait(); bool ipChanged = false; timerWait tmrRateLimiter = new timerWait(); ret = UpdateDns(AppSettings.user, AppSettings.password, AppSettings.hostname, AppSettings.updateLink); AppSettings.lastUpdateStatus = ret; AppSettings.lastUpdateStatusChanged = true; AddLog("Updating DNS: " + ret.ToString()); while (AppSettings.exitUpdateLoop == false) { if (Wait(ref tmrTimedUpdate, AppSettings.updateInterval * 60) == true)//default updateInterval is 60 min { ret = UpdateDns(AppSettings.user, AppSettings.password, AppSettings.hostname, AppSettings.updateLink); AppSettings.lastUpdateStatus = ret; AppSettings.lastUpdateStatusChanged = true; AddLog("Updating DNS: " + ret.ToString()); oldNetCardIdAndIp = null;//prevent next ip change check from trigger: if you boot from standby after long time and every timer is expired, probably you are also reconnecting, no need to update twice } if (AppSettings.checkAlsoLocalIpChange == true) { if (Wait(ref tmrIpChangeDetect, 30) == true)//every 30 second check for local ip change { ipChanged = IpIdChangedSinceLastCall(); tmrIpChangeUpdate.oldTime = DateTime.Now; //reset timer } if (Wait(ref tmrIpChangeUpdate, 15) == true && ipChanged == true && ipBasedUpdaterRateLimiter > 0) //after 15 second do the actual update { //if status changed and i have lives, update ddns. 30sec delay to ensure you are connected after network change ipChanged = false; //disable timer ipBasedUpdaterRateLimiter--; //use a life ret = UpdateDns(AppSettings.user, AppSettings.password, AppSettings.hostname, AppSettings.updateLink); AppSettings.lastUpdateStatus = ret; AppSettings.lastUpdateStatusChanged = true; tmrTimedUpdate.oldTime = DateTime.Now;//reset timer, we updated just now because of ip change, so reset timed update if (ipBasedUpdaterRateLimiter > 0) { AddLog("Ip changed, updating DNS: " + ret.ToString()); } else { AddLog("Ip changed (rate limited), updating DNS: " + ret.ToString()); } } if (Wait(ref tmrRateLimiter, 10 * 60) == true)//every 10 minutes add a "life" to rate limiter counter { if (ipBasedUpdaterRateLimiter < 3) { ipBasedUpdaterRateLimiter++;//this means that you have 3 fast(30 sec) updates and then you will be rate limited to 1 every 5 minutes, in case of slower update you eventually regain all 3 fast updates } } } System.Threading.Thread.Sleep(1000);//1 sec } }
/// <summary> /// PC Clock based wait /// </summary> /// <param name="t">structure that hold current and old time</param> /// <param name="seconds">seconds to wait</param> /// <returns></returns> bool Wait(ref timerWait t, uint seconds) { if (t.oldTime.ToBinary() == 0) { t.oldTime = DateTime.Now; //init } t.myTime = DateTime.Now; TimeSpan diff = t.myTime.Subtract(t.oldTime); if ((uint)diff.TotalSeconds < seconds) { return(false); //not yet } t.oldTime = t.myTime; //reset return(true); //done }