网关性能优化

This commit is contained in:
flswld
2023-03-13 11:07:01 +08:00
parent ea069c705c
commit a14eea9bfb
16 changed files with 2452 additions and 2593 deletions

View File

@@ -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 {

View File

@@ -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,