From 955408052d3bae1468b7a1da98cc1b507cc8f17d Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 16 Jul 2024 14:59:11 +0800 Subject: [PATCH] ref[go]: golang protocol --- .../serializer/golang/CodeGenerateGolang.java | 2 +- .../src/main/resources/golang/ByteBuffer.go | 84 ++++++++++--------- .../golang/ProtocolRegistrationTemplate.go | 2 +- .../ProtocolRegistrationTemplateEmpty.go | 2 +- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java index da09deb11..403f3f902 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java @@ -216,7 +216,7 @@ private String protocol_write_serialization(ProtocolRegistration registration) { var fieldRegistrations = registration.getFieldRegistrations(); var goBuilder = new StringBuilder(); if (registration.isCompatible()) { - goBuilder.append("var beforeWriteIndex = buffer.WriteOffset()").append(LS); + goBuilder.append("var beforeWriteIndex = buffer.GetWriteOffset()").append(LS); goBuilder.append(StringUtils.format("buffer.WriteInt({})", registration.getPredictionLength())).append(LS); } else { goBuilder.append("buffer.WriteInt(-1)").append(LS); diff --git a/protocol/src/main/resources/golang/ByteBuffer.go b/protocol/src/main/resources/golang/ByteBuffer.go index f99c90408..38bfdaa72 100644 --- a/protocol/src/main/resources/golang/ByteBuffer.go +++ b/protocol/src/main/resources/golang/ByteBuffer.go @@ -13,14 +13,14 @@ const maxSize int = 655537 var initArray []byte = make([]byte, initSize, initSize) type ByteBuffer struct { - buffer []byte - writeIndex int - readIndex int + buffer []byte + writeOffset int + readOffset int } func (byteBuffer *ByteBuffer) AdjustPadding(predictionLength int, beforeWriteIndex int) { // 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置 - var currentWriteIndex = byteBuffer.WriteOffset() + var currentWriteIndex = byteBuffer.GetWriteOffset() var predictionCount = byteBuffer.WriteIntCount(int32(predictionLength)) var length = currentWriteIndex - beforeWriteIndex - predictionCount var lengthCount = byteBuffer.WriteIntCount(int32(length)) @@ -38,44 +38,48 @@ func (byteBuffer *ByteBuffer) AdjustPadding(predictionLength int, beforeWriteInd } func (byteBuffer *ByteBuffer) CompatibleRead(beforeReadIndex int, length int) bool { - return length != -1 && byteBuffer.readIndex < length+beforeReadIndex + return length != -1 && byteBuffer.readOffset < length+beforeReadIndex } // -------------------------------------------------get/set------------------------------------------------- -func (byteBuffer *ByteBuffer) WriteOffset() int { - return byteBuffer.writeIndex +func (byteBuffer *ByteBuffer) GetBuffer() []byte { + return byteBuffer.buffer +} + +func (byteBuffer *ByteBuffer) GetWriteOffset() int { + return byteBuffer.writeOffset } func (byteBuffer *ByteBuffer) SetWriteOffset(writeIndex int) { if writeIndex > len(byteBuffer.buffer) { - var error = fmt.Sprintf("writeIndex:[{%d}] out of bounds exception: readerIndex:[{%d}] , writerIndex:[{%d}] (expected: 0 <= readerIndex <= writerIndex <= capacity:[{%d}])", writeIndex, byteBuffer.readIndex, byteBuffer.writeIndex, len(byteBuffer.buffer)) + var error = fmt.Sprintf("writeIndex:[{%d}] out of bounds exception: readOffset:[{%d}] , writeOffset:[{%d}] (expected: 0 <= readOffset <= writeOffset <= capacity:[{%d}])", writeIndex, byteBuffer.readOffset, byteBuffer.writeOffset, len(byteBuffer.buffer)) panic(error) } - byteBuffer.writeIndex = writeIndex + byteBuffer.writeOffset = writeIndex } -func (byteBuffer *ByteBuffer) ReadOffset() int { - return byteBuffer.readIndex +func (byteBuffer *ByteBuffer) GetReadOffset() int { + return byteBuffer.readOffset } func (byteBuffer *ByteBuffer) SetReadOffset(readIndex int) { - if readIndex > byteBuffer.writeIndex { - var error = fmt.Sprintf("readIndex:[{%d}] out of bounds exception: readerIndex:[{%d}] , writerIndex:[{%d}] (expected: 0 <= readerIndex <= writerIndex <= capacity:[{%d}])", readIndex, byteBuffer.readIndex, byteBuffer.writeIndex, len(byteBuffer.buffer)) + if readIndex > byteBuffer.writeOffset { + var error = fmt.Sprintf("readIndex:[{%d}] out of bounds exception: readOffset:[{%d}] , writeOffset:[{%d}] (expected: 0 <= readOffset <= writeOffset <= capacity:[{%d}])", readIndex, byteBuffer.readOffset, byteBuffer.writeOffset, len(byteBuffer.buffer)) panic(error) } - byteBuffer.readIndex = readIndex + byteBuffer.readOffset = readIndex } func (byteBuffer *ByteBuffer) ToBytes() []byte { - return byteBuffer.buffer[0:byteBuffer.writeIndex] + return byteBuffer.buffer[0:byteBuffer.writeOffset] } func (byteBuffer *ByteBuffer) ToString() string { - return fmt.Sprintf("writeIndex:[{%d}], readIndex:[{%d}], len:[{%d}], cap:[{%d}]", byteBuffer.writeIndex, byteBuffer.readIndex, len(byteBuffer.buffer), cap(byteBuffer.buffer)) + return fmt.Sprintf("writeOffset:[{%d}], readOffset:[{%d}], len:[{%d}], cap:[{%d}]", byteBuffer.writeOffset, byteBuffer.readOffset, len(byteBuffer.buffer), cap(byteBuffer.buffer)) } func (byteBuffer *ByteBuffer) GetCapacity() int { - return len(byteBuffer.buffer) - byteBuffer.writeIndex + return len(byteBuffer.buffer) - byteBuffer.writeOffset } func (byteBuffer *ByteBuffer) EnsureCapacity(capacity int) { @@ -93,12 +97,12 @@ func (byteBuffer *ByteBuffer) EnsureCapacity(capacity int) { } func (byteBuffer *ByteBuffer) Clear() { - byteBuffer.writeIndex = 0 - byteBuffer.readIndex = 0 + byteBuffer.writeOffset = 0 + byteBuffer.readOffset = 0 } func (byteBuffer *ByteBuffer) IsReadable() bool { - return byteBuffer.writeIndex > byteBuffer.readIndex + return byteBuffer.writeOffset > byteBuffer.readOffset } // -------------------------------------------------write/read------------------------------------------------- @@ -122,55 +126,55 @@ func BytesToInt(b []byte) int { func (byteBuffer *ByteBuffer) WriteBool(value bool) { byteBuffer.EnsureCapacity(1) if value { - byteBuffer.buffer[byteBuffer.writeIndex] = 1 + byteBuffer.buffer[byteBuffer.writeOffset] = 1 } else { - byteBuffer.buffer[byteBuffer.writeIndex] = 0 + byteBuffer.buffer[byteBuffer.writeOffset] = 0 } - byteBuffer.writeIndex++ + byteBuffer.writeOffset++ } func (byteBuffer *ByteBuffer) ReadBool() bool { - var byteValue = byteBuffer.buffer[byteBuffer.readIndex] - byteBuffer.readIndex++ + var byteValue = byteBuffer.buffer[byteBuffer.readOffset] + byteBuffer.readOffset++ return byteValue == 1 } func (byteBuffer *ByteBuffer) WriteByte(value int8) { byteBuffer.EnsureCapacity(1) - byteBuffer.buffer[byteBuffer.writeIndex] = byte(value) - byteBuffer.writeIndex++ + byteBuffer.buffer[byteBuffer.writeOffset] = byte(value) + byteBuffer.writeOffset++ } func (byteBuffer *ByteBuffer) ReadByte() int8 { - var byteValue = byteBuffer.buffer[byteBuffer.readIndex] - byteBuffer.readIndex++ + var byteValue = byteBuffer.buffer[byteBuffer.readOffset] + byteBuffer.readOffset++ return int8(byteValue) } func (byteBuffer *ByteBuffer) WriteUByte(value byte) { byteBuffer.EnsureCapacity(1) - byteBuffer.buffer[byteBuffer.writeIndex] = value - byteBuffer.writeIndex++ + byteBuffer.buffer[byteBuffer.writeOffset] = value + byteBuffer.writeOffset++ } func (byteBuffer *ByteBuffer) ReadUByte() byte { - var byteValue = byteBuffer.buffer[byteBuffer.readIndex] - byteBuffer.readIndex++ + var byteValue = byteBuffer.buffer[byteBuffer.readOffset] + byteBuffer.readOffset++ return byteValue } func (byteBuffer *ByteBuffer) WriteUBytes(bytes []byte) { var length = len(bytes) byteBuffer.EnsureCapacity(length) - copy(byteBuffer.buffer[byteBuffer.writeIndex:], bytes) - byteBuffer.writeIndex += length + copy(byteBuffer.buffer[byteBuffer.writeOffset:], bytes) + byteBuffer.writeOffset += length } func (byteBuffer *ByteBuffer) ReadUBytes(length int) []byte { - var readOffset = byteBuffer.readIndex - var endOffset = byteBuffer.readIndex + length + var readOffset = byteBuffer.readOffset + var endOffset = byteBuffer.readOffset + length var bytes = byteBuffer.buffer[readOffset:endOffset] - byteBuffer.readIndex += length + byteBuffer.readOffset += length return bytes } @@ -514,7 +518,7 @@ func Read(buffer *ByteBuffer) any { } // -------------------------------------------------CutDown------------------------------------------------- -func (byteBuffer *ByteBuffer) WriteBooleanArray(array []bool) { +func (byteBuffer *ByteBuffer) WriteBoolArray(array []bool) { if array == nil { byteBuffer.WriteInt(0) } else { @@ -525,7 +529,7 @@ func (byteBuffer *ByteBuffer) WriteBooleanArray(array []bool) { } } -func (byteBuffer *ByteBuffer) ReadBooleanArray() []bool { +func (byteBuffer *ByteBuffer) ReadBoolArray() []bool { var size = byteBuffer.ReadInt() var array = make([]bool, size) if size > 0 { diff --git a/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go b/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go index 5cc712f25..654302d28 100644 --- a/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go +++ b/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go @@ -17,7 +17,7 @@ func (protocol ${protocol_name}) read(buffer *ByteBuffer) any { if length == 0 { return packet } - var beforeReadIndex = buffer.ReadOffset() + var beforeReadIndex = buffer.GetReadOffset() ${protocol_read_deserialization} if length > 0 { buffer.SetReadOffset(beforeReadIndex + length) diff --git a/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go b/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go index dc47d1991..3f09d843e 100644 --- a/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go +++ b/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go @@ -16,7 +16,7 @@ func (protocol ${protocol_name}) read(buffer *ByteBuffer) any { if length == 0 { return packet } - var beforeReadIndex = buffer.ReadOffset() + var beforeReadIndex = buffer.GetReadOffset() if length > 0 { buffer.SetReadOffset(beforeReadIndex + length) }