mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
服务器玩家在线信息同步
This commit is contained in:
@@ -21,6 +21,8 @@ service Discovery {
|
||||
rpc GetAllGateServerInfoList (NullMsg) returns (GateServerInfoList) {}
|
||||
// 获取主游戏服务器的appid
|
||||
rpc GetMainGameServerAppId (NullMsg) returns (GetMainGameServerAppIdRsp) {}
|
||||
// 获取全服玩家GS在线列表
|
||||
rpc GetGlobalGsOnlineMap (NullMsg) returns (GetGlobalGsOnlineMapRsp) {}
|
||||
}
|
||||
|
||||
message NullMsg {
|
||||
@@ -84,3 +86,7 @@ message GateServerInfo {
|
||||
message GateServerInfoList {
|
||||
repeated GateServerInfo gate_server_info_list = 1;
|
||||
}
|
||||
|
||||
message GetGlobalGsOnlineMapRsp {
|
||||
map<uint32, string> GlobalGsOnlineMap = 1;
|
||||
}
|
||||
|
||||
@@ -5,4 +5,5 @@ const (
|
||||
GS = "GS"
|
||||
ANTICHEAT = "ANTICHEAT"
|
||||
PATHFINDING = "PATHFINDING"
|
||||
NODE = "NODE"
|
||||
)
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"syscall"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/node/api"
|
||||
"hk4e/node/service"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
@@ -30,7 +32,12 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
s, err := service.NewService(conn)
|
||||
|
||||
// 只用来监听全服广播
|
||||
messageQueue := mq.NewMessageQueue(api.NODE, "node", nil)
|
||||
defer messageQueue.Close()
|
||||
|
||||
s, err := service.NewService(conn, messageQueue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -50,9 +50,11 @@ type ServerInstance struct {
|
||||
}
|
||||
|
||||
type DiscoveryService struct {
|
||||
regionEc2b *random.Ec2b // 全局区服密钥信息
|
||||
serverInstanceMap map[string]*sync.Map // 全部服务器实例集合 key:服务器类型 value:服务器实例集合 -> key:appid value:服务器实例
|
||||
serverAppIdMap *sync.Map // 服务器appid集合 key:appid value:是否存在
|
||||
regionEc2b *random.Ec2b // 全局区服密钥信息
|
||||
serverInstanceMap map[string]*sync.Map // 全部服务器实例集合 key:服务器类型 value:服务器实例集合 -> key:appid value:服务器实例
|
||||
serverAppIdMap *sync.Map // 服务器appid集合 key:appid value:是否存在
|
||||
globalGsOnlineMap map[uint32]string
|
||||
globalGsOnlineMapLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewDiscoveryService() *DiscoveryService {
|
||||
@@ -65,6 +67,7 @@ func NewDiscoveryService() *DiscoveryService {
|
||||
r.serverInstanceMap[api.ANTICHEAT] = new(sync.Map)
|
||||
r.serverInstanceMap[api.PATHFINDING] = new(sync.Map)
|
||||
r.serverAppIdMap = new(sync.Map)
|
||||
r.globalGsOnlineMap = make(map[uint32]string)
|
||||
go r.removeDeadServer()
|
||||
return r
|
||||
}
|
||||
@@ -281,6 +284,19 @@ func (s *DiscoveryService) GetMainGameServerAppId(ctx context.Context, req *api.
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetGlobalGsOnlineMap 获取全服玩家GS在线列表
|
||||
func (s *DiscoveryService) GetGlobalGsOnlineMap(ctx context.Context, req *api.NullMsg) (*api.GetGlobalGsOnlineMapRsp, error) {
|
||||
copyMap := make(map[uint32]string)
|
||||
s.globalGsOnlineMapLock.RLock()
|
||||
for k, v := range s.globalGsOnlineMap {
|
||||
copyMap[k] = v
|
||||
}
|
||||
s.globalGsOnlineMapLock.RUnlock()
|
||||
return &api.GetGlobalGsOnlineMapRsp{
|
||||
GlobalGsOnlineMap: copyMap,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *DiscoveryService) getRandomServerInstance(instMap *sync.Map) *ServerInstance {
|
||||
instList := make(ServerInstanceSortList, 0)
|
||||
instMap.Range(func(key, value any) bool {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"hk4e/common/mq"
|
||||
"hk4e/node/api"
|
||||
|
||||
"github.com/byebyebruce/natsrpc"
|
||||
@@ -9,9 +10,11 @@ import (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
messageQueue *mq.MessageQueue
|
||||
discoveryService *DiscoveryService
|
||||
}
|
||||
|
||||
func NewService(conn *nats.Conn) (*Service, error) {
|
||||
func NewService(conn *nats.Conn, messageQueue *mq.MessageQueue) (*Service, error) {
|
||||
enc, err := nats.NewEncodedConn(conn, protobuf.PROTOBUF_ENCODER)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -25,8 +28,36 @@ func NewService(conn *nats.Conn) (*Service, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Service{}, nil
|
||||
s := &Service{
|
||||
messageQueue: messageQueue,
|
||||
discoveryService: discoveryService,
|
||||
}
|
||||
go s.BroadcastReceiver()
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Service) Close() {
|
||||
}
|
||||
|
||||
func (s *Service) BroadcastReceiver() {
|
||||
for {
|
||||
netMsg := <-s.messageQueue.GetNetMsg()
|
||||
if netMsg.MsgType != mq.MsgTypeServer {
|
||||
continue
|
||||
}
|
||||
if netMsg.EventId != mq.ServerUserOnlineStateChangeNotify {
|
||||
continue
|
||||
}
|
||||
if netMsg.OriginServerType != api.GS {
|
||||
continue
|
||||
}
|
||||
serverMsg := netMsg.ServerMsg
|
||||
s.discoveryService.globalGsOnlineMapLock.Lock()
|
||||
if serverMsg.IsOnline {
|
||||
s.discoveryService.globalGsOnlineMap[serverMsg.UserId] = netMsg.OriginServerAppId
|
||||
} else {
|
||||
delete(s.discoveryService.globalGsOnlineMap, serverMsg.UserId)
|
||||
}
|
||||
s.discoveryService.globalGsOnlineMapLock.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user