mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 15:32:26 +08:00
服务器玩家在线信息同步
This commit is contained in:
@@ -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