82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/pb"
|
|
"reflect"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
/*
|
|
服务网关组件 用于接收网关服务发送过来的消息
|
|
*/
|
|
|
|
func NewMatchComp() comm.ISC_MatchComp {
|
|
comp := new(SCompMatch)
|
|
return comp
|
|
}
|
|
|
|
// 服务网关组件
|
|
type SCompMatch struct {
|
|
cbase.ServiceCompBase
|
|
options *CompOptions
|
|
service comm.IService //rpc服务对象 通过这个对象可以发布服务和调用其他服务的接口
|
|
msghandles map[string]*msghandle //处理函数的管理对象
|
|
}
|
|
|
|
// 设置服务组件名称 方便业务模块中获取此组件对象
|
|
func (this *SCompMatch) GetName() core.S_Comps {
|
|
return comm.SC_ServiceMatchComp
|
|
}
|
|
|
|
func (this *SCompMatch) NewOptions() (options core.ICompOptions) {
|
|
return new(CompOptions)
|
|
}
|
|
|
|
// 组件初始化函数
|
|
func (this *SCompMatch) Init(service core.IService, comp core.IServiceComp, options core.ICompOptions) (err error) {
|
|
err = this.ServiceCompBase.Init(service, comp, options)
|
|
this.options = options.(*CompOptions)
|
|
this.service = service.(comm.IService)
|
|
return err
|
|
}
|
|
|
|
// 组件启动时注册rpc服务监听
|
|
func (this *SCompMatch) Start() (err error) {
|
|
this.service.RegisterFunctionName(string(comm.RPC_SuccMatchNotice), this.SuccMatchNotice) //注册用户登录通知
|
|
err = this.ServiceCompBase.Start()
|
|
return
|
|
}
|
|
|
|
func (this *SCompMatch) RegisterMatchPool(poolName string, comp reflect.Value, msg reflect.Type, handle reflect.Method) {
|
|
this.msghandles[poolName] = &msghandle{
|
|
rcvr: comp,
|
|
msgType: msg,
|
|
handle: handle,
|
|
}
|
|
}
|
|
|
|
// RPC_NoticeUserClose 接收用户登录通知
|
|
func (this *SCompMatch) SuccMatchNotice(ctx context.Context, args *pb.SuccMatchNoticeReq, reply *pb.SuccMatchNoticeResp) (err error) {
|
|
msghandle, ok := this.msghandles[args.Poolname]
|
|
if ok {
|
|
var (
|
|
msg proto.Message
|
|
)
|
|
newSlice := reflect.New(msghandle.msgType)
|
|
for _, v := range args.Players {
|
|
if msg, err = v.UnmarshalNew(); err != nil {
|
|
return
|
|
}
|
|
newSlice = reflect.Append(newSlice, reflect.ValueOf(msg))
|
|
}
|
|
//执行处理流
|
|
msghandle.handle.Func.Call([]reflect.Value{msghandle.rcvr, newSlice})
|
|
}
|
|
return nil
|
|
}
|