/// <summary> /// Try to send a presentation packet for MAX_NETFAIL_ATTEMPTS time and /// execute the delaget NetworkCannotConnect if fails. /// </summary> private void SendPresentationPacket() { int attempts = 0; // attempts to send presentation // Tries to send the PresentationPacket for N times. If it fails, calls the relative callback while (!network.send(new PresentationPacket(Settings.Instance.LocalPeer))) { if (++attempts > MAX_NETFAIL_ATTEMPTS) { NetworkCannotConnect?.Invoke(); // failed N times: calling the delegate return; } System.Threading.Thread.Sleep(3000); } }
protected override void execute() { int attempts = 0; // a counter to take trace of consecutive attempts to send // I will continue while it will not be telled me to stop and eventually I will // stuck on the visibiliyChange event if someone calls Reset() on it. while (!Stop && visibilityChange.WaitOne()) { if (Stop) { break; // quit } // will store the result of the send() operation bool sendresult = false; while (!sendresult) { sendresult = network.send(new KeepalivePacket(Settings.Instance.LocalPeer.Id, Settings.Instance.LocalPeer.Name, Settings.Instance.LocalPeer.Ipaddress)); if (sendresult) // if the send() failed we don't want to wait, we want to try again { WaitingEvent.WaitOne(Settings.Instance.HELLO_INTERVAL); attempts = 0; // reset attempts } else if (++attempts == MAX_SEND_ATTEMPTS) { CannotSend(); // TODO Do something return; } } } }