Package lacewing :: Package packetloaders :: Module handshake
[frames] | no frames]

Source Code for Module lacewing.packetloaders.handshake

  1  """ 
  2  Initial connection request and reply. 
  3  """ 
  4   
  5  from struct import unpack_from, pack 
  6   
  7  from lacewing.constants import IMPLEMENTATION_TYPES 
  8  from lacewing.baseloader import _BaseLoader 
  9  from lacewing.bitdict import BitDict 
 10   
11 -class Welcome(_BaseLoader):
12 """ 13 This is a reply to the client connection request. 14 It tells the client what settings are set at the server. 15 16 Use the methods to manipulate properties (if available). 17 18 @ivar flags: The settings specified by the server 19 @type flags: BitDict 20 """ 21 flags = None 22 message = None 23 serverType = None 24
25 - def initialize(self):
26 self.flags = BitDict( 27 'UDP', 28 None, # reserved 29 None, # reserved 30 'Ping', 31 'ChannelListing', 32 'Extensions', 33 default = False 34 )
35
36 - def getServerType(self):
37 """ 38 Return the server type as a string. 39 @return: See L{IMPLEMENTATION_TYPES}. 40 """ 41 return IMPLEMENTATION_TYPES[self.serverType]
42
43 - def setServerType(self, serverType):
44 """ 45 Set the type of this server from string. 46 @param serverType: Server type as string, see L{IMPLEMENTATION_TYPES}. 47 """ 48 print serverType 49 self.serverType = IMPLEMENTATION_TYPES.index(serverType)
50
51 - def read(self, data):
52 flags, serverType = unpack_from('<IB', data) 53 self.flags.setFlags(flags) 54 self.serverType = serverType 55 self.message = data[5:]
56
57 - def generate(self):
58 return pack('<IB', self.flags.getFlags(), self.serverType 59 ) + self.message
60
61 -class Hello(_BaseLoader):
62 """ 63 This is the initial connection request sent 64 by the client. If it does not send this request within 65 reasonable time, it is disconnected. 66 67 Use the methods to manipulate properties (if available). 68 69 @ivar flags: Settings specified the client. It might, for example, 70 not want to get messaged privately. 71 72 @ivar protocolVersion: This is the protocol version that 73 the client can communicate with. The server might 74 want to disconnect the client if it does not support this 75 version. 76 """ 77 flags = None 78 protocolVersion = None 79 clientType = None 80 extensions = None 81
82 - def initialize(self):
83 self.flags = BitDict( 84 None, 85 None, 86 None, 87 'Extensions' 88 ) 89 self.extensions = []
90
91 - def read(self, data):
92 (flags, 93 self.protocolVersion, 94 self.clientType) = unpack_from('<IBB', data) 95 self.flags.setFlags(flags) 96 self.extensions = data[6:].split('\x00')
97
98 - def getClientType(self):
99 """ 100 Return the client type as a string. 101 @return: See L{IMPLEMENTATION_TYPES}. 102 """ 103 return IMPLEMENTATION_TYPES[self.clientType]
104
105 - def setClientType(self, clientType):
106 """ 107 Set the type of this client from string. 108 @param clientType: Client type as string, see L{IMPLEMENTATION_TYPES}. 109 """ 110 self.clientType = IMPLEMENTATION_TYPES.index(clientType)
111
112 - def generate(self):
113 # because Jamie is such a sex god (no question) and made 114 # the client flags in the first place, we're sending 115 # compatibility flags here 116 compatibleFlags = 0b1111 117 return pack('<IBB', compatibleFlags, self.protocolVersion, 118 self.clientType) + '\x00'.join(self.extensions or ())
119 120 __all__ = ['Hello', 'Welcome'] 121