changed signed handling

This commit is contained in:
Dave Schuyler 2005-04-21 00:28:46 +00:00
parent c0c9cb499e
commit d058dcfaff
2 changed files with 24 additions and 16 deletions

View File

@ -23,8 +23,9 @@ NetworkTimePrecision = 100.0
# These values are derived from the above. # These values are derived from the above.
NetworkTimeMask = (1 << NetworkTimeBits) - 1 NetworkTimeMask = (1 << NetworkTimeBits) - 1
NetworkTimeSignedMask = NetworkTimeMask >> 1 # the max absolute value bits.
NetworkTimeTopBits = 32 - NetworkTimeBits NetworkTimeTopBits = 32 - NetworkTimeBits
MaxTimeDelta = (NetworkTimeMask / 2.0) / NetworkTimePrecision MaxTimeDelta = NetworkTimeSignedMask / NetworkTimePrecision
# This is the maximum number of seconds by which we expect our clock # This is the maximum number of seconds by which we expect our clock
# (or the server's clock) to drift over an hour. # (or the server's clock) to drift over an hour.
@ -65,7 +66,7 @@ class ClockDelta(DirectObject.DirectObject):
# uncertainty increases over time (due to relative clock # uncertainty increases over time (due to relative clock
# drift). # drift).
self.lastResync = 0.0 self.lastResync = 0.0
self.accept("resetClock", self.__resetClock) self.accept("resetClock", self.__resetClock)
def getDelta(self): def getDelta(self):
@ -76,10 +77,10 @@ class ClockDelta(DirectObject.DirectObject):
# as a number of seconds plus or minus. Returns None, # as a number of seconds plus or minus. Returns None,
# representing infinite uncertainty, if we have never received # representing infinite uncertainty, if we have never received
# a time measurement. # a time measurement.
if self.uncertainty == None: if self.uncertainty == None:
return None return None
now = self.globalClock.getRealTime() now = self.globalClock.getRealTime()
elapsed = now - self.lastResync elapsed = now - self.lastResync
return self.uncertainty + elapsed * ClockDriftPerSecond return self.uncertainty + elapsed * ClockDriftPerSecond
@ -88,7 +89,7 @@ class ClockDelta(DirectObject.DirectObject):
# Returns the local time at which we last resynchronized the # Returns the local time at which we last resynchronized the
# clock delta. # clock delta.
return self.lastResync return self.lastResync
def __resetClock(self, timeDelta): def __resetClock(self, timeDelta):
""" """
this is called when the global clock gets adjusted this is called when the global clock gets adjusted
@ -134,7 +135,7 @@ class ClockDelta(DirectObject.DirectObject):
value is negative if the test was not even considered (because value is negative if the test was not even considered (because
it happened too soon after another recent request). it happened too soon after another recent request).
""" """
now = self.globalClock.getRealTime() now = self.globalClock.getRealTime()
if now - self.lastResync < P2PResyncDelay: if now - self.lastResync < P2PResyncDelay:
# We can't process this request; it came in on the heels # We can't process this request; it came in on the heels
@ -143,7 +144,7 @@ class ClockDelta(DirectObject.DirectObject):
# request is meaningless. # request is meaningless.
assert(self.notify.debug("Ignoring request for resync from %s within %.3f s." % (avId, now - self.lastResync))) assert(self.notify.debug("Ignoring request for resync from %s within %.3f s." % (avId, now - self.lastResync)))
return -1 return -1
# The timestamp value will be a timestamp that we sent out # The timestamp value will be a timestamp that we sent out
# previously, echoed back to us. Therefore we can confidently # previously, echoed back to us. Therefore we can confidently
# convert it back into our local time, even though we suspect # convert it back into our local time, even though we suspect
@ -168,7 +169,7 @@ class ClockDelta(DirectObject.DirectObject):
self.notify.info("Got sync +/- %.3f s, elapsed %.3f s, from %s." % (uncertainty, elapsed, avId)) self.notify.info("Got sync +/- %.3f s, elapsed %.3f s, from %s." % (uncertainty, elapsed, avId))
delta -= elapsed / 2.0 delta -= elapsed / 2.0
uncertainty += elapsed / 2.0 uncertainty += elapsed / 2.0
gotSync = self.newDelta(local, delta, uncertainty, trustNew = 0) gotSync = self.newDelta(local, delta, uncertainty, trustNew = 0)
return gotSync return gotSync
@ -188,7 +189,7 @@ class ClockDelta(DirectObject.DirectObject):
# Our previous measurement was self.delta +/- oldUncertainty; # Our previous measurement was self.delta +/- oldUncertainty;
# our new measurement is newDelta +/- newUncertainty. Take # our new measurement is newDelta +/- newUncertainty. Take
# the intersection of both. # the intersection of both.
oldLow = self.delta - oldUncertainty oldLow = self.delta - oldUncertainty
oldHigh = self.delta + oldUncertainty oldHigh = self.delta + oldUncertainty
newLow = newDelta - newUncertainty newLow = newDelta - newUncertainty
@ -196,20 +197,20 @@ class ClockDelta(DirectObject.DirectObject):
low = max(oldLow, newLow) low = max(oldLow, newLow)
high = min(oldHigh, newHigh) high = min(oldHigh, newHigh)
# If there is no intersection, whoops! Either the old # If there is no intersection, whoops! Either the old
# measurement or the new measurement is completely wrong. # measurement or the new measurement is completely wrong.
if low > high: if low > high:
if not trustNew: if not trustNew:
self.notify.info('discarding new delta.') self.notify.info('discarding new delta.')
return 0 return 0
self.notify.info('discarding previous delta.') self.notify.info('discarding previous delta.')
else: else:
newDelta = (low + high) / 2.0 newDelta = (low + high) / 2.0
newUncertainty = (high - low) / 2.0 newUncertainty = (high - low) / 2.0
assert(self.notify.debug('intersection at %.3f s, +/- %.3f s.' % (newDelta, newUncertainty))) assert(self.notify.debug('intersection at %.3f s, +/- %.3f s.' % (newDelta, newUncertainty)))
self.delta = newDelta self.delta = newDelta
self.uncertainty = newUncertainty self.uncertainty = newUncertainty
self.lastResync = localTime self.lastResync = localTime
@ -245,9 +246,9 @@ class ClockDelta(DirectObject.DirectObject):
# Assume the bits is either 16 or 32. If it's 32, no need # Assume the bits is either 16 or 32. If it's 32, no need
# to sign-extend. 32 bits gives us about 227 days of # to sign-extend. 32 bits gives us about 227 days of
# continuous timestamp. # continuous timestamp.
diff = networkTime - ntime diff = networkTime - ntime
return now + float(diff) / ticksPerSec return now + float(diff) / ticksPerSec
def localToNetworkTime(self, localTime, bits = 16, def localToNetworkTime(self, localTime, bits = 16,
@ -310,6 +311,13 @@ class ClockDelta(DirectObject.DirectObject):
Preserves the lower NetworkTimeBits of the networkTime value, Preserves the lower NetworkTimeBits of the networkTime value,
and extends the sign bit all the way up. and extends the sign bit all the way up.
""" """
return ((networkTime & NetworkTimeMask) << NetworkTimeTopBits) >> NetworkTimeTopBits if networkTime < 0:
# flip the sign, mask it as if it were positive, flip the sign back:
r = (networkTime * -1 & NetworkTimeSignedMask) * -1
else:
r = networkTime & NetworkTimeSignedMask
assert -32768 <= r <= 32767
return r
globalClockDelta = ClockDelta() globalClockDelta = ClockDelta()

View File

@ -1098,7 +1098,7 @@ def randInt32(rng=random.random):
""" """
i = int(rng() * 0x7FFFFFFF) i = int(rng() * 0x7FFFFFFF)
if rng() < .5: if rng() < .5:
i += 0x80000000 i *= -1
return i return i
class Enum: class Enum: