From 2a3ba9995beba1e34f894be5b03d6a77f40ec7e9 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 21 Jul 2024 15:38:09 +0800 Subject: [PATCH] feat[rust]: compatible protocol --- .../com/zfoo/protocol/serializer/rust/CodeGenerateRust.java | 2 +- protocol/src/main/resources/rust/byte_buffer.rs | 4 ++++ protocol/src/main/resources/rust/i_byte_buffer.rs | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/rust/CodeGenerateRust.java b/protocol/src/main/java/com/zfoo/protocol/serializer/rust/CodeGenerateRust.java index bf7ebdd5a..f349b9ecd 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/rust/CodeGenerateRust.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/rust/CodeGenerateRust.java @@ -353,7 +353,7 @@ private String protocol_read_deserialization(ProtocolRegistration registration) var field = fields[i]; var fieldRegistration = fieldRegistrations[i]; if (field.isAnnotationPresent(Compatible.class)) { - rustBuilder.append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS); + rustBuilder.append("if buffer.compatibleRead(beforeReadIndex, length) {").append(LS); var compatibleReadObject = rustSerializer(fieldRegistration.serializer()).readObject(rustBuilder, 1, field, fieldRegistration); rustBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS); rustBuilder.append("}").append(LS); diff --git a/protocol/src/main/resources/rust/byte_buffer.rs b/protocol/src/main/resources/rust/byte_buffer.rs index 3e1e00620..607d02d4a 100644 --- a/protocol/src/main/resources/rust/byte_buffer.rs +++ b/protocol/src/main/resources/rust/byte_buffer.rs @@ -51,6 +51,10 @@ impl IByteBuffer for ByteBuffer { } } + fn compatibleRead(&mut self, beforeReadIndex: i32, length: i32) -> bool{ + return length != -1 && self.getReadOffset() < length + beforeReadIndex; + } + fn getBuffer(&self) -> &Vec { return &self.buffer; } diff --git a/protocol/src/main/resources/rust/i_byte_buffer.rs b/protocol/src/main/resources/rust/i_byte_buffer.rs index 1220c189b..b464edc6e 100644 --- a/protocol/src/main/resources/rust/i_byte_buffer.rs +++ b/protocol/src/main/resources/rust/i_byte_buffer.rs @@ -13,6 +13,7 @@ pub trait IPacket { #[allow(unused_parens)] pub trait IByteBuffer { fn adjustPadding(&mut self, predictionLength: i32, beforeWriteIndex: i32); + fn compatibleRead(&mut self, beforeReadIndex: i32, length: i32) -> bool; fn getBuffer(&self) -> &Vec; fn getWriteOffset(&self) -> i32; fn setWriteOffset(&mut self, writeIndex: i32);