Skip to content

Commit

Permalink
codec map match
Browse files Browse the repository at this point in the history
  • Loading branch information
Bughue committed Nov 14, 2024
1 parent 2826e7c commit 8c2f73e
Showing 1 changed file with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultiVersionCodec.VersionRange, MessageSeataCodec> oldVersionCodec() {
return Collections.singletonMap(
// todo 【我的todo】
new MultiVersionCodec.VersionRange("2.1.0"),
new MessageSeataCodec() {
@Override
public Class<?> getMessageClassType() {
return RegisterRMResponseCodec.this.getMessageClassType();
}

@Override
public <T> 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 <T> 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));
}
}
);
}
}

0 comments on commit 8c2f73e

Please sign in to comment.