From 8c2f73ec0234db4ee340f3c6d5d5f989fd1b0f22 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 14 Nov 2024 19:05:25 +0800 Subject: [PATCH] codec map match --- .../protocol/RegisterRMResponseCodec.java | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseCodec.java b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseCodec.java index 41738263166..533c9bcdbbd 100644 --- a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseCodec.java @@ -16,16 +16,73 @@ */ package org.apache.seata.serializer.seata.protocol; +import io.netty.buffer.ByteBuf; +import org.apache.seata.core.protocol.AbstractIdentifyResponse; import org.apache.seata.core.protocol.RegisterRMResponse; +import org.apache.seata.serializer.seata.MessageSeataCodec; +import org.apache.seata.serializer.seata.MultiVersionCodec; + +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Map; /** * The type Register rm response codec. - * */ -public class RegisterRMResponseCodec extends AbstractIdentifyResponseCodec { +public class RegisterRMResponseCodec extends AbstractIdentifyResponseCodec implements MultiVersionCodec { @Override public Class getMessageClassType() { return RegisterRMResponse.class; } + + + @Override + public Map oldVersionCodec() { + return Collections.singletonMap( + // todo 【我的todo】 + new MultiVersionCodec.VersionRange("2.1.0"), + new MessageSeataCodec() { + @Override + public Class getMessageClassType() { + return RegisterRMResponseCodec.this.getMessageClassType(); + } + + @Override + public void encode(T t, ByteBuf out) { + AbstractIdentifyResponse abstractIdentifyResponse = (AbstractIdentifyResponse) t; + boolean identified = abstractIdentifyResponse.isIdentified(); + String version = abstractIdentifyResponse.getVersion(); + + out.writeByte(identified ? (byte) 1 : (byte) 0); + if (version != null) { + byte[] bs = version.getBytes(UTF8); + out.writeShort((short) bs.length); + if (bs.length > 0) { + out.writeBytes(bs); + } + } else { + out.writeShort((short) 0); + } + } + + @Override + public void decode(T t, ByteBuffer in) { + AbstractIdentifyResponse abstractIdentifyResponse = (AbstractIdentifyResponse) t; + + abstractIdentifyResponse.setIdentified(in.get() == 1); + short len = in.getShort(); + if (len <= 0) { + return; + } + if (in.remaining() < len) { + return; + } + byte[] bs = new byte[len]; + in.get(bs); + abstractIdentifyResponse.setVersion(new String(bs, UTF8)); + } + } + ); + } }