优化客户端协议代理

This commit is contained in:
flswld
2023-06-05 12:33:31 +08:00
parent baea3b1334
commit 8d039dee06
8 changed files with 85 additions and 252 deletions

View File

@@ -8,6 +8,5 @@
> 1. 在此目录下建立proto目录
> 2. 将对应版本的proto协议文件和client_cmd.csv协议号文件复制到proto目录下
> 3. 到项目根目录下执行`make gen_client_proto`(本操作可能会修改proto文件请注意备份)
> 4. 执行`protoc --go_out=. *.proto`将proto目录下的proto协议文件编译成pb.go
> 5. 将gate服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能
> 3. 到项目根目录下执行`make gen_client_proto`
> 4. 将gate服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能

View File

@@ -1,8 +1,6 @@
package client_proto
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
@@ -33,15 +31,13 @@ func TestClientProtoGen(t *testing.T) {
clientCmdData := string(clientCmdFile)
clientCmdLineList := strings.Split(clientCmdData, "\n")
// 生成代码文件
var fileDataBuffer bytes.Buffer
fileDataBuffer.WriteString(`package client_proto
import (
"hk4e/gate/client_proto/proto"
)
func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {
`)
fileData := "package client_proto\n"
fileData += "\n"
fileData += "import (\n"
fileData += "\t\"hk4e/gate/client_proto/proto\"\n"
fileData += ")\n"
fileData += "\n"
fileData += "func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {\n"
for _, clientCmdLine := range clientCmdLineList {
// 清理空格以及换行符之类的
clientCmdLine = strings.TrimSpace(clientCmdLine)
@@ -54,125 +50,22 @@ func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {
}
cmdName := item[0]
cmdId := item[1]
_, err = fmt.Fprintf(&fileDataBuffer, ` c.clientCmdIdCmdNameMap[uint16(%s)] = "%s"
c.clientCmdNameCmdIdMap["%s"] = uint16(%s)
`, cmdId, cmdName, cmdName, cmdId)
if err != nil {
panic(err)
}
fileData += "\tc.clientCmdIdCmdNameMap[uint16(" + cmdId + ")] = \"" + cmdName + "\"\n"
fileData += "\tc.clientCmdNameCmdIdMap[\"" + cmdName + "\"] = uint16(" + cmdId + ")\n"
}
fileDataBuffer.WriteString(`}
func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {
switch protoObjName {
`)
fileData += "}\n"
fileData += "\n"
fileData += "func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {\n"
fileData += "\tswitch protoObjName {\n"
for _, protoObjName := range protoObjNameList {
_, err = fmt.Fprintf(&fileDataBuffer, ` case "%s":
return new(proto.%s)
`, protoObjName, protoObjName)
if err != nil {
panic(err)
}
fileData += "\tcase \"" + protoObjName + "\":\n\t\treturn new(proto." + protoObjName + ")\n"
}
fileDataBuffer.WriteString(` default:
return nil
}
}
`)
err = os.WriteFile("./client_proto_gen.go", fileDataBuffer.Bytes(), 0644)
fileData += "\tdefault:\n"
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")
var newFileBuffer bytes.Buffer
for i := 0; i < len(rawFileLine); i++ {
line := rawFileLine[i]
newFileBuffer.WriteString(line + "\n")
if !strings.Contains(line, "enum") {
continue
}
split := strings.Split(strings.TrimSpace(line), " ")
if len(split) != 3 || split[0] != "enum" || split[2] != "{" {
continue
}
enumName := split[1]
// 从protocol/proto_hk4e下复制同名的枚举类替换掉原proto文件里的内容
refEnum := FindEnumInDirFile("../../protocol/proto_hk4e", enumName)
if refEnum == nil {
continue
}
for _, ref := range refEnum {
newFileBuffer.WriteString(ref + "\n")
}
i++
for {
nextLine := rawFileLine[i]
if !strings.Contains(nextLine, "}") {
i++
} else {
newFileBuffer.WriteString(nextLine + "\n")
break
}
}
}
err = os.WriteFile("./proto/"+entry.Name(), newFileBuffer.Bytes(), 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(strings.TrimSpace(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
}