From 900cededc5cb8f52f19083bc45ad6d7024accd8f Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 21 Jun 2022 14:54:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=B6=88=E6=81=AF=E5=88=86?= =?UTF-8?q?=E6=B3=95=E9=85=8D=E7=BD=AE=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/json/game_msgdistrib.json | 1 + modules/gateway/agent.go | 28 ++++++++----- modules/gateway/configure_comp.go | 44 ++++++++++++++++++++ modules/gateway/core.go | 1 + modules/gateway/module.go | 7 ++++ sys/configure/structs/game.msgDistrib.go | 42 +++++++++++++++++++ sys/configure/structs/game.msgDistribData.go | 29 +++++++++++++ 7 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 bin/json/game_msgdistrib.json create mode 100644 modules/gateway/configure_comp.go create mode 100644 sys/configure/structs/game.msgDistrib.go create mode 100644 sys/configure/structs/game.msgDistribData.go diff --git a/bin/json/game_msgdistrib.json b/bin/json/game_msgdistrib.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/bin/json/game_msgdistrib.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index bb423bfd4..8ed506fe5 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -43,15 +43,15 @@ func newAgent(gateway IGateway, conn *websocket.Conn) *Agent { //用户代理 type Agent struct { - gateway IGateway - wsConn *websocket.Conn - sessionId string - uId string - wId string - writeChan chan *pb.UserMessage - closeSignal chan bool - state int32 //状态 0 关闭 1 运行 2 关闭中 - wg sync.WaitGroup + gateway IGateway + wsConn *websocket.Conn + sessionId string + uId string + wId string + writeChan chan *pb.UserMessage + closeSignal chan bool + state int32 //状态 0 关闭 1 运行 2 关闭中 + wg sync.WaitGroup } func (this *Agent) readLoop() { @@ -207,7 +207,15 @@ func (this *Agent) Close() { func (this *Agent) messageDistribution(msg *pb.UserMessage) error { reply := &pb.RPCMessageReply{} log.Debugf("agent:%s uId:%s MessageDistribution msg:%s.%s", this.sessionId, this.uId, msg.MainType, msg.SubType) - if err := this.gateway.Service().RpcCall(context.Background(), comm.Service_Worker, string(comm.Rpc_GatewayRoute), &pb.AgentMessage{ + servicePath := comm.Service_Worker + if rule, ok := this.gateway.GetMsgDistribRule(msg.MainType, msg.SubType); ok { + servicePath = rule + } else { + if len(this.wId) > 0 { + servicePath = fmt.Sprintf("%s/%s", comm.Service_Worker, this.wId) + } + } + if err := this.gateway.Service().RpcCall(context.Background(), servicePath, string(comm.Rpc_GatewayRoute), &pb.AgentMessage{ Ip: this.IP(), UserSessionId: this.sessionId, UserId: this.uId, diff --git a/modules/gateway/configure_comp.go b/modules/gateway/configure_comp.go new file mode 100644 index 000000000..bd31932f8 --- /dev/null +++ b/modules/gateway/configure_comp.go @@ -0,0 +1,44 @@ +package gateway + +import ( + "go_dreamfactory/modules" + cfg "go_dreamfactory/sys/configure/structs" + + "go_dreamfactory/lego/core" +) + +const ( + game_msgdistrib = "game_msgdistrib.json" +) + +///背包配置管理组件 +type Configure_Comp struct { + modules.MComp_Configure +} + +//组件初始化接口 +func (this *Configure_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.ModuleCompBase.Init(service, module, comp, options) + this.LoadConfigure(game_msgdistrib, cfg.NewGame_msgDistrib) + return +} + +//获取消息分发规则读取配置表 +func (this *Configure_Comp) GetMsgDistribRule(mtype, stype string) (rule string, ok bool) { + var ( + err error + v interface{} + ) + if v, err = this.GetConfigure(game_msgdistrib); err != nil { + return + } else { + for _, v := range v.(*cfg.Game_msgDistrib).GetDataMap() { + if v.Mtype == mtype && v.Stype == stype { + rule = v.Routrules + ok = true + return + } + } + } + return +} diff --git a/modules/gateway/core.go b/modules/gateway/core.go index d60a6423f..44fc74aae 100644 --- a/modules/gateway/core.go +++ b/modules/gateway/core.go @@ -24,5 +24,6 @@ type ( Service() base.IRPCXService Connect(a IAgent) DisConnect(a IAgent) + GetMsgDistribRule(mtype, stype string) (rule string, ok bool) } ) diff --git a/modules/gateway/module.go b/modules/gateway/module.go index c496fbf31..ec6f45569 100644 --- a/modules/gateway/module.go +++ b/modules/gateway/module.go @@ -24,6 +24,7 @@ type Gateway struct { service base.IRPCXService wsservice_comp *WSService_Comp //websocket 服务组件 提供websocket服务监听 agentmgr_comp *AgentMgr_Comp //用户代理对象管理组件 管理用户socekt对象 + configure_comp *Configure_Comp } //模块名 @@ -71,6 +72,7 @@ func (this *Gateway) OnInstallComp() { this.ModuleBase.OnInstallComp() this.agentmgr_comp = this.RegisterComp(new(AgentMgr_Comp)).(*AgentMgr_Comp) this.wsservice_comp = this.RegisterComp(new(WSService_Comp)).(*WSService_Comp) + this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } //有新的连接对象进入 @@ -84,3 +86,8 @@ func (this *Gateway) DisConnect(a IAgent) { log.Debugf("[Module.Gateway] have disConnect:Ip[%s] SessionId:[%s] uid:[%s]", a.IP(), a.SessionId(), a.UserId()) this.agentmgr_comp.DisConnect(a) } + +//读取消息分发规则 +func (this *Gateway) GetMsgDistribRule(mtype, stype string) (rule string, ok bool) { + return this.configure_comp.GetMsgDistribRule(mtype, stype) +} diff --git a/sys/configure/structs/game.msgDistrib.go b/sys/configure/structs/game.msgDistrib.go new file mode 100644 index 000000000..d51bb8947 --- /dev/null +++ b/sys/configure/structs/game.msgDistrib.go @@ -0,0 +1,42 @@ + +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +package cfg + +type Game_msgDistrib struct { + _dataMap map[string]*Game_msgDistribData + _dataList []*Game_msgDistribData +} + +func NewGame_msgDistrib(_buf []map[string]interface{}) (*Game_msgDistrib, error) { + _dataList := make([]*Game_msgDistribData, 0, len(_buf)) + dataMap := make(map[string]*Game_msgDistribData) + for _, _ele_ := range _buf { + if _v, err2 := NewGame_msgDistribData(_ele_); err2 != nil { + return nil, err2 + } else { + _dataList = append(_dataList, _v) + dataMap[_v.Mtype] = _v + } + } + return &Game_msgDistrib{_dataList:_dataList, _dataMap:dataMap}, nil +} + +func (table *Game_msgDistrib) GetDataMap() map[string]*Game_msgDistribData { + return table._dataMap +} + +func (table *Game_msgDistrib) GetDataList() []*Game_msgDistribData { + return table._dataList +} + +func (table *Game_msgDistrib) Get(key string) *Game_msgDistribData { + return table._dataMap[key] +} + + diff --git a/sys/configure/structs/game.msgDistribData.go b/sys/configure/structs/game.msgDistribData.go new file mode 100644 index 000000000..7d06bf250 --- /dev/null +++ b/sys/configure/structs/game.msgDistribData.go @@ -0,0 +1,29 @@ + +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ +package cfg + +import "errors" + +type Game_msgDistribData struct { + Mtype string + Stype string + Routrules string +} + +func (Game_msgDistribData) GetTypeId() int { + return -758961622 +} + +func NewGame_msgDistribData(_buf map[string]interface{}) (_v *Game_msgDistribData, err error) { + _v = &Game_msgDistribData{} + { var _ok_ bool; if _v.Mtype, _ok_ = _buf["mtype"].(string); !_ok_ { err = errors.New("mtype error"); return } } + { var _ok_ bool; if _v.Stype, _ok_ = _buf["stype"].(string); !_ok_ { err = errors.New("stype error"); return } } + { var _ok_ bool; if _v.Routrules, _ok_ = _buf["routrules"].(string); !_ok_ { err = errors.New("routrules error"); return } } + return +}