def _control_message(self, ctrl, msg): """ Pack a control message and a regular message (can be None) together and send them over the connection """ if msg is None: packet = b'p' + codec.term_to_binary(ctrl) else: packet = b'p' + codec.term_to_binary(ctrl) + codec.term_to_binary( msg) self._send_packet4(packet)
def tcp_receive(conn, proto_dict): size_str = conn.recv(4) if not size_str: return False size_int = int.from_bytes(size_str, byteorder='big') logging.info('Receiving : size_str = %s, size = %d', size_str, size_int) c2s_bin = conn.recv(size_int - 1) logging.info('Receiving : len = %s, c2s_bin = %s', len(c2s_bin), c2s_bin) c2s_msg = all_in_one.C2s().parse(c2s_bin) logging.info('c2s_msg = %s', c2s_msg) msg_seq = c2s_msg.seq msg_type = c2s_msg.type msg_data = c2s_msg.data if msg_type != -1: proto_name = proto_dict.get(str(msg_type)) logging.info('\t%d => %s : %s', msg_type, type(proto_name), proto_name) if proto_name is None: logging.info('\tmsg_data = %s', msg_data) erl_term = codec.binary_to_term(msg_data) (erl_val, tail) = erl_term logging.info('\terl_val = %s, type = %s, tail = %s', erl_val, type(erl_val), tail) term_bytes = codec.term_to_binary(erl_val) logging.info('\tcodec.term_to_binary(erl_val) = %s', term_bytes) erl_term2 = codec.binary_to_term(term_bytes) (erl_val2, tail2) = erl_term2 logging.info('\terl_val2 = %s, type = %s, tail = %s', erl_val2, type(erl_val2), tail) else: cls = get_class('protobuf.all_in_one', proto_name) logging.info('\tcls = %s', cls) pb_msg = cls().parse(msg_data) logging.info('\tpb_msg = %s', pb_msg) cp_msg = copy.deepcopy(pb_msg) logging.info('\tcp_msg = %s', cp_msg) message_replace(cp_msg, UID_BASE_BOUNDARY * 3) logging.info('\tcp_msg = %s', cp_msg) # logging.info('\tmsg_data == bytes(pb_msg) -> %s', msg_data == bytes(pb_msg)) # logging.info('\tlen(msg_data) == len(bytes(pb_msg)) -> %s', len(msg_data) == len(bytes(pb_msg))) # logging.info('\tmsg_data = %s', msg_data) # logging.info('\tpb_bytes = %s', bytes(pb_msg)) # logging.info('\tpb_bytes == cpy_bytes -> %s', bytes(pb_msg) == bytes(cpy)) # logging.info('\tlen(pb_bytes) == (cpy_bytes) -> %s', len(bytes(pb_msg)) == len(bytes(cpy))) need_response = int.from_bytes(conn.recv(1), byteorder='big') logging.info('need_response = %s', need_response) return True
def __bytes__(self): # todo: 确定这个结果是否正确 return codec.term_to_binary(self.erl_term)