diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java index 0b85cc3d4..5abc8d0c5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java @@ -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 goSerializerMap; @@ -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()); @@ -77,6 +82,7 @@ public static void init(GenerateOperation generateOperation) { public static void clear() { goSerializerMap = null; protocolOutputRootPath = null; + protocolOutputPath = null; } /** diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java deleted file mode 100644 index 41c21e6f1..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and limitations under the License. - */ - -package com.zfoo.protocol.serializer.go; - -import com.zfoo.protocol.exception.RunException; -import com.zfoo.protocol.generate.GenerateProtocolFile; -import com.zfoo.protocol.model.Pair; -import com.zfoo.protocol.registration.field.IFieldRegistration; -import com.zfoo.protocol.util.StringUtils; - -import java.lang.reflect.Field; - -import static com.zfoo.protocol.util.FileUtils.LS; - -/** - * @author godotg - */ -public class GoCharSerializer implements IGoSerializer { - - @Override - public String fieldType(Field field, IFieldRegistration fieldRegistration) { - return "string"; - } - - @Override - public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("buffer.WriteChar({})", objectStr)).append(LS); - } - - @Override - public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { - String result = "result" + GenerateProtocolFile.index.getAndIncrement(); - GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("var {} = buffer.ReadChar()", result)).append(LS); - return result; - } - -} diff --git a/protocol/src/main/resources/go/ByteBuffer.go b/protocol/src/main/resources/go/ByteBuffer.go index f84f93bd3..a6ba0541f 100644 --- a/protocol/src/main/resources/go/ByteBuffer.go +++ b/protocol/src/main/resources/go/ByteBuffer.go @@ -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 @@ -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 { @@ -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))) // 右移操作>>是带符号右移 @@ -449,7 +486,7 @@ func (byteBuffer *ByteBuffer) ReadPacket(protocolId int16) any { // -------------------------------------------------IProtocolRegistration------------------------------------------------- type IProtocolRegistration interface { - ProtocolId() int16 + protocolId() int16 write(buffer *ByteBuffer, packet any) @@ -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)