go_dreamfactory/modules/battletest/battleclientmgr.go
2024-03-01 17:53:00 +08:00

82 lines
2.0 KiB
Go

package battletest
import (
"context"
"go_dreamfactory/pb"
"sync"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log"
)
/*
战斗服务客户端组件
*/
type battleClientMgrComp struct {
cbase.ModuleCompBase
options *Options
service core.IService
module *BattleTest
mutexs []sync.Mutex
clinet []*client
lock sync.Mutex
index int32
}
//组件初始化接口
func (this *battleClientMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.options = options.(*Options)
this.module = module.(*BattleTest)
this.mutexs = make([]sync.Mutex, len(this.options.BattleServerAddr))
return
}
func (this *battleClientMgrComp) Start() (err error) {
err = this.ModuleCompBase.Start()
this.clinet = make([]*client, this.options.ClientNum)
if this.options.OpenCheck {
for i := int32(0); i < (this.options.ClientNum); i++ {
if this.clinet[i], err = newClient(i, this.options.BattleServerAddr, this, this.options.Log); err != nil {
return
}
}
}
return
}
//关闭客户端连接对象
func (this *battleClientMgrComp) Shutdown(c *client) {
this.clinet[c.i] = nil
log.Errorf("战斗校验服务被关闭!")
}
func (this *battleClientMgrComp) getclient() (_client *client, err error) {
i := this.index % this.options.ClientNum
_client = this.clinet[i]
if _client != nil {
this.index++
return
}
if _client, err = newClient(i, this.options.BattleServerAddr, this, this.options.Log); err != nil {
log.Errorln(err)
return
}
this.clinet[i] = _client
return
}
//校验战斗结果
func (this *battleClientMgrComp) CheckBattle(ctx context.Context, req *pb.BattleReport) (reply *pb.BattleCheckResults, err error) {
var client *client
reply = &pb.BattleCheckResults{}
if client, err = this.getclient(); err != nil {
return
}
if err = client.callBattle(ctx, "Check", req, reply); err != nil {
log.Errorln(err)
}
return
}