From 05d9661d7a94caa3c2f8f31868ef6b9dcc3eafca Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 27 Apr 2009 22:36:39 +0000 Subject: [PATCH] * 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 --- tools/masterserver/protocol.py | 55 ++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/tools/masterserver/protocol.py b/tools/masterserver/protocol.py index 5afc47198..3d22f164c 100755 --- a/tools/masterserver/protocol.py +++ b/tools/masterserver/protocol.py @@ -235,24 +235,47 @@ class BinaryProtocol21(BinaryProtocol20): lobbyPort = BaseProtocol.lobbyPort['2.1'] def encodeSingle(self, game, out = str()): - global _swap_endianness - _swap_endianness_back = _swap_endianness - # Make _swap_endianness (temporarily) a NO-OP - _swap_endianness = lambda num: num - try: - return super(BinaryProtocol21, self).encodeSingle(game, out) - finally: - _swap_endianness = _swap_endianness_back + with writeable(out) as write: + write.write(self.gameFormat.pack( + self._encodeName(game), + game.size or self.size, game.flags, + self._encodeHost(game), + maxPlayers, currentPlayers, game.user1, game.user2, game.user3, game.user4)) + + try: + return write.getvalue() + except AttributeError: + return out def decodeSingle(self, input, game = Game(), offset = None): - global _swap_endianness - _swap_endianness_back = _swap_endianness - # Make _swap_endianness (temporarily) a NO-OP - _swap_endianness = lambda num: num - try: - return super(BinaryProtocol21, self).decodeSingle(input, game, offset) - finally: - _swap_endianness = _swap_endianness_back + with readable(input) as read: + decData = {} + + if offset != None and read == None: + def unpack(): + return self.gameFormat.unpack_from(out, offset) + elif not read: + 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): # >= 2.2 data