mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
网关性能优化
This commit is contained in:
@@ -10,4 +10,4 @@
|
||||
> 2. 将对应版本的proto协议文件和client_cmd.csv协议号文件复制到proto目录下
|
||||
> 3. 到项目根目录下执行`make gen_client_proto`(本操作可能会修改proto文件,请注意备份)
|
||||
> 4. 执行`protoc --go_out=. *.proto`,将proto目录下的proto协议文件编译成pb.go
|
||||
> 5. 将gate和gs和fight服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能
|
||||
> 5. 将gate服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能
|
||||
|
||||
@@ -217,16 +217,11 @@ func ProtoEncode(protoMsg *ProtoMsg,
|
||||
}
|
||||
// payload msg
|
||||
if protoMsg.PayloadMessage != nil {
|
||||
cmdId, protoData := EncodeProtoToPayload(protoMsg.PayloadMessage, serverCmdProtoMap)
|
||||
if cmdId == 0 || protoData == nil {
|
||||
protoData := EncodeProtoToPayload(protoMsg.PayloadMessage, serverCmdProtoMap)
|
||||
if protoData == nil {
|
||||
logger.Error("encode proto data is nil")
|
||||
return nil
|
||||
}
|
||||
if cmdId != 65535 && cmdId != protoMsg.CmdId {
|
||||
logger.Error("cmd id is not match with proto obj, src cmd id: %v, found cmd id: %v",
|
||||
protoMsg.CmdId, cmdId)
|
||||
return nil
|
||||
}
|
||||
kcpMsg.ProtoData = protoData
|
||||
} else {
|
||||
kcpMsg.ProtoData = nil
|
||||
@@ -279,7 +274,7 @@ func ProtoEncode(protoMsg *ProtoMsg,
|
||||
}
|
||||
|
||||
func DecodePayloadToProto(cmdId uint16, protoData []byte, serverCmdProtoMap *cmd.CmdProtoMap) (protoObj pb.Message) {
|
||||
protoObj = serverCmdProtoMap.GetProtoObjByCmdId(cmdId)
|
||||
protoObj = serverCmdProtoMap.GetProtoObjCacheByCmdId(cmdId)
|
||||
if protoObj == nil {
|
||||
logger.Error("get new proto object is nil")
|
||||
return nil
|
||||
@@ -292,15 +287,14 @@ func DecodePayloadToProto(cmdId uint16, protoData []byte, serverCmdProtoMap *cmd
|
||||
return protoObj
|
||||
}
|
||||
|
||||
func EncodeProtoToPayload(protoObj pb.Message, serverCmdProtoMap *cmd.CmdProtoMap) (cmdId uint16, protoData []byte) {
|
||||
cmdId = serverCmdProtoMap.GetCmdIdByProtoObj(protoObj)
|
||||
func EncodeProtoToPayload(protoObj pb.Message, serverCmdProtoMap *cmd.CmdProtoMap) (protoData []byte) {
|
||||
var err error = nil
|
||||
protoData, err = pb.Marshal(protoObj)
|
||||
if err != nil {
|
||||
logger.Error("marshal proto object err: %v", err)
|
||||
return 0, nil
|
||||
return nil
|
||||
}
|
||||
return cmdId, protoData
|
||||
return protoData
|
||||
}
|
||||
|
||||
func GetClientProtoObjByName(protoObjName string, clientCmdProtoMap *client_proto.ClientCmdProtoMap) pb.Message {
|
||||
|
||||
@@ -23,6 +23,8 @@ import (
|
||||
"hk4e/pkg/random"
|
||||
"hk4e/protocol/cmd"
|
||||
"hk4e/protocol/proto"
|
||||
|
||||
pb "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -80,7 +82,8 @@ func (k *KcpConnectManager) recvMsgHandle(protoMsg *ProtoMsg, session *Session)
|
||||
rsp.HeadMessage = k.getHeadMsg(protoMsg.HeadMessage.ClientSequenceId)
|
||||
rsp.PayloadMessage = pingRsp
|
||||
k.localMsgOutput <- rsp
|
||||
logger.Debug("convId: %v, RTO: %v, SRTT: %v, RTTVar: %v", protoMsg.ConvId, session.conn.GetRTO(), session.conn.GetSRTT(), session.conn.GetSRTTVar())
|
||||
logger.Debug("convId: %v, RTO: %v, SRTT: %v, RTTVar: %v",
|
||||
protoMsg.ConvId, session.conn.GetRTO(), session.conn.GetSRTT(), session.conn.GetSRTTVar())
|
||||
if connState != ConnActive {
|
||||
return
|
||||
}
|
||||
@@ -108,13 +111,20 @@ func (k *KcpConnectManager) recvMsgHandle(protoMsg *ProtoMsg, session *Session)
|
||||
logger.Error("conn not active so drop packet, cmdId: %v, userId: %v, convId: %v", protoMsg.CmdId, userId, protoMsg.ConvId)
|
||||
return
|
||||
}
|
||||
gameMsg := new(mq.GameMsg)
|
||||
gameMsg.UserId = userId
|
||||
gameMsg.CmdId = protoMsg.CmdId
|
||||
gameMsg.ClientSeq = protoMsg.HeadMessage.ClientSequenceId
|
||||
// 在这里直接序列化成二进制数据 终结PayloadMessage的生命周期并回收进缓存池
|
||||
payloadMessageData, err := pb.Marshal(protoMsg.PayloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v, stack: %v", err, logger.Stack())
|
||||
return
|
||||
}
|
||||
k.serverCmdProtoMap.PutProtoObjCache(protoMsg.CmdId, protoMsg.PayloadMessage)
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
// 转发到寻路服务器
|
||||
if session.pathfindingServerAppId != "" && (protoMsg.CmdId == cmd.QueryPathReq || protoMsg.CmdId == cmd.ObstacleModifyNotify) {
|
||||
gameMsg := new(mq.GameMsg)
|
||||
gameMsg.UserId = userId
|
||||
gameMsg.CmdId = protoMsg.CmdId
|
||||
gameMsg.ClientSeq = protoMsg.HeadMessage.ClientSequenceId
|
||||
gameMsg.PayloadMessage = protoMsg.PayloadMessage
|
||||
k.messageQueue.SendToPathfinding(session.pathfindingServerAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
EventId: mq.NormalMsg,
|
||||
@@ -124,11 +134,6 @@ func (k *KcpConnectManager) recvMsgHandle(protoMsg *ProtoMsg, session *Session)
|
||||
}
|
||||
// 转发到战斗服务器
|
||||
if session.fightServerAppId != "" && protoMsg.CmdId == cmd.CombatInvocationsNotify {
|
||||
gameMsg := new(mq.GameMsg)
|
||||
gameMsg.UserId = userId
|
||||
gameMsg.CmdId = protoMsg.CmdId
|
||||
gameMsg.ClientSeq = protoMsg.HeadMessage.ClientSequenceId
|
||||
gameMsg.PayloadMessage = protoMsg.PayloadMessage
|
||||
k.messageQueue.SendToFight(session.fightServerAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
EventId: mq.NormalMsg,
|
||||
@@ -136,11 +141,6 @@ func (k *KcpConnectManager) recvMsgHandle(protoMsg *ProtoMsg, session *Session)
|
||||
})
|
||||
}
|
||||
// 转发到GS
|
||||
gameMsg := new(mq.GameMsg)
|
||||
gameMsg.UserId = userId
|
||||
gameMsg.CmdId = protoMsg.CmdId
|
||||
gameMsg.ClientSeq = protoMsg.HeadMessage.ClientSequenceId
|
||||
gameMsg.PayloadMessage = protoMsg.PayloadMessage
|
||||
k.messageQueue.SendToGs(session.gsServerAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
EventId: mq.NormalMsg,
|
||||
|
||||
Reference in New Issue
Block a user