Skip to content

Commit

Permalink
fix[protocol]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 26, 2023
1 parent ec9ade6 commit d17c815
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public void write(ByteBuf byteBuf, Object packet) {
return;
}

var beforeWriteIndex = byteBuf.writerIndex();

if (compatible) {
byteBuf.markWriterIndex();
ByteBufUtils.writeInt(byteBuf, predictionLength);
} else {
ByteBufUtils.writeInt(byteBuf, -1);
}

var beforeWriteIndex = byteBuf.writerIndex();
for (int i = 0, length = fields.length; i < length; i++) {
Field field = fields[i];
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
Expand All @@ -96,23 +96,24 @@ public void write(ByteBuf byteBuf, Object packet) {
if (compatible) {
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
var currentWriteIndex = byteBuf.writerIndex();
var length = currentWriteIndex - beforeWriteIndex;
var predictionCount = ByteBufUtils.writeIntCount(predictionLength);
var length = currentWriteIndex - beforeWriteIndex - predictionCount;
var lengthCount = ByteBufUtils.writeIntCount(length);
var padding = lengthCount - ByteBufUtils.writeIntCount(predictionLength);
var padding = lengthCount - predictionCount;
if (padding == 0) {
byteBuf.resetWriterIndex();
byteBuf.writerIndex(beforeWriteIndex);
ByteBufUtils.writeInt(byteBuf, length);
byteBuf.writerIndex(currentWriteIndex);
} else if (padding < 0) {
var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length);
byteBuf.resetWriterIndex();
byteBuf.writerIndex(beforeWriteIndex);
ByteBufUtils.writeInt(byteBuf, length);
byteBuf.writeBytes(retainedByteBuf);
ReferenceCountUtil.release(retainedByteBuf);
} else {
var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length);
var bytes = ByteBufUtils.readAllBytes(retainedByteBuf);
byteBuf.resetWriterIndex();
byteBuf.writerIndex(beforeWriteIndex);
ByteBufUtils.writeInt(byteBuf, length);
byteBuf.writeBytes(bytes);
ReferenceCountUtil.release(retainedByteBuf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ protected Kryo initialValue() {
public static final Map<Integer, String> mapWithInteger = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -99, "b", 0, "c", 99, "d", Integer.MAX_VALUE, "e"));

public static final ObjectB objectB = new ObjectB(true);
// public static final ObjectB objectB = new ObjectB(true, 44);
public static final ObjectA objectA = new ObjectA(Integer.MAX_VALUE, mapWithInteger, objectB);
// public static final ObjectA objectA = new ObjectA(Integer.MAX_VALUE, mapWithInteger, objectB, 66);
public static final List<Integer> listWithInteger = new ArrayList<>(ArrayUtils.toList(intArray));
public static final List<Integer> listWithInteger1 = new ArrayList<>(ArrayUtils.toList(intArray1));
public static final List<Integer> listWithInteger2 = new ArrayList<>(ArrayUtils.toList(intArray2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public void enhanceNormalTest() {
public void normalTest() {
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
ProtocolManager.write(buffer, normalObject);
// FileUtils.writeInputStreamToFile(new File("normal-no-compatible.bytes"), new ByteArrayInputStream(ByteBufUtils.readAllBytes(buffer)));
// normalObject.outCompatibleValue = 88;
// FileUtils.writeInputStreamToFile(new File("normal-no-compatible.bytes"), new ByteArrayInputStream(ByteBufUtils.readAllBytes(buffer)));
var packet = ProtocolManager.read(buffer);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;


import com.zfoo.protocol.anno.Compatible;
import com.zfoo.protocol.anno.Protocol;

import java.util.List;
Expand Down Expand Up @@ -57,6 +58,9 @@ public class NormalObject {
private Set<Integer> s;
private Set<String> ssss;

// @Compatible(1)
// public int outCompatibleValue;

public byte getA() {
return a;
}
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/test/java/com/zfoo/protocol/packet/ObjectA.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;


import com.zfoo.protocol.anno.Compatible;
import com.zfoo.protocol.anno.Protocol;

import java.util.Map;
Expand All @@ -29,5 +30,8 @@ public record ObjectA(
Map<Integer, String> m,

ObjectB objectB

// @Compatible(1)
// int innerCompatibleValue,
) {
}
4 changes: 4 additions & 0 deletions protocol/src/test/java/com/zfoo/protocol/packet/ObjectB.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;


import com.zfoo.protocol.anno.Compatible;
import com.zfoo.protocol.anno.Protocol;

/**
Expand All @@ -24,6 +25,9 @@ public record ObjectB(

boolean flag

// @Compatible(1)
// int innerCompatibleValue

) {
}

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit d17c815

Please sign in to comment.