优化模块功能

This commit is contained in:
boyce
2019-02-12 12:03:20 +08:00
parent 9b7fcf0e27
commit ba4b084085
4 changed files with 48 additions and 43 deletions

View File

@@ -20,8 +20,10 @@ type MethodInfo struct {
type IModule interface {
SetModuleType(moduleType uint32)
GetModuleType() uint32
RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error
InitModule(module IModule) error
OnInit() error
OnRun() error
OnRun() bool
AddModule(module IModule) bool
GetModuleByType(moduleType uint32) IModule
GetOwnerService() IService
@@ -30,11 +32,9 @@ type IModule interface {
type IService interface {
Init(Iservice IService, servicetype int) error
Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error
OnInit() error
OnEndInit() error
OnRun() error
OnRunLoop() error
OnRun() bool
OnDestory() error
OnFetchService(iservice IService) error
OnSetupService(iservice IService) //其他服务被安装
@@ -70,9 +70,7 @@ type BaseService struct {
servicename string
servicetype int
tickTime int64
statTm int64
Status int
Status int
}
type BaseModule struct {
@@ -80,6 +78,7 @@ type BaseModule struct {
mapModule map[uint32]IModule
ownerService IService
tickTime int64
}
func (slf *BaseService) GetServiceId() int {
@@ -135,30 +134,6 @@ func (slf *BaseService) Init(iservice IService, servicetype int) error {
return nil
}
func (slf *BaseService) OnRunLoop() error {
return fmt.Errorf("None Loop")
}
func (slf *BaseService) Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error {
defer pwaitGroup.Done()
service.OnRun()
for {
select {
case <-exit:
fmt.Println("stopping...")
return nil
default:
}
slf.tickTime = time.Now().UnixNano() / 1e6
if service.OnRunLoop() != nil {
break
}
slf.tickTime = time.Now().UnixNano() / 1e6
}
return nil
}
func (slf *BaseService) RPC_CheckServiceTickTimeOut(microSecond int64) error {
if slf.IsTimeOutTick(microSecond) == true {
@@ -213,12 +188,13 @@ func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule {
return ret
}
func (slf *BaseModule) OnInit() error {
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType())
}
func (slf *BaseModule) OnRun() error {
return fmt.Errorf("not implement OnRun moduletype %d ", slf.GetModuleType())
func (slf *BaseModule) OnRun() bool {
return false
}
func (slf *BaseModule) GetOwnerService() IService {
@@ -228,3 +204,36 @@ func (slf *BaseModule) GetOwnerService() IService {
func (slf *BaseModule) SetOwnerService(iservice IService) {
slf.ownerService = iservice
}
func (slf *BaseModule) InitModule(module IModule) error {
module.OnInit()
for _, subModule := range slf.mapModule {
go subModule.OnInit()
}
return nil
}
func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error {
//运行所有子模块
for _, subModule := range slf.mapModule {
go subModule.RunModule(subModule, exit, pwaitGroup)
}
pwaitGroup.Add(1)
defer pwaitGroup.Done()
for {
select {
case <-exit:
fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf))
return nil
default:
}
if module.OnRun() == false {
break
}
slf.tickTime = time.Now().UnixNano() / 1e6
}
return nil
}

View File

@@ -14,7 +14,6 @@ type IServiceManager interface {
}
type CServiceManager struct {
//serviceList []IService
genserviceid int
localserviceMap map[string]IService
}
@@ -47,9 +46,7 @@ func (slf *CServiceManager) FetchService(s FetchService) IService {
func (slf *CServiceManager) Init() bool {
for _, s := range slf.localserviceMap {
if s.OnInit() != nil {
return false
}
(s.(IModule)).InitModule(s.(IModule))
}
// 初始化结束
@@ -64,8 +61,7 @@ func (slf *CServiceManager) Init() bool {
func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool {
for _, s := range slf.localserviceMap {
pwaitGroup.Add(1)
go s.Run(s, exit, pwaitGroup)
(s.(IModule)).RunModule(s.(IModule), exit, pwaitGroup)
}
pwaitGroup.Add(1)

View File

@@ -51,10 +51,10 @@ func (slf *HttpServerService) initRouterHandler() http.Handler {
return cors.Handler(r)
}
func (slf *HttpServerService) OnRun() error {
func (slf *HttpServerService) OnRun() bool {
slf.httpserver.Start()
return nil
return false
}
func NewHttpServerService(port uint16) *HttpServerService {

View File

@@ -21,10 +21,10 @@ func (ws *WSServerService) OnInit() error {
return nil
}
func (ws *WSServerService) OnRun() error {
func (ws *WSServerService) OnRun() bool {
ws.wsserver.Start()
return nil
return false
}
func NewWSServerService(pattern string, port uint16, messageReciver network.IMessageReceiver, bEnableCompression bool) *WSServerService {