Add simple client clock offset calculating logic
This commit is contained in:
parent
1b7a9984f9
commit
e25ba023ed
@ -69,7 +69,7 @@ class SyncClientProtocol(CommandProtocol):
|
|||||||
|
|
||||||
@arg_count(1)
|
@arg_count(1)
|
||||||
def handle_connected_ping(self, args):
|
def handle_connected_ping(self, args):
|
||||||
self.send_message('pong', args[0])
|
self.send_message('pong', args[0], int(time.time()*100000))
|
||||||
|
|
||||||
@arg_count(2)
|
@arg_count(2)
|
||||||
def handle_connected_playing(self, args):
|
def handle_connected_playing(self, args):
|
||||||
|
|||||||
@ -58,9 +58,17 @@ class SyncServerProtocol(CommandProtocol):
|
|||||||
|
|
||||||
self.factory.seek(self, counter, position)
|
self.factory.seek(self, counter, position)
|
||||||
|
|
||||||
@arg_count(1)
|
@arg_count(2)
|
||||||
def handle_connected_pong(self, args):
|
def handle_connected_pong(self, args):
|
||||||
self.factory.pong_received(self, args[0])
|
value, ctime = args
|
||||||
|
try:
|
||||||
|
ctime = int(ctime)
|
||||||
|
except ValueError:
|
||||||
|
self.drop_with_error('Invalid arguments')
|
||||||
|
|
||||||
|
ctime /= 100000.0
|
||||||
|
|
||||||
|
self.factory.pong_received(self, value, ctime)
|
||||||
|
|
||||||
@arg_count(1)
|
@arg_count(1)
|
||||||
def handle_connected_playing(self, args):
|
def handle_connected_playing(self, args):
|
||||||
@ -132,6 +140,7 @@ class WatcherInfo(object):
|
|||||||
self.last_update_sent = None
|
self.last_update_sent = None
|
||||||
|
|
||||||
self.ping = None
|
self.ping = None
|
||||||
|
self.time_offset = 0
|
||||||
self.last_ping_time = None
|
self.last_ping_time = None
|
||||||
self.last_ping_value = None
|
self.last_ping_value = None
|
||||||
|
|
||||||
@ -263,17 +272,16 @@ class SyncFactory(Factory):
|
|||||||
#min() arg is an empty sequence
|
#min() arg is an empty sequence
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
def pong_received(self, watcher_proto, value):
|
def pong_received(self, watcher_proto, value, ctime):
|
||||||
watcher = self.watchers.get(watcher_proto)
|
watcher = self.watchers.get(watcher_proto)
|
||||||
if not watcher:
|
if not watcher:
|
||||||
return
|
return
|
||||||
|
|
||||||
if watcher.last_ping_value == value:
|
if watcher.last_ping_value == value:
|
||||||
ping = (time.time() - watcher.last_ping_time)/2
|
ping = (time.time() - watcher.last_ping_time)/2
|
||||||
if watcher.ping is None:
|
if watcher.ping is None or watcher.ping > ping:
|
||||||
watcher.ping = ping
|
watcher.ping = ping
|
||||||
else:
|
watcher.time_offset = time.time() - (ctime + ping)
|
||||||
watcher.ping = watcher.ping*0.6 + ping*0.4
|
|
||||||
|
|
||||||
self.schedule_send_ping(watcher_proto)
|
self.schedule_send_ping(watcher_proto)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user