From 0b3cfe06cb3fb56983d785a4e7058de5a4295883 Mon Sep 17 00:00:00 2001 From: flswld Date: Mon, 23 Jan 2023 16:35:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E4=BB=A3=E7=90=86=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E7=B1=BB=E5=9E=8B=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/mq/nats.go | 3 +- gate/client_proto/client_proto_gen_test.go | 94 +++++++++++++++++++++- pkg/object/object.go | 8 +- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/common/mq/nats.go b/common/mq/nats.go index 6116fdbd..bdea4f29 100644 --- a/common/mq/nats.go +++ b/common/mq/nats.go @@ -358,7 +358,8 @@ func (m *MessageQueue) gateTcpMqRecvHandleLoop(data []byte, dataBuf *[]byte) { } // 长度太短 if len(data) < 4 { - logger.Debug("packet len less 4 byte") + logger.Debug("packet len less 4 byte, data: %v", data) + *dataBuf = append(*dataBuf, data...) return } // 消息的载荷部分长度 diff --git a/gate/client_proto/client_proto_gen_test.go b/gate/client_proto/client_proto_gen_test.go index 69c2435e..fd6b5181 100644 --- a/gate/client_proto/client_proto_gen_test.go +++ b/gate/client_proto/client_proto_gen_test.go @@ -7,6 +7,7 @@ import ( ) func TestClientProtoGen(t *testing.T) { + // 生成根据proto类名获取对象实例的switch方法 dir, err := os.ReadDir("./proto") if err != nil { panic(err) @@ -22,7 +23,6 @@ func TestClientProtoGen(t *testing.T) { } nameList = append(nameList, split[len(split)-2]) } - fileData := "package client_proto\n" fileData += "\n" fileData += "import (\n" @@ -38,9 +38,99 @@ func TestClientProtoGen(t *testing.T) { fileData += "\t\treturn nil\n" fileData += "\t}\n" fileData += "}\n" - err = os.WriteFile("./client_proto_gen.go", []byte(fileData), 0644) if err != nil { panic(err) } + // 处理枚举 + for _, entry := range dir { + rawFileData, err := os.ReadFile("./proto/" + entry.Name()) + if err != nil { + panic(err) + } + rawFileStr := string(rawFileData) + rawFileLine := strings.Split(rawFileStr, "\n") + newFileStr := "" + for i := 0; i < len(rawFileLine); i++ { + line := rawFileLine[i] + newFileStr += line + "\n" + if !strings.Contains(line, "enum") { + continue + } + split := strings.Split(line, " ") + if len(split) != 3 || split[0] != "enum" || split[2] != "{" { + continue + } + enumName := split[1] + refEnum := FindEnumInDirFile("../../protocol/proto_hk4e", enumName) + if refEnum == nil { + continue + } + i++ + x := 0 + for { + nextLine := rawFileLine[i] + if !strings.Contains(nextLine, "}") && x < len(refEnum) { + newFileStr += refEnum[x] + "\n" + i++ + x++ + } else { + newFileStr += line + "\n" + break + } + } + } + err = os.WriteFile("./proto/"+entry.Name(), []byte(newFileStr), 0644) + if err != nil { + panic(err) + } + } +} + +func FindEnumInDirFile(path string, name string) (lineList []string) { + dir, err := os.ReadDir(path) + if err != nil { + panic(err) + } + for _, entry := range dir { + if entry.IsDir() { + ret := FindEnumInDirFile(path+"/"+entry.Name(), name) + if ret != nil { + return ret + } + continue + } + fileData, err := os.ReadFile(path + "/" + entry.Name()) + if err != nil { + panic(err) + } + fileStr := string(fileData) + fileLine := strings.Split(fileStr, "\n") + for i := 0; i < len(fileLine); i++ { + line := fileLine[i] + if !strings.Contains(line, "enum") { + continue + } + split := strings.Split(line, " ") + if len(split) != 3 || split[0] != "enum" || split[2] != "{" { + continue + } + enumName := split[1] + if enumName != name { + continue + } + i++ + lineList := make([]string, 0) + for { + nextLine := fileLine[i] + if !strings.Contains(nextLine, "}") { + lineList = append(lineList, nextLine) + } else { + return lineList + } + i++ + } + } + } + return nil } diff --git a/pkg/object/object.go b/pkg/object/object.go index a0622c10..929f65f6 100644 --- a/pkg/object/object.go +++ b/pkg/object/object.go @@ -39,7 +39,7 @@ func FastDeepCopy(dst, src any) error { } func CopyProtoBufSameField(dst, src pb.Message) ([]string, error) { - date, err := protojson.Marshal(src) + data, err := protojson.Marshal(src) if err != nil { return nil, err } @@ -50,7 +50,7 @@ func CopyProtoBufSameField(dst, src pb.Message) ([]string, error) { if loopCount > 1000 { return nil, errors.New("loop count limit") } - err = protojson.Unmarshal(date, dst) + err = protojson.Unmarshal(data, dst) if err != nil { if !strings.Contains(err.Error(), "unknown field") { return nil, err @@ -61,13 +61,13 @@ func CopyProtoBufSameField(dst, src pb.Message) ([]string, error) { } fieldName := split[1] jsonObj := make(map[string]any) - err = json.Unmarshal(date, &jsonObj) + err = json.Unmarshal(data, &jsonObj) if err != nil { return nil, err } DeleteAllKeyNameFromStringAnyMap(jsonObj, fieldName) delList = append(delList, fieldName) - date, err = json.Marshal(jsonObj) + data, err = json.Marshal(jsonObj) if err != nil { return nil, err }