优化GM服务器
This commit is contained in:
1 parent
00d32292ad
commit
bea91fbf73
12 files changed
+152
-37
No files matched your search
@@ -60,6 +60,7 @@ const (
|
||||
ServerForwardDispatchInfoNotify // 转发模式区服信息通知
|
||||
ServerStopNotify // 停服通知
|
||||
ServerDispatchCancelNotify // 服务器取消调度通知
|
||||
ServerGmCmdNotify // 服务器GM指令执行通知
|
||||
)
|
||||
|
||||
type ServerMsg struct {
|
||||
@@ -73,6 +74,8 @@ type ServerMsg struct {
|
||||
AddFriendInfo *AddFriendInfo
|
||||
ForwardDispatchInfo *ForwardDispatchInfo
|
||||
AppVersion string
|
||||
GmCmdFuncName string
|
||||
GmCmdParamList []string
|
||||
}
|
||||
|
||||
type OriginInfo struct {
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ func Run(ctx context.Context, configFile string) error {
|
||||
logger.CloseLogger()
|
||||
}()
|
||||
|
||||
messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, discoveryClient)
|
||||
messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, nil)
|
||||
defer messageQueue.Close()
|
||||
|
||||
db, err := dao.NewDao()
|
||||
|
||||
@@ -50,6 +50,14 @@ func NewController(db *dao.Dao, discovery *rpc.DiscoveryClient, messageQueue *mq
|
||||
}
|
||||
r.ec2b = ec2b
|
||||
r.messageQueue = messageQueue
|
||||
go func() {
|
||||
for {
|
||||
_, ok := <-r.messageQueue.GetNetMsg()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
r.gateServerMap = new(sync.Map)
|
||||
r.stopServerInfo = nil
|
||||
r.whiteList = nil
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ func Run(ctx context.Context, configFile string) error {
|
||||
logger.CloseLogger()
|
||||
}()
|
||||
|
||||
messageQueue := mq.NewMessageQueue(api.GATE, APPID, discoveryClient)
|
||||
messageQueue := mq.NewMessageQueue(api.GATE, APPID, nil)
|
||||
defer messageQueue.Close()
|
||||
|
||||
db, err := dao.NewDao()
|
||||
|
||||
+6
-1
@@ -7,8 +7,10 @@ import (
|
||||
"syscall"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/gm/controller"
|
||||
"hk4e/node/api"
|
||||
"hk4e/pkg/logger"
|
||||
)
|
||||
|
||||
@@ -27,7 +29,10 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = controller.NewController(discoveryClient)
|
||||
messageQueue := mq.NewMessageQueue(api.GM, "gm", nil)
|
||||
defer messageQueue.Close()
|
||||
|
||||
_ = controller.NewController(discoveryClient, messageQueue)
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
@@ -13,22 +16,62 @@ import (
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
gmClientMap map[uint32]*rpc.GMClient
|
||||
gmClientMapLock sync.RWMutex
|
||||
discoveryClient *rpc.DiscoveryClient
|
||||
gmClientMap map[uint32]*rpc.GMClient
|
||||
gmClientMapLock sync.RWMutex
|
||||
discoveryClient *rpc.DiscoveryClient
|
||||
messageQueue *mq.MessageQueue
|
||||
globalGsOnlineMap map[uint32]string // 全服玩家在线表
|
||||
globalGsOnlineMapLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewController(discoveryClient *rpc.DiscoveryClient) (r *Controller) {
|
||||
func NewController(discoveryClient *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.gmClientMap = make(map[uint32]*rpc.GMClient)
|
||||
r.discoveryClient = discoveryClient
|
||||
r.messageQueue = messageQueue
|
||||
go func() {
|
||||
for {
|
||||
_, ok := <-r.messageQueue.GetNetMsg()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
r.globalGsOnlineMap = make(map[uint32]string)
|
||||
r.syncGlobalGsOnlineMap()
|
||||
go r.autoSyncGlobalGsOnlineMap()
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
func (c *Controller) autoSyncGlobalGsOnlineMap() {
|
||||
ticker := time.NewTicker(time.Second * 60)
|
||||
for {
|
||||
<-ticker.C
|
||||
c.syncGlobalGsOnlineMap()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) syncGlobalGsOnlineMap() {
|
||||
rsp, err := c.discoveryClient.GetGlobalGsOnlineMap(context.TODO(), nil)
|
||||
if err != nil {
|
||||
logger.Error("get global gs online map error: %v", err)
|
||||
return
|
||||
}
|
||||
copyMap := make(map[uint32]string)
|
||||
for k, v := range rsp.OnlineMap {
|
||||
copyMap[k] = v
|
||||
}
|
||||
copyMapLen := len(copyMap)
|
||||
c.globalGsOnlineMapLock.Lock()
|
||||
c.globalGsOnlineMap = copyMap
|
||||
c.globalGsOnlineMapLock.Unlock()
|
||||
logger.Info("sync global gs online map finish, len: %v", copyMapLen)
|
||||
}
|
||||
|
||||
func (c *Controller) authorize() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
if true {
|
||||
if context.GetHeader("Auth") == "flswld" {
|
||||
// 验证通过
|
||||
context.Next()
|
||||
return
|
||||
@@ -55,6 +98,7 @@ func (c *Controller) registerRouter() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
engine.GET("/server/online/stats", c.serverOnlineStats)
|
||||
engine.Use(c.authorize())
|
||||
engine.POST("/gm/cmd", c.gmCmd)
|
||||
engine.GET("/server/stop/state", c.serverStopState)
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/gs/api"
|
||||
"hk4e/pkg/logger"
|
||||
@@ -14,6 +15,7 @@ type GmCmdReq struct {
|
||||
FuncName string `json:"func_name"`
|
||||
ParamList []string `json:"param_list"`
|
||||
GsId uint32 `json:"gs_id"`
|
||||
GsAppid string `json:"gs_appid"`
|
||||
}
|
||||
|
||||
func (c *Controller) gmCmd(ctx *gin.Context) {
|
||||
@@ -25,28 +27,53 @@ func (c *Controller) gmCmd(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
logger.Info("GmCmdReq: %v", gmCmdReq)
|
||||
c.gmClientMapLock.RLock()
|
||||
gmClient, exist := c.gmClientMap[gmCmdReq.GsId]
|
||||
c.gmClientMapLock.RUnlock()
|
||||
if !exist {
|
||||
var err error = nil
|
||||
gmClient, err = rpc.NewGMClient(gmCmdReq.GsId)
|
||||
if gmCmdReq.GsId != 0 {
|
||||
// 指定gsid
|
||||
c.gmClientMapLock.RLock()
|
||||
gmClient, exist := c.gmClientMap[gmCmdReq.GsId]
|
||||
c.gmClientMapLock.RUnlock()
|
||||
if !exist {
|
||||
var err error = nil
|
||||
gmClient, err = rpc.NewGMClient(gmCmdReq.GsId)
|
||||
if err != nil {
|
||||
logger.Error("new gm client error: %v", err)
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
c.gmClientMapLock.Lock()
|
||||
c.gmClientMap[gmCmdReq.GsId] = gmClient
|
||||
c.gmClientMapLock.Unlock()
|
||||
}
|
||||
rsp, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
ParamList: gmCmdReq.ParamList,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("new gm client error: %v", err)
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
c.gmClientMapLock.Lock()
|
||||
c.gmClientMap[gmCmdReq.GsId] = gmClient
|
||||
c.gmClientMapLock.Unlock()
|
||||
ctx.JSON(http.StatusOK, rsp)
|
||||
} else if gmCmdReq.GsAppid != "" {
|
||||
// 指定gsappid
|
||||
c.messageQueue.SendToGs(gmCmdReq.GsAppid, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerGmCmdNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
GmCmdFuncName: gmCmdReq.FuncName,
|
||||
GmCmdParamList: gmCmdReq.ParamList,
|
||||
},
|
||||
})
|
||||
ctx.JSON(http.StatusOK, nil)
|
||||
} else {
|
||||
// 全服
|
||||
c.messageQueue.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerGmCmdNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
GmCmdFuncName: gmCmdReq.FuncName,
|
||||
GmCmdParamList: gmCmdReq.ParamList,
|
||||
},
|
||||
})
|
||||
ctx.JSON(http.StatusOK, nil)
|
||||
}
|
||||
rep, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
ParamList: gmCmdReq.ParamList,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, rep)
|
||||
}
|
||||
@@ -122,3 +122,15 @@ func (c *Controller) serverDispatchCancel(ctx *gin.Context) {
|
||||
}
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: nil})
|
||||
}
|
||||
|
||||
type ServerOnlineStats struct {
|
||||
TotalOnlinePlayerNum uint32 `json:"total_online_player_num"`
|
||||
}
|
||||
|
||||
func (c *Controller) serverOnlineStats(ctx *gin.Context) {
|
||||
num := 0
|
||||
c.globalGsOnlineMapLock.RLock()
|
||||
num = len(c.globalGsOnlineMap)
|
||||
c.globalGsOnlineMapLock.RUnlock()
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: &ServerOnlineStats{TotalOnlinePlayerNum: uint32(num)}})
|
||||
}
|
||||
+2
-1
@@ -5,6 +5,7 @@ import (
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -66,7 +67,7 @@ func Run(ctx context.Context, configFile string) error {
|
||||
})
|
||||
}()
|
||||
|
||||
logger.InitLogger("gs_" + APPID)
|
||||
logger.InitLogger("gs_" + strconv.Itoa(int(GSID)) + "_" + APPID)
|
||||
logger.Warn("gs start, appid: %v, gsid: %v", APPID, GSID)
|
||||
defer func() {
|
||||
logger.CloseLogger()
|
||||
|
||||
@@ -249,7 +249,8 @@ func (p *PluginPubg) GlobalTickPubg() {
|
||||
// GlobalTickMinuteChange 定时开启pubg游戏
|
||||
func (p *PluginPubg) GlobalTickMinuteChange() {
|
||||
minute := time.Now().Minute()
|
||||
startMinute := GAME.GetGsId() % 6 * 10
|
||||
roomNumber := GAME.GetGsId() - 1
|
||||
startMinute := roomNumber % 6 * 10
|
||||
if uint32(minute) == startMinute {
|
||||
p.StartPubg()
|
||||
}
|
||||
@@ -268,6 +269,10 @@ func (p *PluginPubg) UserTimerPubgEnd(player *model.Player, data []any) {
|
||||
newWorld := WORLD_MANAGER.CreateWorld(player)
|
||||
GAME.WorldAddPlayer(newWorld, player)
|
||||
WORLD_MANAGER.InitAiWorld(player)
|
||||
roomNumber := GAME.GetGsId() - 1
|
||||
startMinute := roomNumber % 6 * 10
|
||||
info := fmt.Sprintf("下一次游戏开启时间:%02d:%02d。", time.Now().Add(time.Hour).Hour(), startMinute)
|
||||
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
|
||||
}
|
||||
|
||||
// UserTimerPubgUpdateArea 更新游戏区域
|
||||
@@ -375,6 +380,8 @@ func (p *PluginPubg) StartPubg() {
|
||||
})
|
||||
p.playerHitTimeMap[player.PlayerId] = 0
|
||||
}
|
||||
info := "游戏开始。"
|
||||
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
|
||||
}
|
||||
|
||||
// StopPubg 结束pubg游戏
|
||||
@@ -397,6 +404,8 @@ func (p *PluginPubg) StopPubg() {
|
||||
p.entityIdWorldGadgetIdMap = make(map[uint32]int32)
|
||||
p.playerHitTimeMap = make(map[uint32]int64)
|
||||
p.CreateUserTimer(world.GetOwner().PlayerId, 60, p.UserTimerPubgEnd)
|
||||
info := "游戏结束。"
|
||||
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
|
||||
}
|
||||
|
||||
// IsStartPubg pubg游戏是否开启
|
||||
|
||||
@@ -212,6 +212,13 @@ func (r *RouteManager) RouteHandle(netMsg *mq.NetMsg) {
|
||||
GAME.ServerStopNotify()
|
||||
case mq.ServerDispatchCancelNotify:
|
||||
GAME.ServerDispatchCancelNotify(serverMsg.AppVersion)
|
||||
case mq.ServerGmCmdNotify:
|
||||
commandTextInput := COMMAND_MANAGER.GetCommandMessageInput()
|
||||
commandTextInput <- &CommandMessage{
|
||||
GMType: SystemFuncGM,
|
||||
FuncName: serverMsg.GmCmdFuncName,
|
||||
ParamList: serverMsg.GmCmdParamList,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package api
|
||||
|
||||
const (
|
||||
NODE = "NODE"
|
||||
DISPATCH = "DISPATCH"
|
||||
GATE = "GATE"
|
||||
GS = "GS"
|
||||
ANTICHEAT = "ANTICHEAT"
|
||||
PATHFINDING = "PATHFINDING"
|
||||
MULTI = "MULTI"
|
||||
ROBOT = "ROBOT"
|
||||
NODE = "NODE"
|
||||
DISPATCH = "DISPATCH"
|
||||
GATE = "GATE"
|
||||
GS = "GS"
|
||||
MULTI = "MULTI"
|
||||
GM = "GM"
|
||||
ROBOT = "ROBOT"
|
||||
)
|
||||
Reference in new issue
Block a user