From 3e3c398ed5ba0fb108cb88a6df6957b162051aa6 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 16 Jul 2024 15:01:27 +0800 Subject: [PATCH] test[go]: golang protocol test --- protocol/src/test/golang/buffer_test.go | 4 +- protocol/src/test/golang/zfoogo/ByteBuffer.go | 84 ++++----- .../src/test/golang/zfoogo/EmptyObject.go | 31 ++++ .../zfoogo/{Protocols.go => NormalObject.go} | 159 +----------------- protocol/src/test/golang/zfoogo/ObjectA.go | 51 ++++++ protocol/src/test/golang/zfoogo/ObjectB.go | 43 +++++ .../src/test/golang/zfoogo/SimpleObject.go | 39 +++++ 7 files changed, 214 insertions(+), 197 deletions(-) create mode 100644 protocol/src/test/golang/zfoogo/EmptyObject.go rename protocol/src/test/golang/zfoogo/{Protocols.go => NormalObject.go} (51%) create mode 100644 protocol/src/test/golang/zfoogo/ObjectA.go create mode 100644 protocol/src/test/golang/zfoogo/ObjectB.go create mode 100644 protocol/src/test/golang/zfoogo/SimpleObject.go diff --git a/protocol/src/test/golang/buffer_test.go b/protocol/src/test/golang/buffer_test.go index 69c728fba..2d92dc0fa 100644 --- a/protocol/src/test/golang/buffer_test.go +++ b/protocol/src/test/golang/buffer_test.go @@ -133,11 +133,11 @@ func packetTest() { buffer.WriteUBytes(bytes) var packet = zfoogo.Read(buffer) fmt.Println(packet) - fmt.Println("source size ", buffer.WriteOffset()) + fmt.Println("source size ", buffer.GetWriteOffset()) buffer.Clear() zfoogo.Write(buffer, packet) var newPacket = zfoogo.Read(buffer) - fmt.Println("target size ", buffer.WriteOffset()) + fmt.Println("target size ", buffer.GetWriteOffset()) fmt.Println(newPacket) } diff --git a/protocol/src/test/golang/zfoogo/ByteBuffer.go b/protocol/src/test/golang/zfoogo/ByteBuffer.go index f99c90408..38bfdaa72 100644 --- a/protocol/src/test/golang/zfoogo/ByteBuffer.go +++ b/protocol/src/test/golang/zfoogo/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/test/golang/zfoogo/EmptyObject.go b/protocol/src/test/golang/zfoogo/EmptyObject.go new file mode 100644 index 000000000..69ddfdd56 --- /dev/null +++ b/protocol/src/test/golang/zfoogo/EmptyObject.go @@ -0,0 +1,31 @@ +package zfoogo + + +type EmptyObject struct { + +} + +func (protocol EmptyObject) ProtocolId() int16 { + return 0 +} + +func (protocol EmptyObject) write(buffer *ByteBuffer, packet any) { + if packet == nil { + buffer.WriteInt(0) + return + } + buffer.WriteInt(-1) +} + +func (protocol EmptyObject) read(buffer *ByteBuffer) any { + var packet = new(EmptyObject) + var length = buffer.ReadInt() + if length == 0 { + return packet + } + var beforeReadIndex = buffer.GetReadOffset() + if length > 0 { + buffer.SetReadOffset(beforeReadIndex + length) + } + return packet +} \ No newline at end of file diff --git a/protocol/src/test/golang/zfoogo/Protocols.go b/protocol/src/test/golang/zfoogo/NormalObject.go similarity index 51% rename from protocol/src/test/golang/zfoogo/Protocols.go rename to protocol/src/test/golang/zfoogo/NormalObject.go index 0cef3677a..362ee380b 100644 --- a/protocol/src/test/golang/zfoogo/Protocols.go +++ b/protocol/src/test/golang/zfoogo/NormalObject.go @@ -1,14 +1,11 @@ package zfoogo - -type EmptyObject struct { - -} - +// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法 type NormalObject struct { A int8 Aaa []int8 B int16 + // 整数类型 C int D int64 E float32 @@ -28,47 +25,6 @@ type NormalObject struct { OutCompatibleValue2 int } -type ObjectA struct { - A int - M map[int]string - ObjectB ObjectB - InnerCompatibleValue int -} - -type ObjectB struct { - Flag bool - InnerCompatibleValue int -} - -type SimpleObject struct { - C int - G bool -} - -func (protocol EmptyObject) ProtocolId() int16 { - return 0 -} - -func (protocol EmptyObject) write(buffer *ByteBuffer, packet any) { - if packet == nil { - buffer.WriteInt(0) - return - } - buffer.WriteInt(-1) -} - -func (protocol EmptyObject) read(buffer *ByteBuffer) any { - var packet = new(EmptyObject) - var length = buffer.ReadInt() - if length == 0 { - return packet - } - var beforeReadIndex = buffer.ReadOffset() - if length > 0 { - buffer.SetReadOffset(beforeReadIndex + length) - } - return packet -} func (protocol NormalObject) ProtocolId() int16 { return 101 } @@ -79,7 +35,7 @@ func (protocol NormalObject) write(buffer *ByteBuffer, packet any) { return } var message = packet.(*NormalObject) - var beforeWriteIndex = buffer.WriteOffset() + var beforeWriteIndex = buffer.GetWriteOffset() buffer.WriteInt(857) buffer.WriteByte(message.A) buffer.WriteByteArray(message.Aaa) @@ -127,7 +83,7 @@ func (protocol NormalObject) read(buffer *ByteBuffer) any { if length == 0 { return packet } - var beforeReadIndex = buffer.ReadOffset() + var beforeReadIndex = buffer.GetReadOffset() var result0 = buffer.ReadByte() packet.A = result0 var array1 = buffer.ReadByteArray() @@ -191,111 +147,4 @@ func (protocol NormalObject) read(buffer *ByteBuffer) any { buffer.SetReadOffset(beforeReadIndex + length) } return packet -} -func (protocol ObjectA) ProtocolId() int16 { - return 102 -} - -func (protocol ObjectA) write(buffer *ByteBuffer, packet any) { - if packet == nil { - buffer.WriteInt(0) - return - } - var message = packet.(*ObjectA) - var beforeWriteIndex = buffer.WriteOffset() - buffer.WriteInt(201) - buffer.WriteInt(message.A) - buffer.WriteIntStringMap(message.M) - buffer.WritePacket(&message.ObjectB, 103) - buffer.WriteInt(message.InnerCompatibleValue) - buffer.AdjustPadding(201, beforeWriteIndex) -} - -func (protocol ObjectA) read(buffer *ByteBuffer) any { - var packet = new(ObjectA) - var length = buffer.ReadInt() - if length == 0 { - return packet - } - var beforeReadIndex = buffer.ReadOffset() - var result0 = buffer.ReadInt() - packet.A = result0 - var map1 = buffer.ReadIntStringMap() - packet.M = map1 - var result2 = *buffer.ReadPacket(103).(*ObjectB) - packet.ObjectB = result2 - if buffer.CompatibleRead(beforeReadIndex, length) { - var result3 = buffer.ReadInt() - packet.InnerCompatibleValue = result3 - } - if length > 0 { - buffer.SetReadOffset(beforeReadIndex + length) - } - return packet -} -func (protocol ObjectB) ProtocolId() int16 { - return 103 -} - -func (protocol ObjectB) write(buffer *ByteBuffer, packet any) { - if packet == nil { - buffer.WriteInt(0) - return - } - var message = packet.(*ObjectB) - var beforeWriteIndex = buffer.WriteOffset() - buffer.WriteInt(4) - buffer.WriteBool(message.Flag) - buffer.WriteInt(message.InnerCompatibleValue) - buffer.AdjustPadding(4, beforeWriteIndex) -} - -func (protocol ObjectB) read(buffer *ByteBuffer) any { - var packet = new(ObjectB) - var length = buffer.ReadInt() - if length == 0 { - return packet - } - var beforeReadIndex = buffer.ReadOffset() - var result0 = buffer.ReadBool() - packet.Flag = result0 - if buffer.CompatibleRead(beforeReadIndex, length) { - var result1 = buffer.ReadInt() - packet.InnerCompatibleValue = result1 - } - if length > 0 { - buffer.SetReadOffset(beforeReadIndex + length) - } - return packet -} -func (protocol SimpleObject) ProtocolId() int16 { - return 104 -} - -func (protocol SimpleObject) write(buffer *ByteBuffer, packet any) { - if packet == nil { - buffer.WriteInt(0) - return - } - var message = packet.(*SimpleObject) - buffer.WriteInt(-1) - buffer.WriteInt(message.C) - buffer.WriteBool(message.G) -} - -func (protocol SimpleObject) read(buffer *ByteBuffer) any { - var packet = new(SimpleObject) - var length = buffer.ReadInt() - if length == 0 { - return packet - } - var beforeReadIndex = buffer.ReadOffset() - var result0 = buffer.ReadInt() - packet.C = result0 - var result1 = buffer.ReadBool() - packet.G = result1 - if length > 0 { - buffer.SetReadOffset(beforeReadIndex + length) - } - return packet } \ No newline at end of file diff --git a/protocol/src/test/golang/zfoogo/ObjectA.go b/protocol/src/test/golang/zfoogo/ObjectA.go new file mode 100644 index 000000000..7104b6425 --- /dev/null +++ b/protocol/src/test/golang/zfoogo/ObjectA.go @@ -0,0 +1,51 @@ +package zfoogo + + +type ObjectA struct { + A int + M map[int]string + ObjectB ObjectB + InnerCompatibleValue int +} + +func (protocol ObjectA) ProtocolId() int16 { + return 102 +} + +func (protocol ObjectA) write(buffer *ByteBuffer, packet any) { + if packet == nil { + buffer.WriteInt(0) + return + } + var message = packet.(*ObjectA) + var beforeWriteIndex = buffer.GetWriteOffset() + buffer.WriteInt(201) + buffer.WriteInt(message.A) + buffer.WriteIntStringMap(message.M) + buffer.WritePacket(&message.ObjectB, 103) + buffer.WriteInt(message.InnerCompatibleValue) + buffer.AdjustPadding(201, beforeWriteIndex) +} + +func (protocol ObjectA) read(buffer *ByteBuffer) any { + var packet = new(ObjectA) + var length = buffer.ReadInt() + if length == 0 { + return packet + } + var beforeReadIndex = buffer.GetReadOffset() + var result0 = buffer.ReadInt() + packet.A = result0 + var map1 = buffer.ReadIntStringMap() + packet.M = map1 + var result2 = *buffer.ReadPacket(103).(*ObjectB) + packet.ObjectB = result2 + if buffer.CompatibleRead(beforeReadIndex, length) { + var result3 = buffer.ReadInt() + packet.InnerCompatibleValue = result3 + } + if length > 0 { + buffer.SetReadOffset(beforeReadIndex + length) + } + return packet +} \ No newline at end of file diff --git a/protocol/src/test/golang/zfoogo/ObjectB.go b/protocol/src/test/golang/zfoogo/ObjectB.go new file mode 100644 index 000000000..7fd7feaaa --- /dev/null +++ b/protocol/src/test/golang/zfoogo/ObjectB.go @@ -0,0 +1,43 @@ +package zfoogo + + +type ObjectB struct { + Flag bool + InnerCompatibleValue int +} + +func (protocol ObjectB) ProtocolId() int16 { + return 103 +} + +func (protocol ObjectB) write(buffer *ByteBuffer, packet any) { + if packet == nil { + buffer.WriteInt(0) + return + } + var message = packet.(*ObjectB) + var beforeWriteIndex = buffer.GetWriteOffset() + buffer.WriteInt(4) + buffer.WriteBool(message.Flag) + buffer.WriteInt(message.InnerCompatibleValue) + buffer.AdjustPadding(4, beforeWriteIndex) +} + +func (protocol ObjectB) read(buffer *ByteBuffer) any { + var packet = new(ObjectB) + var length = buffer.ReadInt() + if length == 0 { + return packet + } + var beforeReadIndex = buffer.GetReadOffset() + var result0 = buffer.ReadBool() + packet.Flag = result0 + if buffer.CompatibleRead(beforeReadIndex, length) { + var result1 = buffer.ReadInt() + packet.InnerCompatibleValue = result1 + } + if length > 0 { + buffer.SetReadOffset(beforeReadIndex + length) + } + return packet +} \ No newline at end of file diff --git a/protocol/src/test/golang/zfoogo/SimpleObject.go b/protocol/src/test/golang/zfoogo/SimpleObject.go new file mode 100644 index 000000000..b2683301d --- /dev/null +++ b/protocol/src/test/golang/zfoogo/SimpleObject.go @@ -0,0 +1,39 @@ +package zfoogo + + +type SimpleObject struct { + C int + G bool +} + +func (protocol SimpleObject) ProtocolId() int16 { + return 104 +} + +func (protocol SimpleObject) write(buffer *ByteBuffer, packet any) { + if packet == nil { + buffer.WriteInt(0) + return + } + var message = packet.(*SimpleObject) + buffer.WriteInt(-1) + buffer.WriteInt(message.C) + buffer.WriteBool(message.G) +} + +func (protocol SimpleObject) read(buffer *ByteBuffer) any { + var packet = new(SimpleObject) + var length = buffer.ReadInt() + if length == 0 { + return packet + } + var beforeReadIndex = buffer.GetReadOffset() + var result0 = buffer.ReadInt() + packet.C = result0 + var result1 = buffer.ReadBool() + packet.G = result1 + if length > 0 { + buffer.SetReadOffset(beforeReadIndex + length) + } + return packet +} \ No newline at end of file