class AbstractSerializer: def __init__(self,ds,cs): self.ds = ds self.cs = CustomSerializerAdapter(cs) self.output = OutputStream() self.input = InputStream() def toByte(self,obj): if object == None: return ts = self.ds if self.ds.isSuitable(obj) else self.cs self.output.writeByte(ts.getTypeId()) ts.write(self.output,obj) bytes = list(self.output.buf) self.output.flush() return bytes def toObject(self,data): self.input.setData(data) typeId = -1 try: typeId = self.input.readByte() ts = self.ds if self.ds.getTypeId()==typeId else self.cs return ts.read(self.input) except Exception as e: print "serialization error while reading" , e finally: self.input.flush()
def generate(folder): data_stream1 = InputStream(folder + "\\data1.qqq") data_stream2 = InputStream(folder + "\\data2.qqq") data_stream3 = InputStream(folder + "\\data3.qqq") data_stream4 = InputStream(folder + "\\data4.qqq") data_stream1.initialize() data_stream2.initialize() data_stream3.initialize() data_stream4.initialize() node01 = InputNode(100, data_stream1) node02 = InputNode(10, data_stream2) node03 = InputNode(10, data_stream3) node04 = InputNode(2, data_stream4) node05 = InputNode(100, data_stream4) gen1_calc_nodes = create_calculation_nodes1( [node01, node02, node03, node04, node05]) gen2_calc_nodes = create_calculation_nodes1(gen1_calc_nodes) gen3_calc_nodes = create_calculation_nodes2(gen2_calc_nodes) limit = 1073741824 # 2^30 node06 = OutputNode(0, limit) node07 = OutputNode(-limit, limit) l = len(gen3_calc_nodes) for i in range(0, int(2.0 / 3.0 * l)): node06.subscribe(gen3_calc_nodes[i]) for i in range(int(1.0 / 3.0 * l), l): node07.subscribe(gen3_calc_nodes[i]) output1 = OutputStream(folder + "\\output1.qqq") output2 = OutputStream(folder + "\\output2.qqq") node06.counter_subscribe(output1) node07.counter_subscribe(output2) return data_stream1, data_stream2, data_stream3, data_stream4, output1, output2
def __init__(self, *InputStream): if len(InputStream) <= 0: return decode_Data = OutputStream(InputStream[0]) self.headLength = decode_Data.readUnsignedShort() self.id = decode_Data.readInt() self.type = decode_Data.readInt() self.frameId = decode_Data.readUnsignedShort() self.pageId = decode_Data.readInt() self.dataLength = decode_Data.readInt() self.textLength = decode_Data.readInt()
def b_out(self, decode_Data): if not self.c_i_i(3, -1): return decode_Data v7 = self.i - decode_Data.available() if v7 >= self.e: return decode_Data v0 = self.e - v7 v1 = decode_Data.readBytes(v0) v3 = self.f * self.b v4 = decode_Data.readInt() v5 = self.a_out(decode_Data) v6 = v5 + 1 v6_1 = decode_Data.readBytes(decode_Data.available()) v8 = b'' for index in range(v6): v8 += b'\x00' ret, v6_1, v8 = ftj.ftj_b(v6_1, 0, v4, v8, v5) if ret != v3: return decode_Data v9 = b'' for index in range(v3 + v0 + 4): v9 += b'\x00' v9 = bytearray(v9) v9[:len(v1)] = bytearray(v1) v1_1 = 0 v3 = 0 while v3 < self.f: v4 = 0 v5 = v0 while v4 < self.b: v9[v5:v5 + 1] = v8[v1_1:v1_1 + 1] v5 += self.f v4 += 1 v1_1 += 1 v3 += 1 v0 += 1 self.i = len(v9) + v7 decode_Data = OutputStream(bytes(v9)) return decode_Data
def __init__(self,ds,cs): self.ds = ds self.cs = CustomSerializerAdapter(cs) self.output = OutputStream() self.input = InputStream()
def __init__(self, InputStream): decode_Data = OutputStream(InputStream) self.title = decode_Data.readUTF() self.content = decode_Data.readUTF() if self.content == '': self.content == '操作频繁,请稍后再试' length = decode_Data.readUnsignedShort() if length > 0: self.confirm = decode_Data.readUnicode2UTF8(length) length = decode_Data.readUnsignedShort() if length > 0: self.cancel = decode_Data.readUnicode2UTF8(length) if decode_Data.available() >= 6: decode_Data.skipBytes(2) self.type = decode_Data.readInt() if decode_Data.available() >= 6: decode_Data.skipBytes(2) self.id = decode_Data.readInt()
def handle_data(data): if len(data) < 24: return decode_Data = OutputStream(data) data_header = MiniDataHead(decode_Data.readBytes(24)) if data_header.id == 8888: data_header.type |= 0x0A # 加密种类 enc_type = data_header.type & 0xF0000000 # 数据包被DES加密 if enc_type == 0x10000000: plain_data = Des.des(decode_Data.readBytes(decode_Data.available()), globals()['server_key'], False) decode_Data.__init__(plain_data) # 数据包被RSA_短密钥加密,这种情况暂时未遇到,不能上线正式环境,如果出现,尝试解密后抛出异常以便分析 if enc_type == 0x20000000: if constants.debug == 1: assert False, 'RSA!!!' # 数据包被AES加密,这种情况暂时未遇到,不能上线正式环境,如果出现,抛出异常以便分析 if enc_type == 0x70000000: if constants.debug == 1: assert False, 'AES!!!' # 数据包被压缩 if data_header.type & 0xF000 == 0x1000: uncompress_data = snappy.uncompress(decode_Data.readBytes(data_header.pageId)) data = uncompress_data + decode_Data.readBytes(decode_Data.available()) decode_Data.__init__(data) # 进程数据各类情况,暂时未遇到,先直接无视,以后有时间再完善 if data_header.id == -3: if constants.debug == 1: if data_header.type & 15 == 11: assert False, 'data_header.id == -3' mark = data_header.type & 15 # 采用字典代替switch if constants.log == 1: print('data_mark={}'.format(mark)) ret_data = '' # 主要包含StuffCurveStruct以及StuffTableStruct两种格式数据的处理 # StuffCurveStruct应该包含的是股票的详细信息, 这部分暂未完成解析, 感觉用处不大 # StuffTableStruct包含了除股票信息之类, 比如个人持仓之类的信息 def case_0(): nonlocal ret_data ret_data = dataHelper.handle_case_0(data_header, decode_Data) # 系统消息 def case_1(): nonlocal ret_data stuffTextData_data = stuffTextData(data_header.type, decode_Data.readBytes(decode_Data.available())) ret_data = stuffTextData_data.title + ':' + stuffTextData_data.content def case_3(): nonlocal ret_data number = decode_Data.readByte() decode_Data.skipBytes(1) for _ in range(number): dataId = decode_Data.readUnsignedShort() ctrlType = decode_Data.readInt() colorIndex = decode_Data.readByte() content = decode_Data.readUTF() ret_data += 'stuffCtrlData:' + 'dataId=' + str(dataId) + ',ctrlType=' + str(ctrlType) + ',colorIndex=' + str( colorIndex) + ',content=' + content + '\n' # 券商登录成功返回 def case_4(): nonlocal ret_data if data_header.type == 0x10000004: ret_data = '证券登录成功' return if data_header.type == 0x30000004: ret_data = '连接中断' return ret_data = '证券登录失败' # 同花顺连接认证以及登录返回 def case_6(): nonlocal ret_data content_header = decode_Data.readByte() if content_header == 128: globals()['qs_login_header'] = decode_Data.readBytes(2) data_length = decode_Data.readShort() # 这部分数据用于二次登录的使用, 手机上的登录保持就是由于该数据. if data_length > 0: passport_dat = decode_Data.readBytes(data_length) # 写出passport.dat内容, 下一次直接加载登录就可以 passport_dat = b'\x00\x00' + passport_dat[0:2] + passport_dat with open("passport.dat", mode='wb') as f: f.write(passport_dat) # 以下数据是服务器返回的帐号登录信息 data_length = decode_Data.readShort() if data_length > 0: data_bytes = decode_Data.readBytes(data_length) data_str = str(data_bytes, encoding='gbk') # 判断一下手机号是否已经登录 if data_str[-4:] != 'sid=': nonlocal ret_data ret_data = '登录成功' return ret_data = decode_Data.readUTF() if ret_data == "": ret_data = '需要手机号登录' # 这部分数据没必要解析, 主要包含sessid以及服务器地址和端口等 ''' decode_Data.skipBytes(5) data_length = decode_Data.readShort() if data_length > 0: data_bytes = decode_Data.readBytes(data_length) data_str = str(data_bytes, encoding='gbk') print('server data: ' + data_str) decode_Data.skipBytes(2) data_length = decode_Data.readShort() if data_length > 0: data_bytes = decode_Data.readBytes(data_length) data_str = str(data_bytes, encoding='gbk') print('server data: ' + data_str) data_length = decode_Data.readShort() if data_length > 0: data_bytes = decode_Data.readBytes(data_length) data_str = str(data_bytes, encoding='gbk') print('server data: ' + data_str) ''' def case_7(): nonlocal ret_data server_data = decode_Data.readBytes(decode_Data.available()) ret_data = str(server_data, encoding='gbk') if constants.debug == 1: assert False, 'case_7' def case_9(): nonlocal ret_data server_data = decode_Data.readBytes(decode_Data.available()) ret_data = str(server_data, encoding='gbk') # 同花顺或证券登录成功后返回的个人数据或自选 def case_11(): nonlocal ret_data server_data = decode_Data.readBytes(data_header.pageId) ret_data = str(server_data, encoding='gbk') def case_13(): nonlocal ret_data stuffInteractData_data = stuffInteractData(decode_Data.readBytes(decode_Data.available())) ret_data = stuffInteractData_data.title + ':' + stuffInteractData_data.content def case_15(): nonlocal ret_data stuffTextData_data = stuffTextData(data_header.type, decode_Data.readBytes(decode_Data.available())) ret_data = stuffTextData_data.title + ':' + stuffTextData_data.content def default(): nonlocal ret_data ret_data = "出错" if constants.log == 1: print('No such case:' + str(mark)) # python没有switch, 通过这样的方式曲线救国 switch = {0: case_0, 1: case_1, 3: case_3, 4: case_4, 6: case_6, 7: case_7, 9: case_9, 11: case_11, 13: case_13, 14: case_15, 15: case_15, } switch.get(mark, default)() return ret_data, data_header.id
def __init__(self, data_type, InputStream): decode_Data = OutputStream(InputStream) self.title = decode_Data.readUTF() self.content = decode_Data.readUTF() if self.content == '': self.content == '操作频繁,请稍后再试' self.type = data_type & 0xF0 if self.type == 0: self.type == 1 elif self.type == 16: self.type == 2 elif self.type == 32: self.type == 3 elif self.type == 15 or self.type == 48: self.type == 4 elif self.type == 64: self.type == 5 if decode_Data.available() >= 6: decode_Data.skipBytes(2) self.id = decode_Data.readInt() if decode_Data.available() >= 6: decode_Data.skipBytes(2) self.reCode = decode_Data.readInt()