package service import ( "fmt" "github.com/duanhf2012/origin/event" "github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/profiler" "github.com/duanhf2012/origin/rpc" "github.com/duanhf2012/origin/util/timer" "reflect" "runtime" "sync" "sync/atomic" ) var closeSig chan bool var timerDispatcherLen = 10 type IService interface { Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) GetName() string OnSetup(iservice IService) OnInit() error OnRelease() Wait() Start() GetRpcHandler() rpc.IRpcHandler GetServiceCfg()interface{} OpenProfiler() GetProfiler() *profiler.Profiler } type Service struct { Module rpc.RpcHandler //rpc name string //service name wg sync.WaitGroup serviceCfg interface{} gorouterNum int32 startStatus bool eventProcessor event.EventProcessor //事件接收者 profiler *profiler.Profiler //性能分析器 } func (slf *Service) OnSetup(iservice IService){ if iservice.GetName() == "" { slf.name = reflect.Indirect(reflect.ValueOf(iservice)).Type().Name() } } func (slf *Service) OpenProfiler() { slf.profiler = profiler.RegProfiler(slf.GetName()) if slf.profiler==nil { log.Fatal("rofiler.RegProfiler %s fail.",slf.GetName()) } } func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) { slf.dispatcher =timer.NewDispatcher(timerDispatcherLen) slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun) slf.self = iservice.(IModule) //初始化祖先 slf.ancestor = iservice.(IModule) slf.seedModuleId =InitModuleId slf.descendants = map[int64]IModule{} slf.serviceCfg = serviceCfg slf.gorouterNum = 1 slf.eventHandler.Init(&slf.eventProcessor) } func (slf *Service) SetGoRouterNum(gorouterNum int32) bool { //已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程 if slf.startStatus == true || slf.profiler!=nil { log.Error("open profiler mode is not allowed to set Multi-coroutine.") return false } slf.gorouterNum = gorouterNum return true } func (slf *Service) Start() { slf.startStatus = true for i:=int32(0);i