Skip to content

Commit

Permalink
feat[golang]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 15, 2023
1 parent c32a7b3 commit 19559f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
*/
public abstract class GenerateGoUtils {

private static String protocolOutputRootPath = "goProtocol/";
// custom configuration
public static String protocolOutputRootPath = "zfoogo";
public static String protocolOutputPath = StringUtils.EMPTY;

private static Map<ISerializer, IGoSerializer> goSerializerMap;

Expand All @@ -53,10 +55,13 @@ public static IGoSerializer goSerializer(ISerializer serializer) {
}

public static void init(GenerateOperation generateOperation) {
protocolOutputRootPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);

FileUtils.deleteFile(new File(protocolOutputRootPath));
FileUtils.createDirectory(protocolOutputRootPath);
// if not specify output path, then use current default path
if (StringUtils.isEmpty(generateOperation.getProtocolPath())) {
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
} else {
protocolOutputPath = generateOperation.getProtocolPath();
}
FileUtils.deleteFile(new File(protocolOutputPath));

goSerializerMap = new HashMap<>();
goSerializerMap.put(BooleanSerializer.INSTANCE, new GoBooleanSerializer());
Expand All @@ -77,6 +82,7 @@ public static void init(GenerateOperation generateOperation) {
public static void clear() {
goSerializerMap = null;
protocolOutputRootPath = null;
protocolOutputPath = null;
}

/**
Expand Down

This file was deleted.

53 changes: 45 additions & 8 deletions protocol/src/main/resources/go/ByteBuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ type ByteBuffer struct {
readIndex int
}

func (byteBuffer *ByteBuffer) AdjustPadding(predictionLength int, beforeWriteIndex int) {
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
var currentWriteIndex = byteBuffer.WriteOffset()
var predictionCount = byteBuffer.WriteIntCount(int32(predictionLength))
var length = currentWriteIndex - beforeWriteIndex - predictionCount
var lengthCount = byteBuffer.WriteIntCount(int32(length))
var padding = lengthCount - predictionCount
if padding == 0 {
byteBuffer.SetWriteOffset(beforeWriteIndex)
byteBuffer.WriteInt(length)
byteBuffer.SetWriteOffset(currentWriteIndex)
} else {
byteBuffer.SetWriteOffset(currentWriteIndex - length)
var byteArray = byteBuffer.ReadUBytes(length)
byteBuffer.SetWriteOffset(beforeWriteIndex)
byteBuffer.WriteInt(length)
byteBuffer.WriteUBytes(byteArray)
}
}

// -------------------------------------------------get/set-------------------------------------------------
func (byteBuffer *ByteBuffer) WriteOffset() int {
return byteBuffer.writeIndex
Expand Down Expand Up @@ -163,12 +183,11 @@ func (byteBuffer *ByteBuffer) ReadShort() int16 {
return value
}

func (byteBuffer *ByteBuffer) WriteRawInt32(value int32) {
byteBuffer.EnsureCapacity(4)
var bytesBuffer = bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, value)
var byteArray = bytesBuffer.Bytes()
byteBuffer.WriteUBytes(byteArray)
func (byteBuffer *ByteBuffer) WriteRawInt32(intValue int32) {
byteBuffer.WriteUByte(byte(intValue >> 24))
byteBuffer.WriteUByte(byte(intValue >> 16))
byteBuffer.WriteUByte(byte(intValue >> 8))
byteBuffer.WriteUByte(byte(intValue))
}

func (byteBuffer *ByteBuffer) ReadRawInt32() int32 {
Expand All @@ -190,6 +209,24 @@ func (byteBuffer *ByteBuffer) ReadInt() int {
return int(byteBuffer.ReadInt32())
}

func (byteBuffer *ByteBuffer) WriteIntCount(intValue int32) int {
var value uint32 = uint32(((intValue << 1) ^ (intValue >> 31)))
// 右移操作>>是带符号右移
if value>>7 == 0 {
return 1
}
if value>>14 == 0 {
return 2
}
if value>>21 == 0 {
return 3
}
if value>>28 == 0 {
return 4
}
return 5
}

func (byteBuffer *ByteBuffer) WriteInt32(intValue int32) {
var value uint32 = uint32(((intValue << 1) ^ (intValue >> 31)))
// 右移操作>>是带符号右移
Expand Down Expand Up @@ -449,7 +486,7 @@ func (byteBuffer *ByteBuffer) ReadPacket(protocolId int16) any {

// -------------------------------------------------IProtocolRegistration-------------------------------------------------
type IProtocolRegistration interface {
ProtocolId() int16
protocolId() int16

write(buffer *ByteBuffer, packet any)

Expand All @@ -464,7 +501,7 @@ func GetProtocol(protocolId int16) IProtocolRegistration {
}

func Write(buffer *ByteBuffer, packet any) {
var protocolId = packet.(IProtocolRegistration).ProtocolId()
var protocolId = packet.(IProtocolRegistration).protocolId()
buffer.WriteShort(protocolId)
var protocolRegistration = GetProtocol(protocolId)
protocolRegistration.write(buffer, packet)
Expand Down

0 comments on commit 19559f7

Please sign in to comment.