class MenuContent(PacketFields): FIELDS = [ ("Appetizers", ListFieldType(STRING)), ("Sandwiches", ListFieldType(STRING)), ("Salads_and_Soups", ListFieldType(STRING)), ("Desert", ListFieldType(STRING)) ]
class Order(PacketType): DEFINITION_IDENTIFIER = "lab1b.Ruofan.Order" DEFINITION_VERSION = "1.0" FIELDS = [("ID", UINT16), ("name", STRING), ("tableNumber", UINT16), ("ordered_content", ListFieldType(STRING)), ("quantity", ListFieldType(INT32))]
class MissingDish(PacketType): DEFINITION_IDENTIFIER = "lab1b.Ruofan.MissingDish" DEFINITION_VERSION = "1.0" FIELDS = [("ID", UINT16), ("name", STRING), ("message", STRING), ("tableNumber", UINT16), ("missing", ListFieldType(STRING)), ("unavailable", ListFieldType(STRING))]
class TestPacket1(PacketType): DEFINITION_IDENTIFIER = "packettype.basicunittest.TestPacket1" DEFINITION_VERSION = "1.0" class SubFields(PacketFields): FIELDS = [("subfield1", Uint({Bits: 16})), ("subfield2", Uint({Bits: 16}))] SubFieldsType = ComplexFieldType(SubFields) FIELDS = [("header", ComplexFieldType(SubFields)), ("field1", Uint({MaxValue: 1000})), ("field2", StringFieldType), ("listField", ListFieldType(Uint)), ("complexListField", ListFieldType(SubFieldsType)), ("trailer", SubFieldsType)]
class AnswerListType(PacketType): DEFINITION_IDENTIFIER = 'lab1b.student_ZeweiLi.fromserver2' DEFINITION_VERSION = '1.0' FIELDS = [ ('id', UINT32), ('Date',ListFieldType(STRING)), ]
class TestPacket1(PacketType): DEFINITION_IDENTIFIER = "packettype.basicunittest.TestPacket1" DEFINITION_VERSION = "1.0" class SubFields(PacketFields): FIELDS = [("subfield1",Uint({Bits:16})), ("subfield2",Uint({Bits:16}))] FIELDS = [ ("ls", ListFieldType(ComplexFieldType(SubFields))) ]
class invite(PacketType): DEFINITION_IDENTIFIER = "lab1b.calling.invite" DEFINITION_VERSION = "1.0" FIELDS = [ ("name", STRING), ('available', BOOL), ("location", STRING), ("ip", STRING), ("port", UINT32), ("xccpv", INT), ("codec", ListFieldType(STRING)), ]
class SomeFields(PacketFields): FIELDS = [ ("field1", UINT({Bits:32})), ("field2", UINT({Bits:32})), ("list1", ListFieldType(UINT({Bits:8}))) ]
def basicUnitTest(): import io uint1, uint2 = UINT(), UINT() int1, int2 = INT(), INT() bool1, bool2 = BOOL(), BOOL() stream = io.BytesIO() encoder = PlaygroundStandardPacketEncoder() uint1.setData(10) encoder.encode(stream, uint1) stream.seek(0) encoder.decode(stream, uint2) assert uint2.data() == uint1.data() stream = io.BytesIO() int1.setData(-10) encoder.encode(stream, int1) stream.seek(0) encoder.decode(stream, int2) assert int1.data() == int2.data() stream = io.BytesIO() bool1.setData(False) encoder.encode(stream, bool1) stream.seek(0) encoder.decode(stream, bool2) assert bool1.data() == bool2.data() listfield1 = ListFieldType(UINT) listfield2 = ListFieldType(UINT) listfield1.append(10) listfield1.append(100) listfield1.append(1000) stream = io.BytesIO() encoder.encode(stream, listfield1) stream.seek(0) encoder.decode(stream, listfield2) assert len(listfield1) == len(listfield2) for i in range(len(listfield1)): assert listfield1[i] == listfield2[i] str1 = StringFieldType() str2 = StringFieldType() str1.setData("Test1 string") stream = io.BytesIO() encoder.encode(stream, str1) stream.seek(0) encoder.decode(stream, str2) assert str1.data() == str2.data() class SomeFields(PacketFields): FIELDS = [ ("field1", UINT({Bits:32})), ("field2", UINT({Bits:32})), ("list1", ListFieldType(UINT({Bits:8}))) ] fields1Field = ComplexFieldType(SomeFields) fields2Field = ComplexFieldType(SomeFields) fields1 = SomeFields() fields1.field1 = 50 fields1.field2 = 500 fields1.list1 = [] fields1.list1.append(0) fields1.list1.append(255) fields1Field.setData(fields1) fields2Field.setData(SomeFields()) stream = io.BytesIO() encoder.encode(stream, fields1Field) stream.seek(0) encoder.decode(stream, fields2Field) fields2 = fields2Field.data() assert fields1.field1 == fields2.field1 assert fields1.field2 == fields2.field2 assert len(fields1.list1) == len(fields2.list1) assert fields1.list1[0] == fields2.list1[0] assert fields1.list1[-1] == fields2.list1[-1]
def encode(self, stream, listType, topEncoder): stream.pack(self.LIST_SIZE_PACK_CODE, len(listType)) for i in range(len(listType)): topEncoder.encode(stream, listType.__getrawitem__(i)) def decodeIterator(self, stream, listType, topDecoder): listSize = yield from stream.unpackIterator(self.LIST_SIZE_PACK_CODE) for i in range(listSize): listType.append(PacketFieldType.UNSET) # Create a "null" entry in the list rawListData = listType.__getrawitem__(-1) try: yield from topDecoder.decodeIterator(stream, rawListData) except Exception as encodingException: raise PacketEncodingError("Error decoding index {} of list of type {}".format(i, listType.dataType())) PlaygroundStandardPacketEncoder.RegisterTypeEncoder(ListFieldType(PacketFieldType), ListEncoder) class PacketEncoder: PacketIdentifierTemplate = "!B{}sB{}s" # Length followed by length-string # For packet type identifier and version def encode(self, stream, complexType, topEncoder): packet = complexType.data() packetDefEncoded = packet.DEFINITION_IDENTIFIER.encode(UNICODE_ENCODING) packetVerEncoded = packet.DEFINITION_VERSION.encode( UNICODE_ENCODING) packCode = self.PacketIdentifierTemplate.format(len(packetDefEncoded), len(packetVerEncoded)) stream.pack(packCode, len(packetDefEncoded), packetDefEncoded, len(packetVerEncoded), packetVerEncoded) PacketFieldsEncoder().encode(stream, complexType, topEncoder)
class TestOptionalListPacket(PacketType): DEFINITION_IDENTIFIER = "lab1b.tmeng4.TestOptionalListPacket" DEFINITION_VERSION = "1.0" FIELDS = [("testlist", ListFieldType(STRING, {Optional: True}))]
def decodeIterator(self, stream, listType, topDecoder): listSize = yield from stream.unpackIterator(self.LIST_SIZE_PACK_CODE) for i in range(listSize): listType.append( PacketFieldType.UNSET) # Create a "null" entry in the list rawListData = listType.__getrawitem__(-1) try: yield from topDecoder.decodeIterator(stream, rawListData) except Exception as encodingException: raise PacketEncodingError( "Error decoding index {} of list of type {}".format( i, listType.dataType())) PlaygroundStandardPacketEncoder.RegisterTypeEncoder( ListFieldType(PacketFieldType), ListEncoder) class PacketEncoder: PacketIdentifierTemplate = "!B{}sB{}s" # Length followed by length-string # For packet type identifier and version def encode(self, stream, complexType, topEncoder): packet = complexType.data() packetDefEncoded = packet.DEFINITION_IDENTIFIER.encode( UNICODE_ENCODING) packetVerEncoded = packet.DEFINITION_VERSION.encode(UNICODE_ENCODING) packCode = self.PacketIdentifierTemplate.format( len(packetDefEncoded), len(packetVerEncoded)) stream.pack(packCode, len(packetDefEncoded), packetDefEncoded,
class TestPacket(PacketType): DEFINITION_IDENTIFIER = "lab1b.student_qxf.testPacket" DEFINITION_VERSION = "1.0" FIELDS = [ ("testlist",ListFieldType(UINT32)) ]
class AllDataPacket(PacketType): DEFINITION_IDENTIFIER = "lab1b_mwason.AllDataPacket" DEFINITION_VERSION = "1.0" FIELDS = [("all_info_colon_separated", ListFieldType(STRING))]