def build_packets(self, session_id, action, byte_data): if len(session_id) != 16: raise proto_utils.ProtoError("the size of session_id must be 16") data_len = len(byte_data) data_seq = [] tmp_t = self.__get_sent_raw_data(data_len, byte_data) tot_seq = len(tmp_t) md5_hash = proto_utils.calc_content_md5(byte_data) seq = 1 for block in tmp_t: size = len(block) base_header = self.__build_proto_header(session_id, md5_hash, data_len, size, tot_seq, seq, action) e_hdr = self.wrap_header(base_header) e_body = self.wrap_body(size, block) data_seq.append(b"".join(( e_hdr, e_body, ))) seq += 1 return data_seq
def parse(self): size = self.__reader.size() if self.__header_ok: if size < self.__tot_length: return e_body = self.__reader.read(self.__tot_length) body = self.unwrap_body(self.__real_length, e_body) if proto_utils.calc_content_md5(body) != self.__payload_md5: raise proto_utils.ProtoError("data has been modified") self.__results.append(( self.__session_id, self.__action, body, )) self.reset() return if self.__reader.size() < self.__fixed_hdr_size: return hdr = self.unwrap_header(self.__reader.read(self.__fixed_hdr_size)) if not hdr: self.reset() return self.__session_id, self.__payload_md5, \ self.__action, self.__tot_length, self.__real_length = self.__parse_header(hdr) self.__header_ok = True
def build_packets(self, session_id, action, byte_data, redundancy=False): """ :param session_id: :param action: :param byte_data: :param redundancy:是否开启UDP数据冗余 :return: """ if len(session_id) != 16: raise proto_utils.ProtoError("the size of session_id must be 16") data_len = len(byte_data) if data_len > self.__max_pkt_size: raise proto_utils.ProtoError("the size of byte data muse be less than %s" % self.__max_pkt_size + 1) data_seq = [] tmp_t = self.__get_sent_raw_data(data_len, byte_data, redundancy=redundancy) tot_seq = len(tmp_t) md5_hash = proto_utils.calc_content_md5(byte_data) seq = 1 for block in tmp_t: size = len(block) base_header = self.__build_proto_header(session_id, md5_hash, data_len, size, tot_seq, seq, action) e_hdr = self.wrap_header(base_header) e_body = self.wrap_body(size, block) data_seq.append(b"".join((e_hdr, e_body,))) seq += 1 return data_seq
def build_packet(self, session_id, action, byte_data): if len(session_id) != 16: raise proto_utils.ProtoError("the size of session_id must be 16") pkt_len = len(byte_data) tot_len = self.get_payload_length(pkt_len) payload_md5 = proto_utils.calc_content_md5(byte_data) base_hdr = self.__build_proto_headr(session_id, payload_md5, tot_len, pkt_len, action) e_hdr = self.wrap_header(base_hdr) e_body = self.wrap_body(pkt_len, byte_data) return b"".join((e_hdr, e_body,))
def set_map_info(self, key: str, address: str, protocol: str, port: int, is_ipv6=False): k = self.__build_key(address, protocol, port, is_ipv6=is_ipv6) self.__map_info[k] = ( proto_utils.calc_content_md5(key.encode()), key, ) self.__serviced.set_port_map(address, protocol, port, is_ipv6=is_ipv6)
def parse(self): size = self.__reader.size() if self.__header_ok: if size < self.__tot_length: return e_body = self.__reader.read(self.__tot_length) body = self.unwrap_body(self.__real_length, e_body) if proto_utils.calc_content_md5(body) != self.__payload_md5: raise proto_utils.ProtoError( "data has been modified") self.__results.append((self.__session_id, self.__action, body,)) self.reset() return if self.__reader.size() < self.__fixed_hdr_size: return hdr = self.unwrap_header(self.__reader.read(self.__fixed_hdr_size)) if not hdr: self.reset() return self.__session_id, self.__payload_md5, \ self.__action, self.__tot_length, self.__real_length = self.__parse_header(hdr) self.__header_ok = True
def build_packet(self, session_id, action, byte_data): if len(session_id) != 16: raise proto_utils.ProtoError("the size of session_id must be 16") seq = [] a, b = (0, 60000,) while 1: _byte_data = byte_data[a:b] if not _byte_data: break pkt_len = len(_byte_data) tot_len = self.get_payload_length(pkt_len) payload_md5 = proto_utils.calc_content_md5(_byte_data) base_hdr = self.__build_proto_headr(session_id, payload_md5, tot_len, pkt_len, action) e_hdr = self.wrap_header(base_hdr) e_body = self.wrap_body(pkt_len, _byte_data) seq.append(b"".join((e_hdr, e_body,))) a, b = (b, b + 60000,) return b"".join(seq)
def __check_data_is_modify(self, md5, byte_data): n_md5 = proto_utils.calc_content_md5(byte_data) return md5 == n_md5
def session_id(self): if not self.__session_id: connection = self.__configs["connection"] auth_id = connection["auth_id"] self.__session_id = proto_utils.calc_content_md5(auth_id) return self.__session_id