从game server继承

This commit is contained in:
liuxiaobo 2025-06-17 10:06:58 +08:00
parent 28f68495ec
commit 1fdbe979ab
5 changed files with 35 additions and 131 deletions

View File

@ -2,14 +2,12 @@ package server
import (
"fmt"
"game/common/gameService"
"game/common/proto/pb"
"game/common/serviceName"
"game/common/userBindService"
"game/server/chat/config"
"game/server/chat/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"
)
@ -17,9 +15,7 @@ import (
var Chat []*ChatService
type ChatService struct {
*service.NatsService
processor *processor.Processor
bindService *userBindService.UserBindService
*gameService.GameService
}
func Init() {
@ -60,8 +56,6 @@ func newChatService(serviceId int) *ChatService {
return nil
}
s.bindService = userBindService.NewUserBindService(model.UserRedis, s.ServiceEtcd())
s.processor = processor.NewProcessor()
s.initProcessor()
s.OnInit()
return s
@ -93,23 +87,11 @@ func (s *ChatService) OnMessage(data []byte) error {
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.UserId, req)
if req, err := s.Processor().Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
err = s.Processor().Dispatch(iMsg.MsgId, iMsg.UserId, req)
} else {
log.Error(err.Error())
}
//log.Debug(s.Log("received message:%v", iMsg.MsgId))
return nil
}
// 向内部服务发送消息
func (s *ChatService) SendServiceData(topic string, userId int64, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(s.Name(), 0, userId, msgId, data)
_ = s.Send(topic, iMsg)
}
// 向内部服务发送消息
func (s *ChatService) SendServiceMsg(topic string, userId int64, msgId int32, msg proto.Message) {
data, _ := proto.Marshal(msg)
s.SendServiceData(topic, userId, msgId, data)
}

View File

@ -2,15 +2,13 @@ package server
import (
"fmt"
"game/common/gameService"
"game/common/proto/pb"
"game/common/serviceName"
"game/common/topicName"
"game/common/userBindService"
"game/server/gate/config"
"game/server/gate/model"
"github.com/fox/fox/ipb"
"github.com/fox/fox/log"
"github.com/fox/fox/processor"
"github.com/fox/fox/service"
"github.com/fox/fox/ws"
"github.com/golang/protobuf/proto"
@ -20,10 +18,8 @@ import (
var Gates []*GateService
type GateService struct {
*service.NatsService
*gameService.GameService
wss *ws.WsServer
processor *processor.Processor
bindService *userBindService.UserBindService
}
func Init() {
@ -45,11 +41,10 @@ func Stop() {
}
func newGateService(serviceId int) *GateService {
var err error
s := new(GateService)
sName := fmt.Sprintf("%v-%d", serviceName.Gate, serviceId)
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
s.GameService = gameService.NewGameService(&service.InitNatsServiceParams{
EtcdAddress: config.Cfg.Etcd.Address,
EtcdUsername: "",
EtcdPassword: "",
@ -59,10 +54,7 @@ func newGateService(serviceId int) *GateService {
OnFunc: s,
TypeId: int(pb.ServiceTypeId_STI_Gate),
Version: config.Cfg.BuildDate,
}); err != nil {
log.Fatal(err.Error())
return nil
}
})
addressPos := serviceId - config.Command.ServiceId
if len(config.GateCfg.Address) <= addressPos {
@ -71,9 +63,6 @@ func newGateService(serviceId int) *GateService {
}
wsAddress := config.GateCfg.Address[addressPos]
s.wss = ws.NewWsServer(wsAddress, s.WsOnMessage, s.WsOnDisconnect)
s.bindService = userBindService.NewUserBindService(model.UserBindServiceRedis, s.ServiceEtcd())
s.processor = processor.NewProcessor()
s.initProcessor()
s.OnInit()
return s
@ -126,8 +115,8 @@ func (s *GateService) findConn(msg *ipb.InternalMsg) ws.IConn {
// 处理消息
func (s *GateService) connMessage(conn ws.IConn, iMsg *ipb.InternalMsg) {
if req, err := s.processor.Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
err = s.processor.Dispatch(iMsg.MsgId, iMsg, conn, req)
if req, err := s.Processor().Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
err = s.Processor().Dispatch(iMsg.MsgId, iMsg, conn, req)
} else {
s.SendClientData(conn, iMsg.ServiceName, iMsg.MsgId, iMsg.Msg)
}
@ -166,7 +155,7 @@ func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
if msg.ServiceName != "" {
topic = service.TopicEx(msg.ServiceName)
} else {
if node, err := s.bindService.FindServiceNode(msg.ServiceTid, conn.UserId()); err == nil {
if node, err := s.BindService().FindServiceNode(msg.ServiceTid, conn.UserId()); err == nil {
topic, msg.ServiceName = service.TopicEx(node.Name), node.Name
} else {
log.ErrorF("uid:%v 查找节点:%v 失败:%v", conn.UserId(), msg.ServiceTid, err.Error())
@ -174,6 +163,7 @@ func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
}
if topic != "" {
if msg.MsgId == int32(pb.MsgId_ReqUserLoginId) {
// 加入登陆ip
req := &pb.ReqUserLogin{}
_ = proto.Unmarshal(msg.Data, req)
req.Ip = conn.Addr()
@ -189,7 +179,7 @@ func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
// 向内部服务发送消息
func (s *GateService) SendServiceData(topic string, conn ws.IConn, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(s.Name(), conn.Id(), conn.UserId(), msgId, data)
_ = s.Send(topic, iMsg)
_ = s.SendByTopic(topic, iMsg)
}
// 向内部服务发送消息
@ -215,11 +205,11 @@ func (s *GateService) SendClientMsg(conn ws.IConn, serviceName string, msgId int
func (s *GateService) WsOnDisconnect(conn ws.IConn) {
if conn.UserId() > 0 {
sName := s.bindService.LoadFromRedis(conn.UserId(), pb.ServiceTypeId_STI_Gate)
sName := s.BindService().LoadFromRedis(conn.UserId(), pb.ServiceTypeId_STI_Gate)
// 相同网关则为正常下线删除redis信息然后向内网广播下线
// 不同网关,异地登陆顶号 由其它网关通知本网关向玩家发送顶号消息(processor中处理),并主动关闭连接,不广播下线消息
if sName == s.Name() {
s.bindService.DelUserService(conn.UserId(), pb.ServiceTypeId_STI_Gate)
s.BindService().DelUserService(conn.UserId(), pb.ServiceTypeId_STI_Gate)
s.SendServiceMsg(topicName.UserOffline, conn, int32(pb.MsgId_NtfUserOfflineId), &pb.NtfUserOffline{UserId: conn.UserId()})
}
log.Debug(s.Log("user:%v disconnect", conn.UserId()))

View File

@ -2,14 +2,12 @@ package server
import (
"fmt"
"game/common/gameService"
"game/common/proto/pb"
"game/common/serviceName"
"game/common/userBindService"
"game/server/lobby/config"
"game/server/lobby/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"
)
@ -18,9 +16,7 @@ import (
var lobby []*LobbyService
type LobbyService struct {
*service.NatsService
processor *processor.Processor
bindService *userBindService.UserBindService
*gameService.GameService
}
func Init() {
@ -42,11 +38,10 @@ func Stop() {
}
func newLobbyService(serviceId int) *LobbyService {
var err error
s := new(LobbyService)
sName := fmt.Sprintf("%v-%d", serviceName.Lobby, serviceId)
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
s.GameService = gameService.NewGameService(&service.InitNatsServiceParams{
EtcdAddress: config.Cfg.Etcd.Address,
EtcdUsername: "",
EtcdPassword: "",
@ -56,13 +51,8 @@ func newLobbyService(serviceId int) *LobbyService {
OnFunc: s,
TypeId: int(pb.ServiceTypeId_STI_Lobby),
Version: config.Cfg.BuildDate,
}); err != nil {
log.Fatal(err.Error())
return nil
}
})
s.bindService = userBindService.NewUserBindService(model.UserRedis, s.ServiceEtcd())
s.processor = processor.NewProcessor()
s.initProcessor()
s.OnInit()
return s
@ -94,23 +84,11 @@ func (s *LobbyService) OnMessage(data []byte) error {
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)
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", iMsg.MsgId))
return nil
}
// 向内部服务发送消息
func (s *LobbyService) SendServiceData(topic string, userId int64, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(s.Name(), 0, userId, msgId, data)
_ = s.Send(topic, iMsg)
}
// 向内部服务发送消息
func (s *LobbyService) SendServiceMsg(topic string, userId int64, msgId int32, msg proto.Message) {
data, _ := proto.Marshal(msg)
s.SendServiceData(topic, userId, msgId, data)
}

View File

@ -2,14 +2,12 @@ package server
import (
"fmt"
"game/common/gameService"
"game/common/proto/pb"
"game/common/serviceName"
"game/common/userBindService"
"game/server/login/config"
"game/server/login/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"
)
@ -17,9 +15,7 @@ import (
var Login []*LoginService
type LoginService struct {
*service.NatsService
processor *processor.Processor
bindService *userBindService.UserBindService
*gameService.GameService
}
func Init() {
@ -43,11 +39,10 @@ func Stop() {
}
func newLoginService(serviceId int) *LoginService {
var err error
s := new(LoginService)
sName := fmt.Sprintf("%v-%d", serviceName.Login, serviceId)
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
s.GameService = gameService.NewGameService(&service.InitNatsServiceParams{
EtcdAddress: config.Cfg.Etcd.Address,
EtcdUsername: "",
EtcdPassword: "",
@ -57,13 +52,8 @@ func newLoginService(serviceId int) *LoginService {
OnFunc: s,
TypeId: int(pb.ServiceTypeId_STI_Login),
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
@ -94,24 +84,11 @@ func (s *LoginService) OnMessage(data []byte) error {
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)
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 *LoginService) SendServiceData(topic string, connId uint32, userId int64, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(s.Name(), connId, userId, msgId, data)
_ = s.Send(topic, iMsg)
}
// 向内部服务发送消息
func (s *LoginService) SendServiceMsg(topic string, connId uint32, userId int64, msgId int32, msg proto.Message) {
log.DebugF("send to:%v msg id:%v, msg:%v", topic, msgId, msg.String())
data, _ := proto.Marshal(msg)
s.SendServiceData(topic, connId, userId, msgId, data)
}

View File

@ -2,15 +2,13 @@ package server
import (
"fmt"
"game/common/gameService"
"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"
)
@ -18,9 +16,7 @@ import (
var Services []*MatchService
type MatchService struct {
*service.NatsService
processor *processor.Processor
bindService *userBindService.UserBindService
*gameService.GameService
matchMgr *match.MatchMgr
}
@ -45,12 +41,11 @@ func Stop() {
}
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{
s.GameService = gameService.NewGameService(&service.InitNatsServiceParams{
EtcdAddress: config.Cfg.Etcd.Address,
EtcdUsername: "",
EtcdPassword: "",
@ -60,13 +55,8 @@ func newService(serviceId int) *MatchService {
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
@ -97,24 +87,11 @@ func (s *MatchService) OnMessage(data []byte) error {
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)
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)
}