package service import ( "github.com/duanhf2012/origin/rpc" "github.com/duanhf2012/origin/util/timer" "reflect" "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{} } type Service struct { Module rpc.RpcHandler //rpc name string //service name closeSig chan bool wg sync.WaitGroup this IService serviceCfg interface{} gorouterNum int32 startStatus bool } func (slf *Service) OnSetup(iservice IService){ if iservice.GetName() == "" { slf.name = reflect.Indirect(reflect.ValueOf(iservice)).Type().Name() } } func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) { slf.dispatcher =timer.NewDispatcher(timerDispatcherLen) slf.this = iservice slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun) //初始化祖先 slf.ancestor = iservice.(IModule) slf.seedModuleId =InitModuleId slf.descendants = map[int64]IModule{} slf.serviceCfg = serviceCfg slf.gorouterNum = 1 slf.this.OnInit() } func (slf *Service) SetGoRouterNum(gorouterNum int32) bool { //已经开始状态不允许修改协程数量 if slf.startStatus == true { return false } slf.gorouterNum = gorouterNum return true } func (slf *Service) Start() { slf.startStatus = true for i:=int32(0);i