game/server/match/server/service.go
2025-06-16 00:50:42 +08:00

121 lines
3.1 KiB
Go

package server
import (
"fmt"
"game/common/proto/pb"
"game/common/serviceName"
"game/common/userBindService"
"game/server/match/config"
"game/server/match/match"
"game/server/match/model"
"github.com/fox/fox/ipb"
"github.com/fox/fox/log"
"github.com/fox/fox/processor"
"github.com/fox/fox/service"
"github.com/golang/protobuf/proto"
)
var Services []*MatchService
type MatchService struct {
*service.NatsService
processor *processor.Processor
bindService *userBindService.UserBindService
matchMgr *match.MatchMgr
}
func Init() {
log.DebugF("init service begin id:%v, num:%v", config.Command.ServiceId, config.Command.ServiceNum)
for i := 0; i < config.Command.ServiceNum; i++ {
sid := config.Command.ServiceId + i
if srv := newService(sid); srv != nil {
Services = append(Services, srv)
}
}
}
func Stop() {
for _, srv := range Services {
log.DebugF("notify stop service %v", srv.Name())
srv.NotifyStop()
}
for _, srv := range Services {
srv.WaitStop()
}
}
func newService(serviceId int) *MatchService {
var err error
s := new(MatchService)
s.matchMgr = match.NewMatchMgr()
sName := fmt.Sprintf("%v-%d", serviceName.Match, serviceId)
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
EtcdAddress: config.Cfg.Etcd.Address,
EtcdUsername: "",
EtcdPassword: "",
NatsAddress: config.Cfg.Nats.Address,
ServiceType: serviceName.Match,
ServiceName: sName,
OnFunc: s,
TypeId: int(pb.ServiceTypeId_STI_Match),
Version: config.Cfg.BuildDate,
}); err != nil {
log.Fatal(err.Error())
return nil
}
s.bindService = userBindService.NewUserBindService(model.UserBindServiceRedis, s.ServiceEtcd())
s.processor = processor.NewProcessor()
s.initProcessor()
s.OnInit()
return s
}
func (s *MatchService) OnInit() {
// if err := s.NatsService.QueueSubscribe(service.GroupTopic(s), service.GroupQueue(s)); err != nil {
// log.Error(err.Error())
// }
s.NatsService.Run()
log.Debug("onInit")
}
func (s *MatchService) CanStop() bool {
return true
}
func (s *MatchService) OnStop() {
s.NatsService.OnStop()
log.Debug("OnStop")
}
// 处理其它服发送过来的消息
func (s *MatchService) OnMessage(data []byte) error {
var iMsg = &ipb.InternalMsg{}
var err error
if err = proto.Unmarshal(data, iMsg); err != nil {
log.Error(err.Error())
return err
}
if req, err := s.processor.Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
err = s.processor.Dispatch(iMsg.MsgId, iMsg, req)
} else {
log.Error(err.Error())
}
log.Debug(s.Log("received message:%v", pb.MsgId(iMsg.MsgId)))
return nil
}
// 向内部服务发送消息
func (s *MatchService) SendServiceData(gateName, topic string, connId uint32, userId int64, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(gateName, connId, userId, msgId, data)
_ = s.Send(topic, iMsg)
}
// 向内部服务发送消息
func (s *MatchService) SendServiceMsg(gateName, topic string, connId uint32, userId int64, msgId int32, msg proto.Message) {
log.DebugF("send to:%v msg id:%v, msg:%v", topic, pb.MsgId(msgId), msg.String())
data, _ := proto.Marshal(msg)
s.SendServiceData(gateName, topic, connId, userId, msgId, data)
}