From b3902bfafe1c3d116c913a6a877ff79be153985b Mon Sep 17 00:00:00 2001 From: liuxiaobo <1224730913@qq.com> Date: Thu, 29 May 2025 17:54:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0client=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/client/cmd/cmd.go | 29 ++++++++++ test/client/config/config.go | 41 ++++++++++++++ test/client/main.go | 24 ++++++++ test/client/model/db.go | 19 +++++++ test/client/server/processor.go | 30 ++++++++++ test/client/server/service.go | 98 +++++++++++++++++++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 test/client/cmd/cmd.go create mode 100644 test/client/config/config.go create mode 100644 test/client/main.go create mode 100644 test/client/model/db.go create mode 100644 test/client/server/processor.go create mode 100644 test/client/server/service.go diff --git a/test/client/cmd/cmd.go b/test/client/cmd/cmd.go new file mode 100644 index 0000000..c26573b --- /dev/null +++ b/test/client/cmd/cmd.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "fmt" + "game/test/client/config" + "game/test/client/server" + "github.com/fox/fox/log" + "os" + "os/signal" + "syscall" +) + +func initRepo() { + //model.InitRedis() +} + +func Run(GitCommit, GitBranch, BuildDate string) { + config.LoadConfig(GitCommit, GitBranch, BuildDate) + log.Info(fmt.Sprintf("版本分支:%v,hash值:%v,编译时间:%v", GitBranch, GitCommit, BuildDate)) + initRepo() + + server.Init() + // 截获 SIGINT 和 SIGTERM 信号 + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + sig := <-c + server.Stop() + log.Info(fmt.Sprintf("received %s, initiating shutdown...", sig)) +} diff --git a/test/client/config/config.go b/test/client/config/config.go new file mode 100644 index 0000000..a2b12dd --- /dev/null +++ b/test/client/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "game/common/config" + "github.com/fox/fox/db" + "github.com/fox/fox/log" +) + +const ( + gateAddress1 = "114.132.124.145:5100" + gateAddress2 = "114.132.124.145:5101" +) + +var Command *config.Command +var Cfg *config.Common[ClientConfig] + +type ClientConfig struct { + Address []string `json:"address"` // 网关地址 +} + +func initLog() { + log.Open("client.log", log.DebugL) +} + +func LoadConfig(GitCommit, GitBranch, BuildDate string) { + Command = config.ParseCommand() + initLog() + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + if err != nil { + log.Error(err.Error()) + return + } + defer func() { _ = rdb.Close() }() + Cfg, err = config.LoadCommonConfig[ClientConfig](rdb, GitCommit, GitBranch, BuildDate) + if err != nil { + log.Error(err.Error()) + return + } + Cfg.Special.Address = append(Cfg.Special.Address, gateAddress1, gateAddress2) + log.DebugF("load common config success") +} diff --git a/test/client/main.go b/test/client/main.go new file mode 100644 index 0000000..70facb4 --- /dev/null +++ b/test/client/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "game/test/client/cmd" + "github.com/fox/fox/ksync" + "github.com/fox/fox/log" + "time" +) + +var ( + GitCommit = "unknown" + GitBranch = "unknown" + BuildDate = "unknown" +) + +func main() { + tm, err := time.Parse("20060102150405", BuildDate) + if err == nil { + BuildDate = tm.Format("2006-01-02 15:04:05") + } + ksync.RunSafe(func() { + cmd.Run(GitBranch, GitCommit, BuildDate) + }, func() { log.ErrorF("reset run") }) +} diff --git a/test/client/model/db.go b/test/client/model/db.go new file mode 100644 index 0000000..03f16ca --- /dev/null +++ b/test/client/model/db.go @@ -0,0 +1,19 @@ +package model + +import ( + "game/test/client/config" + "github.com/fox/fox/db" + "github.com/fox/fox/log" + "github.com/go-redis/redis/v8" +) + +var UserRedis *redis.Client +var err error + +func InitRedis() { + UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0) + if err != nil { + log.Fatal(err.Error()) + return + } +} diff --git a/test/client/server/processor.go b/test/client/server/processor.go new file mode 100644 index 0000000..b208673 --- /dev/null +++ b/test/client/server/processor.go @@ -0,0 +1,30 @@ +package server + +import ( + "game/common/proto/pb" + "github.com/fox/fox/processor" +) + +func (s *ClientService) initProcessor() { + s.processor.RegisterMessages(processor.RegisterMetas{ + pb.MsgId_C2SChatId: {pb.C2SChat{}, s.onChat}, + }) +} + +// 收到登陆成功消息,判断是否顶号 +func (s *ClientService) onChat(uid int64, msg *pb.C2SChat) { + _ = uid + _ = msg + //switch msg.Type { + //case pb.ChatType_CT_Private: + // sName, err := s.bindService.FindServiceName(msg.DstUser.UserId, pb.ServiceTypeId_STI_Gate) + // if err != nil { + // log.DebugF("find user:%v in gate err: %v", uid, err) + // return + // } + // s.SendServiceMsg(service.TopicEx(sName), msg.DstUser.UserId, int32(pb.MsgId_S2CChatId), msg) + //default: + // s.SendServiceMsg(service.TopicEx(topicName.WorldMessage), uid, int32(pb.MsgId_S2CChatId), msg) + //} + +} diff --git a/test/client/server/service.go b/test/client/server/service.go new file mode 100644 index 0000000..85eb49a --- /dev/null +++ b/test/client/server/service.go @@ -0,0 +1,98 @@ +package server + +import ( + "fmt" + "game/common/proto/pb" + "game/test/client/config" + "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" +) + +var Chat []*ClientService + +type ClientService struct { + *service.BaseService + client *ws.Client + processor *processor.Processor +} + +func Init() { + for i := 0; i < config.Command.ServiceNum; i++ { + sid := config.Command.ServiceId + i + if srv := newClientService(sid); srv != nil { + Chat = append(Chat, srv) + } + } +} + +func Stop() { + for _, srv := range Chat { + srv.NotifyStop() + } + for _, srv := range Chat { + srv.WaitStop() + } +} + +func newClientService(serviceId int) *ClientService { + var err error + s := new(ClientService) + + sName := fmt.Sprintf("%v-%d", "client", serviceId) + s.BaseService = service.NewBaseService("client", sName, s, s) + size := len(config.Cfg.Special.Address) + addr := config.Cfg.Special.Address[serviceId%size] + if s.client, err = ws.NewClient(fmt.Sprintf("ws://%v", addr), s); err != nil { + return nil + } + s.processor = processor.NewProcessor() + s.initProcessor() + s.OnInit() + return s +} + +func (s *ClientService) OnInit() { + s.client.Start() + s.BaseService.Run() + log.Debug("onInit") +} + +func (s *ClientService) CanStop() bool { + return true +} + +func (s *ClientService) OnStop() { + s.client.Stop() + log.Debug("OnStop") +} + +// 处理其它服发送过来的消息 +func (s *ClientService) OnMessage(data []byte) error { + var cMsg = &pb.ClientMsg{} + var err error + if err = proto.Unmarshal(data, cMsg); err != nil { + log.Error(err.Error()) + return err + } + if req, err := s.processor.Unmarshal(cMsg.MsgId, cMsg.Data); err == nil { + err = s.processor.Dispatch(cMsg.MsgId, cMsg.UserId, cMsg, req) + } + //log.Debug(s.Log("on message:%v", string(cMsg))) + return nil +} + +// 向内部服务发送消息 +func (s *ClientService) SendServiceData(topic string, userId int64, msgId int32, data []byte) { + iMsg := ipb.MakeMsg(s.Name(), 0, userId, msgId, data) + _ = s.Send(topic, iMsg) +} + +// 向内部服务发送消息 +func (s *ClientService) SendServiceMsg(topic string, userId int64, msgId int32, msg proto.Message) { + data, _ := proto.Marshal(msg) + s.SendServiceData(topic, userId, msgId, data) +}