优化初始化方式

This commit is contained in:
duanhf2012
2020-03-28 11:06:22 +08:00
parent 0ddeceb977
commit 7745ed39f3
7 changed files with 17 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import (
var configdir = "./config/" var configdir = "./config/"
type SubNet struct { type SubNet struct {
SubNetName string
NodeList []NodeInfo NodeList []NodeInfo
} }

View File

@@ -119,7 +119,7 @@ func (slf *Cluster) InitCfg(currentNodeId int) error{
if ok == false { if ok == false {
return fmt.Errorf("NodeId %d not in any subnet",currentNodeId) return fmt.Errorf("NodeId %d not in any subnet",currentNodeId)
} }
subnet.SubNetName = localSubnetName
for _,nodeinfo := range subnet.NodeList { for _,nodeinfo := range subnet.NodeList {
localSubNetMapNode[nodeinfo.NodeId] = nodeinfo localSubNetMapNode[nodeinfo.NodeId] = nodeinfo
@@ -160,7 +160,7 @@ func (slf *Cluster) InitCfg(currentNodeId int) error{
slf.localNodeInfo =localNodeInfo slf.localNodeInfo =localNodeInfo
//读取服务 //读取服务
slf.ReadLocalSubNetServiceConfig(slf.localsubnet.SubNetName)
return err return err
} }

View File

@@ -1,6 +1,7 @@
package GateService package GateService
import ( import (
"fmt"
"github.com/duanhf2012/origin/event" "github.com/duanhf2012/origin/event"
"github.com/duanhf2012/origin/network" "github.com/duanhf2012/origin/network"
"github.com/duanhf2012/origin/network/processor" "github.com/duanhf2012/origin/network/processor"
@@ -37,10 +38,10 @@ func (slf *GateService) OnEventHandler(ev *event.Event) error{
} }
func (slf *GateService) OnConnected(clientid uint64){ func (slf *GateService) OnConnected(clientid uint64){
fmt.Printf("client id %d connected",clientid)
} }
func (slf *GateService) OnDisconnected(clientid uint64){ func (slf *GateService) OnDisconnected(clientid uint64){
fmt.Printf("client id %d disconnected",clientid)
} }

View File

@@ -5,7 +5,7 @@
"ListenAddr":"127.0.0.1:8001", "ListenAddr":"127.0.0.1:8001",
"NodeName": "Node_Test1", "NodeName": "Node_Test1",
"remark":"//以_打头的表示只在本机进程不对整个子网开发", "remark":"//以_打头的表示只在本机进程不对整个子网开发",
"ServiceList": ["TestService1","TestService2","TestServiceCall"] "ServiceList": ["TestService1","TestService2","TestServiceCall","GateService","TcpService"]
}, },
{ {
"NodeId": 2, "NodeId": 2,

View File

@@ -189,12 +189,11 @@ func (slf *TestService2) OnInit() error {
} }
func main(){ func main(){
node.Init()
tcpService := &sysservice.TcpService{} tcpService := &sysservice.TcpService{}
gateService := &GateService.GateService{} gateService := &GateService.GateService{}
tcpService.SetEventReciver(gateService) tcpService.SetEventReciver(gateService)
node.Setup(tcpService,gateService) node.Setup(tcpService,gateService)
node.Init()
node.Start() node.Start()
} }

View File

@@ -102,6 +102,7 @@ func Start() {
func Setup(s ...service.IService) { func Setup(s ...service.IService) {
for _,sv := range s { for _,sv := range s {
sv.OnSetup(sv)
preSetupService = append(preSetupService,sv) preSetupService = append(preSetupService,sv)
} }
} }

View File

@@ -16,6 +16,7 @@ type IService interface {
Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{})
GetName() string GetName() string
OnSetup(iservice IService)
OnInit() error OnInit() error
OnRelease() OnRelease()
Wait() Wait()
@@ -38,8 +39,14 @@ type Service struct {
startStatus bool 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{}) { func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) {
slf.name = reflect.Indirect(reflect.ValueOf(iservice)).Type().Name()
slf.dispatcher =timer.NewDispatcher(timerDispatcherLen) slf.dispatcher =timer.NewDispatcher(timerDispatcherLen)
slf.this = iservice slf.this = iservice
slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun) slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun)