* Do not change the function _swap_endianness on the fly to prevent it from being used in a 2.1 lobby

- Change this because altering _swap_endianness on the fly isn't threadsafe
  - Instead revert to code duplication (for now)

git-svn-id: https://warzone2100.svn.sourceforge.net/svnroot/warzone2100/trunk@7218 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2009-04-27 22:36:39 +00:00 committed by Git SVN Gateway
parent a5aa693b95
commit 05d9661d7a
1 changed files with 39 additions and 16 deletions

View File

@ -235,24 +235,47 @@ class BinaryProtocol21(BinaryProtocol20):
lobbyPort = BaseProtocol.lobbyPort['2.1'] lobbyPort = BaseProtocol.lobbyPort['2.1']
def encodeSingle(self, game, out = str()): def encodeSingle(self, game, out = str()):
global _swap_endianness with writeable(out) as write:
_swap_endianness_back = _swap_endianness write.write(self.gameFormat.pack(
# Make _swap_endianness (temporarily) a NO-OP self._encodeName(game),
_swap_endianness = lambda num: num game.size or self.size, game.flags,
self._encodeHost(game),
maxPlayers, currentPlayers, game.user1, game.user2, game.user3, game.user4))
try: try:
return super(BinaryProtocol21, self).encodeSingle(game, out) return write.getvalue()
finally: except AttributeError:
_swap_endianness = _swap_endianness_back return out
def decodeSingle(self, input, game = Game(), offset = None): def decodeSingle(self, input, game = Game(), offset = None):
global _swap_endianness with readable(input) as read:
_swap_endianness_back = _swap_endianness decData = {}
# Make _swap_endianness (temporarily) a NO-OP
_swap_endianness = lambda num: num if offset != None and read == None:
try: def unpack():
return super(BinaryProtocol21, self).decodeSingle(input, game, offset) return self.gameFormat.unpack_from(out, offset)
finally: elif not read:
_swap_endianness = _swap_endianness_back def unpack():
return self.gameFormat.unpack(input)
else:
def unpack():
data = read.read(self.size)
if len(data) != self.size:
return None
return self.gameFormat.unpack(data)
data = unpack()
if not data:
return None
(decData['name'], game.size, game.flags, decData['host'], game.maxPlayers, game.currentPlayers,
game.user1, game.user2, game.user3, game.user4) = data
for strKey in ['name', 'host']:
decData[strKey] = decData[strKey].strip("\0")
game.data.update(decData)
return game
class BinaryProtocol22(BinaryProtocol21): class BinaryProtocol22(BinaryProtocol21):
# >= 2.2 data # >= 2.2 data