def __init__(self): self.Serializer = JsonSerializer() self.prefix = PacketConfig.MAGIC_NUMBER self.prefix += struct.pack(PacketConfig.VersionFmt, PacketConfig.VERSION) self.prefix += struct.pack(PacketConfig.SerializerFmt, self.Serializer.getType()) self.prefix += struct.pack(PacketConfig.EndianFmt, PacketConfig.STREAM_ENDIAN)
def read(path, dataType=None, inProject=True): if inProject: path = join(ProjectPath().path, path) with open(path, 'r') as f: data = f.read() if dataType is None: return JsonSerializer().dic(data) else: return JsonSerializer().deserialize(dataType, data)
def channelRead(self, context, obj): if type(obj) not in self.excludedTypes: logging.info("[Channel#{}]Inbound<{}>: {}".format( context.channel.fileno, type(obj).__name__, JsonSerializer().serialize(obj))) self.nextHandle(context, obj)
def send(self): try: while len(self.frames) > 0: frame = self.frames.popleft() requests.post(settings.SEND_PATH, params={'username': '******'}, data=JsonSerializer().serialize(frame)) except Exception as e: return
def channelWrite(self, context, obj): msg = "[Channel#{}]Outbound<{}>: {}".format( context.channel.fileno, type(obj).__name__, JsonSerializer().serialize(obj)) if type(obj) in self.errorTypes or type(obj) is self.errorType: logging.error(msg) elif type(obj) not in self.excludedTypes: logging.info(msg) self.nextHandle(context, obj)
class PacketCodec(object): __metaclass__ = Singleton def __init__(self): self.Serializer = JsonSerializer() self.prefix = PacketConfig.MAGIC_NUMBER self.prefix += struct.pack(PacketConfig.VersionFmt, PacketConfig.VERSION) self.prefix += struct.pack(PacketConfig.SerializerFmt, self.Serializer.getType()) self.prefix += struct.pack(PacketConfig.EndianFmt, PacketConfig.STREAM_ENDIAN) # encode outbound packet to bytes def encode(self, packet): payload = self.prefix payload += struct.pack(PacketConfig.PacketTypeFmt, packet.typeId) data = self.Serializer.serialize(packet) payload += struct.pack(PacketConfig.PacketSizeFmt, len(data)) payload += data return payload # decode bytes to inbound event def decode(self, frame): # todo: 如果有需求,从数据流中获得序列化器的类型与端序做解析 frame = frame[PacketConfig.META_PREFIX_LENGTH:] # skip prefix packetTypeId = struct.unpack( PacketConfig.PacketTypeFmt, frame[:PacketConfig.CONTENT_TYPE_LENGTH])[0] packetContent = frame[PacketConfig.CONTENT_PREFIX_LENGTH:] # 通过在字节流中取出的type id取得指定的packet type packetType = InboundPacket.getType(packetTypeId) if packetType is None: logging.warn( "Unrecognized type id {0} with content {1}! Please refer packet.inbound.__init__.py" .format(packetTypeId, packetContent)) return None # 通过packet type去把我们得到的(字节流,type)解析成我们真正需要的数据包 return self.Serializer.deserializeConcept(packetType, packetContent)
def write(obj, path, inProject=True): if inProject: path = join(ProjectPath().path, path) with open(path, 'w') as f: data = JsonSerializer().serialize(obj) f.write(data)
def __init__(self, eid, info): super(UpdateSkillInfo, self).__init__() self.eid = eid self.info = JsonSerializer().serialize(info)
def __init__(self, eid, serial, data): super(ModifierChange, self).__init__() self.eid = eid self.serial = serial self.data = JsonSerializer().serialize(data)
def __init__(self, eid, serial, name, data): super(AddModifier, self).__init__() self.eid = eid self.serial = serial self.name = name self.data = JsonSerializer().serialize(data)