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,42 +188,47 @@ class token:
:param host: host osuToken object :param host: host osuToken object
""" """
# Stop spectating old client try:
self.stopSpectating() self._spectLock.acquire()
# Set new spectator host # Stop spectating old client
self.spectating = host.token self.stopSpectating()
self.spectatingUserID = host.userID
# Add us to host's spectator list # Set new spectator host
host.spectators.append(self.token) self.spectating = host.token
self.spectatingUserID = host.userID
# Create and join spectator stream # Add us to host's spectator list
streamName = "spect/{}".format(host.userID) host.spectators.append(self.token)
glob.streams.add(streamName)
self.joinStream(streamName)
host.joinStream(streamName)
# Send spectator join packet to host # Create and join spectator stream
host.enqueue(serverPackets.addSpectator(self.userID)) streamName = "spect/{}".format(host.userID)
glob.streams.add(streamName)
self.joinStream(streamName)
host.joinStream(streamName)
# Create and join #spectator (#spect_userid) channel # Send spectator join packet to host
glob.channels.addTempChannel("#spect_{}".format(host.userID)) host.enqueue(serverPackets.addSpectator(self.userID))
chat.joinChannel(token=self, channel="#spect_{}".format(host.userID))
if len(host.spectators) == 1:
# First spectator, send #spectator join to host too
chat.joinChannel(token=host, channel="#spect_{}".format(host.userID))
# Send fellow spectator join to all clients # Create and join #spectator (#spect_userid) channel
glob.streams.broadcast(streamName, serverPackets.fellowSpectatorJoined(self.userID)) glob.channels.addTempChannel("#spect_{}".format(host.userID))
chat.joinChannel(token=self, channel="#spect_{}".format(host.userID))
if len(host.spectators) == 1:
# First spectator, send #spectator join to host too
chat.joinChannel(token=host, channel="#spect_{}".format(host.userID))
# Get current spectators list # Send fellow spectator join to all clients
for i in host.spectators: glob.streams.broadcast(streamName, serverPackets.fellowSpectatorJoined(self.userID))
if i != self.token and i in glob.tokens.tokens:
self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID))
# Log # Get current spectators list
log.info("{} is spectating {}".format(self.username, host.username)) for i in host.spectators:
if i != self.token and i in glob.tokens.tokens:
self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID))
# Log
log.info("{} is spectating {}".format(self.username, host.username))
finally:
self._spectLock.release()
def stopSpectating(self): def stopSpectating(self):
""" """
@ -231,43 +237,48 @@ class token:
:return: :return:
""" """
# Remove our userID from host's spectators try:
if self.spectating is None: self._spectLock.acquire()
return
if self.spectating in glob.tokens.tokens:
hostToken = glob.tokens.tokens[self.spectating]
else:
hostToken = None
streamName = "spect/{}".format(self.spectatingUserID)
# Remove us from host's spectators list, # Remove our userID from host's spectators
# leave spectator stream if self.spectating is None or self.spectatingUserID <= 0:
# and end the spectator left packet to host return
self.leaveStream(streamName) if self.spectating in glob.tokens.tokens:
if hostToken is not None: hostToken = glob.tokens.tokens[self.spectating]
hostToken.spectators.remove(self.token) else:
hostToken.enqueue(serverPackets.removeSpectator(self.userID)) hostToken = None
streamName = "spect/{}".format(self.spectatingUserID)
# and to all other spectators # Remove us from host's spectators list,
for i in hostToken.spectators: # leave spectator stream
if i in glob.tokens.tokens: # and end the spectator left packet to host
glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID)) self.leaveStream(streamName)
if hostToken is not None:
hostToken.spectators.remove(self.token)
hostToken.enqueue(serverPackets.removeSpectator(self.userID))
# If nobody is spectating the host anymore, close #spectator channel # and to all other spectators
# and remove host from spect stream too for i in hostToken.spectators:
if len(hostToken.spectators) == 0: if i in glob.tokens.tokens:
chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True) glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID))
hostToken.leaveStream(streamName)
# Console output # If nobody is spectating the host anymore, close #spectator channel
log.info("{} is no longer spectating {}. Current spectators: {}".format(self.username, self.spectatingUserID, hostToken.spectators)) # and remove host from spect stream too
if len(hostToken.spectators) == 0:
chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True)
hostToken.leaveStream(streamName)
# Part #spectator channel # Console output
chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True) log.info("{} is no longer spectating {}. Current spectators: {}".format(self.username, self.spectatingUserID, hostToken.spectators))
# Set our spectating user to 0 # Part #spectator channel
self.spectating = None chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True)
self.spectatingUserID = 0
# Set our spectating user to 0
self.spectating = None
self.spectatingUserID = 0
finally:
self._spectLock.release()
def updatePingTime(self): def updatePingTime(self):
""" """