Remove _chatLock and _internalLock

This commit is contained in:
Giuseppe Guerra 2017-08-11 22:19:39 +02:00
parent 29359ad4fd
commit ce75f5ee99

View File

@ -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._internalLock = threading.Lock() # Acquired while performing internal operations on this token
self._chatLock = threading.Lock() # Acquired while performing chat operations
self._streamsLock = threading.Lock() # Acquired while joining/leaving streams self._streamsLock = threading.Lock() # Acquired while joining/leaving streams
self._spectatorLock = threading.Lock() # Acquired while starting/stopping spectating self._spectatorLock = threading.Lock() # Acquired while starting/stopping spectating
self._multiplayerLock = threading.Lock()# Acquired while joining/leaving streams self._multiplayerLock = threading.Lock()# Acquired while joining/leaving streams
@ -143,17 +141,13 @@ class token:
:raises: exceptions.userAlreadyInChannelException() :raises: exceptions.userAlreadyInChannelException()
exceptions.channelNoPermissionsException() exceptions.channelNoPermissionsException()
""" """
try: if channelObject.name in self.joinedChannels:
self._chatLock.acquire() raise exceptions.userAlreadyInChannelException()
if channelObject.name in self.joinedChannels: if channelObject.publicRead == False and self.admin == False:
raise exceptions.userAlreadyInChannelException() raise exceptions.channelNoPermissionsException()
if channelObject.publicRead == False and self.admin == False: self.joinedChannels.append(channelObject.name)
raise exceptions.channelNoPermissionsException() self.joinStream("chat/{}".format(channelObject.name))
self.joinedChannels.append(channelObject.name) self.enqueue(serverPackets.channelJoinSuccess(self.userID, channelObject.clientName))
self.joinStream("chat/{}".format(channelObject.name))
self.enqueue(serverPackets.channelJoinSuccess(self.userID, channelObject.clientName))
finally:
self._chatLock.release()
def partChannel(self, channelObject): def partChannel(self, channelObject):
""" """
@ -161,12 +155,8 @@ class token:
:param channelObject: channel object :param channelObject: channel object
""" """
try: self.joinedChannels.remove(channelObject.name)
self._chatLock.acquire() self.leaveStream("chat/{}".format(channelObject.name))
self.joinedChannels.remove(channelObject.name)
self.leaveStream("chat/{}".format(channelObject.name))
finally:
self._chatLock.release()
def setLocation(self, latitude, longitude): def setLocation(self, latitude, longitude):
""" """
@ -387,19 +377,14 @@ class token:
:param reason: Kick reason, used in logs. Default: "kick" :param reason: Kick reason, used in logs. Default: "kick"
:return: :return:
""" """
try: # Send packet to target
self._internalLock.acquire() log.info("{} has been disconnected. ({})".format(self.username, reason))
if message != "":
self.enqueue(serverPackets.notification(message))
self.enqueue(serverPackets.loginFailed())
# Send packet to target # Logout event
log.info("{} has been disconnected. ({})".format(self.username, reason)) logoutEvent.handle(self, deleteToken=self.irc)
if message != "":
self.enqueue(serverPackets.notification(message))
self.enqueue(serverPackets.loginFailed())
# Logout event
logoutEvent.handle(self, deleteToken=self.irc)
finally:
self._internalLock.release()
def silence(self, seconds = None, reason = "", author = 999): def silence(self, seconds = None, reason = "", author = 999):
""" """
@ -410,26 +395,21 @@ class token:
:param author: userID of who has silenced the user. Default: 999 (FokaBot) :param author: userID of who has silenced the user. Default: 999 (FokaBot)
:return: :return:
""" """
try: if seconds is None:
self._chatLock.acquire() # Get silence expire from db if needed
seconds = max(0, userUtils.getSilenceEnd(self.userID) - int(time.time()))
else:
# Silence in db and token
userUtils.silence(self.userID, seconds, reason, author)
if seconds is None: # Silence token
# Get silence expire from db if needed self.silenceEndTime = int(time.time()) + seconds
seconds = max(0, userUtils.getSilenceEnd(self.userID) - int(time.time()))
else:
# Silence in db and token
userUtils.silence(self.userID, seconds, reason, author)
# Silence token # Send silence packet to user
self.silenceEndTime = int(time.time()) + seconds self.enqueue(serverPackets.silenceEndTime(seconds))
# Send silence packet to user # Send silenced packet to everyone else
self.enqueue(serverPackets.silenceEndTime(seconds)) glob.streams.broadcast("main", serverPackets.userSilenced(self.userID))
# Send silenced packet to everyone else
glob.streams.broadcast("main", serverPackets.userSilenced(self.userID))
finally:
self._chatLock.release()
def spamProtection(self, increaseSpamRate = True): def spamProtection(self, increaseSpamRate = True):
""" """
@ -438,18 +418,13 @@ class token:
:param increaseSpamRate: set to True if the user has sent a new message. Default: True :param increaseSpamRate: set to True if the user has sent a new message. Default: True
:return: :return:
""" """
try: # Increase the spam rate if needed
self._chatLock.acquire() if increaseSpamRate:
self.spamRate += 1
# Increase the spam rate if needed # Silence the user if needed
if increaseSpamRate: if self.spamRate > 10:
self.spamRate += 1 self.silence(1800, "Spamming (auto spam protection)")
# Silence the user if needed
if self.spamRate > 10:
self.silence(1800, "Spamming (auto spam protection)")
finally:
self._chatLock.release()
def isSilenced(self): def isSilenced(self):
""" """
@ -474,22 +449,17 @@ class token:
:return: :return:
""" """
try: stats = userUtils.getUserStats(self.userID, self.gameMode)
self._internalLock.acquire() log.debug(str(stats))
if stats is None:
stats = userUtils.getUserStats(self.userID, self.gameMode) log.warning("Stats query returned None")
log.debug(str(stats)) return
if stats is None: self.rankedScore = stats["rankedScore"]
log.warning("Stats query returned None") self.accuracy = stats["accuracy"]/100
return self.playcount = stats["playcount"]
self.rankedScore = stats["rankedScore"] self.totalScore = stats["totalScore"]
self.accuracy = stats["accuracy"]/100 self.gameRank = stats["gameRank"]
self.playcount = stats["playcount"] self.pp = stats["pp"]
self.totalScore = stats["totalScore"]
self.gameRank = stats["gameRank"]
self.pp = stats["pp"]
finally:
self._internalLock.release()
def checkRestricted(self): def checkRestricted(self):
""" """