Add spect lock back

This commit is contained in:
Giuseppe Guerra at an airport 2017-08-12 21:29:24 +02:00
parent 8d97227965
commit 34e7a332e6
2 changed files with 77 additions and 81 deletions

View File

@ -1,30 +1,15 @@
from objects import glob from objects import glob
from constants import serverPackets from constants import serverPackets
from common.log import logUtils as log
def handle(userToken, packetData): def handle(userToken, packetData):
# get token data # get token data
userID = userToken.userID userID = userToken.userID
# Send spectator frames to every spectator # Send spectator frames to every spectator
glob.streams.broadcast("spect/{}".format(userID), serverPackets.spectatorFrames(packetData[7:])) streamName = "spect/{}".format(userID)
'''for i in userToken.spectators: glob.streams.broadcast(streamName, serverPackets.spectatorFrames(packetData[7:]))
# Send to every user but host log.debug("Broadcasting {}'s frames to {} clients".format(
if i != userID: userID,
try: len(glob.streams.streams[streamName].clients))
# Get spectator token object )
spectatorToken = glob.tokens.getTokenFromUserID(i)
# Make sure the token exists
if spectatorToken is None:
raise exceptions.stopSpectating
# Make sure this user is spectating us
if spectatorToken.spectating != userID:
raise exceptions.stopSpectating
# Everything seems fine, send spectator frames to this spectator
spectatorToken.enqueue(serverPackets.spectatorFrames(packetData[7:]))
except exceptions.stopSpectating:
# Remove this user from spectators
userToken.removeSpectator(i)
userToken.enqueue(serverPackets.removeSpectator(i))'''

View File

@ -88,6 +88,7 @@ class token:
# Locks # Locks
self.processingLock = threading.Lock() # Acquired while there's an incoming packet from this user self.processingLock = threading.Lock() # Acquired while there's an incoming packet from this user
self._bufferLock = threading.Lock() # Acquired while writing to packets buffer self._bufferLock = threading.Lock() # Acquired while writing to packets buffer
self._spectLock = threading.RLock()
# Set stats # Set stats
self.updateCachedStats() self.updateCachedStats()
@ -187,6 +188,9 @@ class token:
:param host: host osuToken object :param host: host osuToken object
""" """
try:
self._spectLock.acquire()
# Stop spectating old client # Stop spectating old client
self.stopSpectating() self.stopSpectating()
@ -223,6 +227,8 @@ class token:
# Log # Log
log.info("{} is spectating {}".format(self.username, host.username)) log.info("{} is spectating {}".format(self.username, host.username))
finally:
self._spectLock.release()
def stopSpectating(self): def stopSpectating(self):
""" """
@ -231,8 +237,11 @@ class token:
:return: :return:
""" """
try:
self._spectLock.acquire()
# Remove our userID from host's spectators # Remove our userID from host's spectators
if self.spectating is None: if self.spectating is None or self.spectatingUserID <= 0:
return return
if self.spectating in glob.tokens.tokens: if self.spectating in glob.tokens.tokens:
hostToken = glob.tokens.tokens[self.spectating] hostToken = glob.tokens.tokens[self.spectating]
@ -268,6 +277,8 @@ class token:
# Set our spectating user to 0 # Set our spectating user to 0
self.spectating = None self.spectating = None
self.spectatingUserID = 0 self.spectatingUserID = 0
finally:
self._spectLock.release()
def updatePingTime(self): def updatePingTime(self):
""" """