From 471fe60108f1b3fa72b7cb8439884f0df57b475e Mon Sep 17 00:00:00 2001 From: Uriziel Date: Sun, 13 Oct 2013 23:32:17 +0200 Subject: [PATCH] Added ping service --- syncplay/protocols.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/syncplay/protocols.py b/syncplay/protocols.py index 3f058d9..f6b01d5 100644 --- a/syncplay/protocols.py +++ b/syncplay/protocols.py @@ -402,3 +402,32 @@ class SyncServerProtocol(JSONCommandProtocol): def sendError(self, message): self.sendMessage({"Error": {"message": message}}) + +class PingService(object): + + def __init__(self): + self._rtt = 0 + self._t0 = None + self._fdDiff = 0 + self._fd = 0 + + def newTimestamp(self): + return time.time() + + def receiveMessage(self, timestamp, senderRtt): + prevRtt = self._rtt + self._rtt = time.time() - timestamp + if(self._t0 == None): + self._t0 = self._rtt / 2 + return + if(senderRtt <= 0): + return + self._fdDiff = self._fdDiff + (prevRtt - senderRtt) + self._fd = self._t0 - self._fdDiff + + def getLastForwardDelay(self): + return self._fd + + def getRtt(self): + return self._rtt +