Package lacewing :: Package moo :: Package packetloaders :: Module common
[frames] | no frames]

Source Code for Module lacewing.moo.packetloaders.common

 1  # Copyright (c) 2011 Mathias Kaerlev. 
 2  # See LICENSE for details. 
 3   
 4  import struct 
 5  from struct import unpack_from, pack 
 6   
 7  from lacewing.baseloader import _BaseLoader 
 8  from lacewing.exceptions import OutOfData 
 9   
10 -def unpack_string(data, size, offset = 0):
11 newData = buffer(data, offset, size) 12 if len(newData) < size: 13 raise OutOfData 14 return str(newData)
15
16 -class _MooLoader(_BaseLoader):
17 settings = None
18 - def __init__(self, **settings):
19 self.settings = settings
20
21 - def load(self, data):
22 try: 23 return self._load(data) 24 except struct.error: 25 raise OutOfData
26
27 -class _ChannelMixin:
28 - def setChannel(self, channel):
29 if hasattr(self, 'channelId'): 30 self.channelId = channel.id 31 if hasattr(self, 'channelName'): 32 self.channelName = channel.name 33 if hasattr(self, 'masterId'): 34 self.masterId = channel.getMaster().id
35
36 -class _ConnectionMixin:
37 - def setConnection(self, connection):
38 if hasattr(self, 'playerName'): 39 self.playerName = connection.name 40 if hasattr(self, 'playerId'): 41 self.playerId = connection.id 42 if hasattr(self, 'playerIp'): 43 self.playerIp = connection.transport.getPeer().host
44