Remove extra locks
This commit is contained in:
		| @@ -88,8 +88,6 @@ 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._spectatorLock = threading.Lock()	# Acquired while starting/stopping spectating |  | ||||||
| 		self._multiplayerLock = threading.Lock()# Acquired while joining/leaving streams |  | ||||||
|  |  | ||||||
| 		# Set stats | 		# Set stats | ||||||
| 		self.updateCachedStats() | 		self.updateCachedStats() | ||||||
| @@ -189,48 +187,42 @@ class token: | |||||||
|  |  | ||||||
| 		:param host: host osuToken object | 		:param host: host osuToken object | ||||||
| 		""" | 		""" | ||||||
| 		try: | 		# Stop spectating old client | ||||||
| 			# Stop spectating old client | 		self.stopSpectating() | ||||||
| 			self.stopSpectating() |  | ||||||
|  |  | ||||||
| 			# Acquire token's lock | 		# Set new spectator host | ||||||
| 			self._spectatorLock.acquire() | 		self.spectating = host.token | ||||||
|  | 		self.spectatingUserID = host.userID | ||||||
|  |  | ||||||
| 			# Set new spectator host | 		# Add us to host's spectator list | ||||||
| 			self.spectating = host.token | 		host.spectators.append(self.token) | ||||||
| 			self.spectatingUserID = host.userID |  | ||||||
|  |  | ||||||
| 			# Add us to host's spectator list | 		# Create and join spectator stream | ||||||
| 			host.spectators.append(self.token) | 		streamName = "spect/{}".format(host.userID) | ||||||
|  | 		glob.streams.add(streamName) | ||||||
|  | 		self.joinStream(streamName) | ||||||
|  | 		host.joinStream(streamName) | ||||||
|  |  | ||||||
| 			# Create and join spectator stream | 		# Send spectator join packet to host | ||||||
| 			streamName = "spect/{}".format(host.userID) | 		host.enqueue(serverPackets.addSpectator(self.userID)) | ||||||
| 			glob.streams.add(streamName) |  | ||||||
| 			self.joinStream(streamName) |  | ||||||
| 			host.joinStream(streamName) |  | ||||||
|  |  | ||||||
| 			# Send spectator join packet to host | 		# Create and join #spectator (#spect_userid) channel | ||||||
| 			host.enqueue(serverPackets.addSpectator(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)) | ||||||
|  |  | ||||||
| 			# Create and join #spectator (#spect_userid) channel | 		# Send fellow spectator join to all clients | ||||||
| 			glob.channels.addTempChannel("#spect_{}".format(host.userID)) | 		glob.streams.broadcast(streamName, serverPackets.fellowSpectatorJoined(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 | 		# Get current spectators list | ||||||
| 			glob.streams.broadcast(streamName, serverPackets.fellowSpectatorJoined(self.userID)) | 		for i in host.spectators: | ||||||
|  | 			if i != self.token and i in glob.tokens.tokens: | ||||||
|  | 				self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID)) | ||||||
|  |  | ||||||
| 			# Get current spectators list | 		# Log | ||||||
| 			for i in host.spectators: | 		log.info("{} is spectating {}".format(self.username, host.username)) | ||||||
| 				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._spectatorLock.release() |  | ||||||
|  |  | ||||||
| 	def stopSpectating(self): | 	def stopSpectating(self): | ||||||
| 		""" | 		""" | ||||||
| @@ -239,49 +231,43 @@ class token: | |||||||
|  |  | ||||||
| 		:return: | 		:return: | ||||||
| 		""" | 		""" | ||||||
| 		try: | 		# Remove our userID from host's spectators | ||||||
| 			# Acquire token lock | 		if self.spectating is None: | ||||||
| 			self._spectatorLock.acquire() | 			return | ||||||
|  | 		if self.spectating in glob.tokens.tokens: | ||||||
|  | 			hostToken = glob.tokens.tokens[self.spectating] | ||||||
|  | 		else: | ||||||
|  | 			hostToken = None | ||||||
|  | 		streamName = "spect/{}".format(self.spectatingUserID) | ||||||
|  |  | ||||||
| 			# Remove our userID from host's spectators | 		# Remove us from host's spectators list, | ||||||
| 			if self.spectating is None: | 		# leave spectator stream | ||||||
| 				return | 		# and end the spectator left packet to host | ||||||
| 			if self.spectating in glob.tokens.tokens: | 		self.leaveStream(streamName) | ||||||
| 				hostToken = glob.tokens.tokens[self.spectating] | 		if hostToken is not None: | ||||||
| 			else: | 			hostToken.spectators.remove(self.token) | ||||||
| 				hostToken = None | 			hostToken.enqueue(serverPackets.removeSpectator(self.userID)) | ||||||
| 			streamName = "spect/{}".format(self.spectatingUserID) |  | ||||||
|  |  | ||||||
| 			# Remove us from host's spectators list, | 			# and to all other spectators | ||||||
| 			# leave spectator stream | 			for i in hostToken.spectators: | ||||||
| 			# and end the spectator left packet to host | 				if i in glob.tokens.tokens: | ||||||
| 			self.leaveStream(streamName) | 					glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID)) | ||||||
| 			if hostToken is not None: |  | ||||||
| 				hostToken.spectators.remove(self.token) |  | ||||||
| 				hostToken.enqueue(serverPackets.removeSpectator(self.userID)) |  | ||||||
|  |  | ||||||
| 				# and to all other spectators | 			# If nobody is spectating the host anymore, close #spectator channel | ||||||
| 				for i in hostToken.spectators: | 			# and remove host from spect stream too | ||||||
| 					if i in glob.tokens.tokens: | 			if len(hostToken.spectators) == 0: | ||||||
| 						glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID)) | 				chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True) | ||||||
|  | 				hostToken.leaveStream(streamName) | ||||||
|  |  | ||||||
| 				# If nobody is spectating the host anymore, close #spectator channel | 			# Console output | ||||||
| 				# and remove host from spect stream too | 			log.info("{} is no longer spectating {}. Current spectators: {}".format(self.username, self.spectatingUserID, hostToken.spectators)) | ||||||
| 				if len(hostToken.spectators) == 0: |  | ||||||
| 					chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True) |  | ||||||
| 					hostToken.leaveStream(streamName) |  | ||||||
|  |  | ||||||
| 				# Console output | 		# Part #spectator channel | ||||||
| 				log.info("{} is no longer spectating {}. Current spectators: {}".format(self.username, self.spectatingUserID, hostToken.spectators)) | 		chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True) | ||||||
|  |  | ||||||
| 			# Part #spectator channel | 		# Set our spectating user to 0 | ||||||
| 			chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True) | 		self.spectating = None | ||||||
|  | 		self.spectatingUserID = 0 | ||||||
| 			# Set our spectating user to 0 |  | ||||||
| 			self.spectating = None |  | ||||||
| 			self.spectatingUserID = 0 |  | ||||||
| 		finally: |  | ||||||
| 			self._spectatorLock.release() |  | ||||||
|  |  | ||||||
| 	def updatePingTime(self): | 	def updatePingTime(self): | ||||||
| 		""" | 		""" | ||||||
| @@ -298,40 +284,35 @@ class token: | |||||||
| 		:param matchID: new match ID | 		:param matchID: new match ID | ||||||
| 		:return: | 		:return: | ||||||
| 		""" | 		""" | ||||||
| 		try: | 		# Make sure the match exists | ||||||
| 			self._multiplayerLock.acquire() | 		if matchID not in glob.matches.matches: | ||||||
|  | 			return | ||||||
|  |  | ||||||
| 			# Make sure the match exists | 		# Match exists, get object | ||||||
| 			if matchID not in glob.matches.matches: | 		match = glob.matches.matches[matchID] | ||||||
| 				return |  | ||||||
|  |  | ||||||
| 			# Match exists, get object | 		# Stop spectating | ||||||
| 			match = glob.matches.matches[matchID] | 		self.stopSpectating() | ||||||
|  |  | ||||||
| 			# Stop spectating | 		# Leave other matches | ||||||
| 			self.stopSpectating() | 		if self.matchID > -1 and self.matchID != matchID: | ||||||
|  | 			self.leaveMatch() | ||||||
|  |  | ||||||
| 			# Leave other matches | 		# Try to join match | ||||||
| 			if self.matchID > -1 and self.matchID != matchID: | 		joined = match.userJoin(self) | ||||||
| 				self.leaveMatch() | 		if not joined: | ||||||
|  | 			self.enqueue(serverPackets.matchJoinFail()) | ||||||
|  | 			return | ||||||
|  |  | ||||||
| 			# Try to join match | 		# Set matchID, join stream, channel and send packet | ||||||
| 			joined = match.userJoin(self) | 		self.matchID = matchID | ||||||
| 			if not joined: | 		self.joinStream(match.streamName) | ||||||
| 				self.enqueue(serverPackets.matchJoinFail()) | 		chat.joinChannel(token=self, channel="#multi_{}".format(self.matchID)) | ||||||
| 				return | 		self.enqueue(serverPackets.matchJoinSuccess(matchID)) | ||||||
|  |  | ||||||
| 			# Set matchID, join stream, channel and send packet | 		# Alert the user if we have just joined a tourney match | ||||||
| 			self.matchID = matchID | 		if match.isTourney: | ||||||
| 			self.joinStream(match.streamName) | 			self.enqueue(serverPackets.notification("You are now in a tournament match.")) | ||||||
| 			chat.joinChannel(token=self, channel="#multi_{}".format(self.matchID)) |  | ||||||
| 			self.enqueue(serverPackets.matchJoinSuccess(matchID)) |  | ||||||
|  |  | ||||||
| 			# Alert the user if we have just joined a tourney match |  | ||||||
| 			if match.isTourney: |  | ||||||
| 				self.enqueue(serverPackets.notification("You are now in a tournament match.")) |  | ||||||
| 		finally: |  | ||||||
| 			self._multiplayerLock.release() |  | ||||||
|  |  | ||||||
| 	def leaveMatch(self): | 	def leaveMatch(self): | ||||||
| 		""" | 		""" | ||||||
| @@ -339,33 +320,28 @@ class token: | |||||||
|  |  | ||||||
| 		:return: | 		:return: | ||||||
| 		""" | 		""" | ||||||
| 		try: | 		# Make sure we are in a match | ||||||
| 			self._multiplayerLock.acquire() | 		if self.matchID == -1: | ||||||
|  | 			return | ||||||
|  |  | ||||||
| 			# Make sure we are in a match | 		# Part #multiplayer channel and streams (/ and /playing) | ||||||
| 			if self.matchID == -1: | 		chat.partChannel(token=self, channel="#multi_{}".format(self.matchID), kick=True) | ||||||
| 				return | 		self.leaveStream("multi/{}".format(self.matchID)) | ||||||
|  | 		self.leaveStream("multi/{}/playing".format(self.matchID))	# optional | ||||||
|  |  | ||||||
| 			# Part #multiplayer channel and streams (/ and /playing) | 		# Set usertoken match to -1 | ||||||
| 			chat.partChannel(token=self, channel="#multi_{}".format(self.matchID), kick=True) | 		leavingMatchID = self.matchID | ||||||
| 			self.leaveStream("multi/{}".format(self.matchID)) | 		self.matchID = -1 | ||||||
| 			self.leaveStream("multi/{}/playing".format(self.matchID))	# optional |  | ||||||
|  |  | ||||||
| 			# Set usertoken match to -1 | 		# Make sure the match exists | ||||||
| 			leavingMatchID = self.matchID | 		if leavingMatchID not in glob.matches.matches: | ||||||
| 			self.matchID = -1 | 			return | ||||||
|  |  | ||||||
| 			# Make sure the match exists | 		# The match exists, get object | ||||||
| 			if leavingMatchID not in glob.matches.matches: | 		match = glob.matches.matches[leavingMatchID] | ||||||
| 				return |  | ||||||
|  |  | ||||||
| 			# The match exists, get object | 		# Set slot to free | ||||||
| 			match = glob.matches.matches[leavingMatchID] | 		match.userLeft(self) | ||||||
|  |  | ||||||
| 			# Set slot to free |  | ||||||
| 			match.userLeft(self) |  | ||||||
| 		finally: |  | ||||||
| 			self._multiplayerLock.release() |  | ||||||
|  |  | ||||||
| 	def kick(self, message="You have been kicked from the server. Please login again.", reason="kick"): | 	def kick(self, message="You have been kicked from the server. Please login again.", reason="kick"): | ||||||
| 		""" | 		""" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user