mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
优化协议代理代码生成
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package client_proto
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"reflect"
|
||||
|
||||
"hk4e/pkg/logger"
|
||||
)
|
||||
@@ -11,31 +9,16 @@ import (
|
||||
type ClientCmdProtoMap struct {
|
||||
clientCmdIdCmdNameMap map[uint16]string
|
||||
clientCmdNameCmdIdMap map[string]uint16
|
||||
RefValue reflect.Value
|
||||
}
|
||||
|
||||
func NewClientCmdProtoMap() (r *ClientCmdProtoMap) {
|
||||
r = new(ClientCmdProtoMap)
|
||||
r.clientCmdIdCmdNameMap = make(map[uint16]string)
|
||||
r.clientCmdNameCmdIdMap = make(map[string]uint16)
|
||||
clientCmdFile, err := os.ReadFile("./client_cmd.csv")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
clientCmdData := string(clientCmdFile)
|
||||
lineList := strings.Split(clientCmdData, "\n")
|
||||
for _, line := range lineList {
|
||||
item := strings.Split(line, ",")
|
||||
if len(item) != 2 {
|
||||
panic("parse client cmd file error")
|
||||
}
|
||||
cmdName := item[0]
|
||||
cmdId, err := strconv.Atoi(item[1])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r.clientCmdIdCmdNameMap[uint16(cmdId)] = cmdName
|
||||
r.clientCmdNameCmdIdMap[cmdName] = uint16(cmdId)
|
||||
}
|
||||
r.RefValue = reflect.ValueOf(r)
|
||||
fn := r.RefValue.MethodByName("LoadClientCmdIdAndCmdName")
|
||||
fn.Call([]reflect.Value{})
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ func TestClientProtoGen(t *testing.T) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
nameList := make([]string, 0)
|
||||
protoObjNameList := make([]string, 0)
|
||||
for _, entry := range dir {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
@@ -21,17 +21,41 @@ func TestClientProtoGen(t *testing.T) {
|
||||
if len(split) < 2 || split[len(split)-1] != "proto" {
|
||||
continue
|
||||
}
|
||||
nameList = append(nameList, split[len(split)-2])
|
||||
protoObjNameList = append(protoObjNameList, split[len(split)-2])
|
||||
}
|
||||
// 生成初始化cmdId和cmdName的方法
|
||||
clientCmdFile, err := os.ReadFile("./proto/client_cmd.csv")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
clientCmdData := string(clientCmdFile)
|
||||
clientCmdLineList := strings.Split(clientCmdData, "\n")
|
||||
// 生成代码文件
|
||||
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 {
|
||||
if clientCmdLine == "" {
|
||||
continue
|
||||
}
|
||||
item := strings.Split(clientCmdLine, ",")
|
||||
if len(item) != 2 {
|
||||
panic("parse client cmd file error")
|
||||
}
|
||||
cmdName := item[0]
|
||||
cmdId := item[1]
|
||||
fileData += "\tc.clientCmdIdCmdNameMap[uint16(" + cmdId + ")] = \"" + cmdName + "\"\n"
|
||||
fileData += "\tc.clientCmdNameCmdIdMap[\"" + cmdName + "\"] = uint16(" + cmdId + ")\n"
|
||||
}
|
||||
fileData += "}\n"
|
||||
fileData += "\n"
|
||||
fileData += "func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {\n"
|
||||
fileData += "\tswitch protoObjName {\n"
|
||||
for _, protoObjName := range nameList {
|
||||
for _, protoObjName := range protoObjNameList {
|
||||
fileData += "\tcase \"" + protoObjName + "\":\n\t\treturn new(proto." + protoObjName + ")\n"
|
||||
}
|
||||
fileData += "\tdefault:\n"
|
||||
@@ -62,6 +86,7 @@ func TestClientProtoGen(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
enumName := split[1]
|
||||
// 从protocol/proto_hk4e下复制同名的枚举类替换掉原proto文件里的内容
|
||||
refEnum := FindEnumInDirFile("../../protocol/proto_hk4e", enumName)
|
||||
if refEnum == nil {
|
||||
continue
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -29,20 +28,19 @@ const (
|
||||
)
|
||||
|
||||
type KcpConnectManager struct {
|
||||
discovery *rpc.DiscoveryClient
|
||||
openState bool
|
||||
sessionConvIdMap map[uint64]*Session
|
||||
sessionUserIdMap map[uint32]*Session
|
||||
sessionMapLock sync.RWMutex
|
||||
kcpEventInput chan *KcpEvent
|
||||
kcpEventOutput chan *KcpEvent
|
||||
serverCmdProtoMap *cmd.CmdProtoMap
|
||||
clientCmdProtoMap *client_proto.ClientCmdProtoMap
|
||||
clientCmdProtoMapRefValue reflect.Value
|
||||
messageQueue *mq.MessageQueue
|
||||
localMsgOutput chan *ProtoMsg
|
||||
createSessionChan chan *Session
|
||||
destroySessionChan chan *Session
|
||||
discovery *rpc.DiscoveryClient
|
||||
openState bool
|
||||
sessionConvIdMap map[uint64]*Session
|
||||
sessionUserIdMap map[uint32]*Session
|
||||
sessionMapLock sync.RWMutex
|
||||
kcpEventInput chan *KcpEvent
|
||||
kcpEventOutput chan *KcpEvent
|
||||
serverCmdProtoMap *cmd.CmdProtoMap
|
||||
clientCmdProtoMap *client_proto.ClientCmdProtoMap
|
||||
messageQueue *mq.MessageQueue
|
||||
localMsgOutput chan *ProtoMsg
|
||||
createSessionChan chan *Session
|
||||
destroySessionChan chan *Session
|
||||
// 密钥相关
|
||||
dispatchKey []byte
|
||||
signRsaKey []byte
|
||||
@@ -60,7 +58,6 @@ func NewKcpConnectManager(messageQueue *mq.MessageQueue, discovery *rpc.Discover
|
||||
r.serverCmdProtoMap = cmd.NewCmdProtoMap()
|
||||
if config.CONF.Hk4e.ClientProtoProxyEnable {
|
||||
r.clientCmdProtoMap = client_proto.NewClientCmdProtoMap()
|
||||
r.clientCmdProtoMapRefValue = reflect.ValueOf(r.clientCmdProtoMap)
|
||||
}
|
||||
r.messageQueue = messageQueue
|
||||
r.localMsgOutput = make(chan *ProtoMsg, 1000)
|
||||
|
||||
@@ -295,7 +295,7 @@ func (k *KcpConnectManager) encodeProtoToPayload(protoObj pb.Message) (cmdId uin
|
||||
}
|
||||
|
||||
func (k *KcpConnectManager) getClientProtoObjByName(protoObjName string) pb.Message {
|
||||
fn := k.clientCmdProtoMapRefValue.MethodByName("GetClientProtoObjByName")
|
||||
fn := k.clientCmdProtoMap.RefValue.MethodByName("GetClientProtoObjByName")
|
||||
ret := fn.Call([]reflect.Value{reflect.ValueOf(protoObjName)})
|
||||
obj := ret[0].Interface()
|
||||
if obj == nil {
|
||||
|
||||
Reference in New Issue
Block a user