mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-12 11:07:53 +08:00
优化模块功能
This commit is contained in:
@@ -20,8 +20,10 @@ type MethodInfo struct {
|
|||||||
type IModule interface {
|
type IModule interface {
|
||||||
SetModuleType(moduleType uint32)
|
SetModuleType(moduleType uint32)
|
||||||
GetModuleType() uint32
|
GetModuleType() uint32
|
||||||
|
RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error
|
||||||
|
InitModule(module IModule) error
|
||||||
OnInit() error
|
OnInit() error
|
||||||
OnRun() error
|
OnRun() bool
|
||||||
AddModule(module IModule) bool
|
AddModule(module IModule) bool
|
||||||
GetModuleByType(moduleType uint32) IModule
|
GetModuleByType(moduleType uint32) IModule
|
||||||
GetOwnerService() IService
|
GetOwnerService() IService
|
||||||
@@ -30,11 +32,9 @@ type IModule interface {
|
|||||||
|
|
||||||
type IService interface {
|
type IService interface {
|
||||||
Init(Iservice IService, servicetype int) error
|
Init(Iservice IService, servicetype int) error
|
||||||
Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error
|
|
||||||
OnInit() error
|
OnInit() error
|
||||||
OnEndInit() error
|
OnEndInit() error
|
||||||
OnRun() error
|
OnRun() bool
|
||||||
OnRunLoop() error
|
|
||||||
OnDestory() error
|
OnDestory() error
|
||||||
OnFetchService(iservice IService) error
|
OnFetchService(iservice IService) error
|
||||||
OnSetupService(iservice IService) //其他服务被安装
|
OnSetupService(iservice IService) //其他服务被安装
|
||||||
@@ -70,9 +70,7 @@ type BaseService struct {
|
|||||||
servicename string
|
servicename string
|
||||||
servicetype int
|
servicetype int
|
||||||
|
|
||||||
tickTime int64
|
Status int
|
||||||
statTm int64
|
|
||||||
Status int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseModule struct {
|
type BaseModule struct {
|
||||||
@@ -80,6 +78,7 @@ type BaseModule struct {
|
|||||||
mapModule map[uint32]IModule
|
mapModule map[uint32]IModule
|
||||||
|
|
||||||
ownerService IService
|
ownerService IService
|
||||||
|
tickTime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) GetServiceId() int {
|
func (slf *BaseService) GetServiceId() int {
|
||||||
@@ -135,30 +134,6 @@ func (slf *BaseService) Init(iservice IService, servicetype int) error {
|
|||||||
return nil
|
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 {
|
func (slf *BaseService) RPC_CheckServiceTickTimeOut(microSecond int64) error {
|
||||||
|
|
||||||
if slf.IsTimeOutTick(microSecond) == true {
|
if slf.IsTimeOutTick(microSecond) == true {
|
||||||
@@ -213,12 +188,13 @@ func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule {
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) OnInit() error {
|
func (slf *BaseModule) OnInit() error {
|
||||||
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType())
|
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) OnRun() error {
|
func (slf *BaseModule) OnRun() bool {
|
||||||
return fmt.Errorf("not implement OnRun moduletype %d ", slf.GetModuleType())
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) GetOwnerService() IService {
|
func (slf *BaseModule) GetOwnerService() IService {
|
||||||
@@ -228,3 +204,36 @@ func (slf *BaseModule) GetOwnerService() IService {
|
|||||||
func (slf *BaseModule) SetOwnerService(iservice IService) {
|
func (slf *BaseModule) SetOwnerService(iservice IService) {
|
||||||
slf.ownerService = 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ type IServiceManager interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CServiceManager struct {
|
type CServiceManager struct {
|
||||||
//serviceList []IService
|
|
||||||
genserviceid int
|
genserviceid int
|
||||||
localserviceMap map[string]IService
|
localserviceMap map[string]IService
|
||||||
}
|
}
|
||||||
@@ -47,9 +46,7 @@ func (slf *CServiceManager) FetchService(s FetchService) IService {
|
|||||||
func (slf *CServiceManager) Init() bool {
|
func (slf *CServiceManager) Init() bool {
|
||||||
|
|
||||||
for _, s := range slf.localserviceMap {
|
for _, s := range slf.localserviceMap {
|
||||||
if s.OnInit() != nil {
|
(s.(IModule)).InitModule(s.(IModule))
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化结束
|
// 初始化结束
|
||||||
@@ -64,8 +61,7 @@ func (slf *CServiceManager) Init() bool {
|
|||||||
|
|
||||||
func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool {
|
func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool {
|
||||||
for _, s := range slf.localserviceMap {
|
for _, s := range slf.localserviceMap {
|
||||||
pwaitGroup.Add(1)
|
(s.(IModule)).RunModule(s.(IModule), exit, pwaitGroup)
|
||||||
go s.Run(s, exit, pwaitGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pwaitGroup.Add(1)
|
pwaitGroup.Add(1)
|
||||||
|
|||||||
@@ -51,10 +51,10 @@ func (slf *HttpServerService) initRouterHandler() http.Handler {
|
|||||||
return cors.Handler(r)
|
return cors.Handler(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) OnRun() error {
|
func (slf *HttpServerService) OnRun() bool {
|
||||||
|
|
||||||
slf.httpserver.Start()
|
slf.httpserver.Start()
|
||||||
return nil
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHttpServerService(port uint16) *HttpServerService {
|
func NewHttpServerService(port uint16) *HttpServerService {
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ func (ws *WSServerService) OnInit() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WSServerService) OnRun() error {
|
func (ws *WSServerService) OnRun() bool {
|
||||||
ws.wsserver.Start()
|
ws.wsserver.Start()
|
||||||
|
|
||||||
return nil
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWSServerService(pattern string, port uint16, messageReciver network.IMessageReceiver, bEnableCompression bool) *WSServerService {
|
func NewWSServerService(pattern string, port uint16, messageReciver network.IMessageReceiver, bEnableCompression bool) *WSServerService {
|
||||||
|
|||||||
Reference in New Issue
Block a user