From f0927c3a1a8d9e277499d8a9d67ba11366e021d5 Mon Sep 17 00:00:00 2001 From: sydnash <157621271@qq.com> Date: Fri, 12 May 2017 16:14:28 +0800 Subject: [PATCH] modify core.Start param. --- core/callhelper.go | 2 +- core/core.go | 22 ++++++++++++++-------- core/module_test.go | 12 ++++++++++-- core/service.go | 7 +++++-- lotou.go | 13 ++++--------- master_test.go | 6 +++++- network/tcp/client_test.go | 12 ++++++++++-- network/tcp/server.go | 6 +++++- network/tcp/server_test.go | 6 +++++- slave_test.go | 6 +++++- topology/master.go | 6 +++++- topology/master_test.go | 6 +++++- topology/slave.go | 12 ++++++++++-- topology/slave_test.go | 6 +++++- 14 files changed, 89 insertions(+), 33 deletions(-) diff --git a/core/callhelper.go b/core/callhelper.go index f1d28f1..ce5b564 100644 --- a/core/callhelper.go +++ b/core/callhelper.go @@ -68,7 +68,7 @@ func (c *CallHelper) getIsAutoReply(cmd CmdType) bool { func (c *CallHelper) findCallbackDesc(cmd CmdType) *callbackDesc { cb, ok := c.funcMap[cmd] if !ok { - if cb, ok := c.funcMap[Cmd_Default]; ok { + if cb, ok = c.funcMap[Cmd_Default]; ok { log.Info("func: <%v>:%d is not found in {%v}, use default cmd handler.", cmd, len(cmd), c.hostServiceName) } else { log.Fatal("func: <%v>:%d is not found in {%v}", cmd, len(cmd), c.hostServiceName) diff --git a/core/core.go b/core/core.go index 145c30e..fcaaa3a 100644 --- a/core/core.go +++ b/core/core.go @@ -6,20 +6,26 @@ import ( "reflect" ) +type ModuleParam struct { + N string + M Module + L int +} + // StartService starts the given modules with specific names //`service' will call module's OnInit after registration // and registers name to master if the name(of service) is a global name (starting without a dot) // and starts msg loop in an another goroutine -func StartService(name string, m Module) ServiceID { - s := newService(name) - s.m = m +func StartService(m *ModuleParam) ServiceID { + s := newService(m.N, m.L) + s.m = m.M id := registerService(s) - m.setService(s) - d := m.getDuration() - if !checkIsLocalName(name) { - globalName(id, name) + m.M.setService(s) + d := m.M.getDuration() + if !checkIsLocalName(m.N) { + globalName(id, m.N) } - s.m.OnModuleStartup(id, name) + s.m.OnModuleStartup(id, m.N) if d > 0 { s.runWithLoop(d) } else { diff --git a/core/module_test.go b/core/module_test.go index c45d94c..041cd50 100644 --- a/core/module_test.go +++ b/core/module_test.go @@ -47,8 +47,16 @@ func (g *Game) OnInit() { func TestModule(t *testing.T) { log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize) - id1 := core.StartService("g1", &Game{Skeleton: core.NewSkeleton(0)}) - core.StartService("g2", &Game{Skeleton: core.NewSkeleton(1000), Dst: id1}) + id1 := core.StartService(&core.ModuleParam{ + N: "g1", + M: &Game{Skeleton: core.NewSkeleton(0)}, + L: 0, + }) + core.StartService(&core.ModuleParam{ + N: "g2", + M: &Game{Skeleton: core.NewSkeleton(1000), Dst: id1}, + L: 0, + }) ch := make(chan int) go func() { diff --git a/core/service.go b/core/service.go index 0cb55fc..465d26e 100644 --- a/core/service.go +++ b/core/service.go @@ -47,9 +47,12 @@ var ( ServiceCallTimeout = errors.New("call time out") ) -func newService(name string) *service { +func newService(name string, len int) *service { s := &service{name: name} - s.msgChan = make(chan *Message, 1024) + if len <= 1024 { + len = 1024 + } + s.msgChan = make(chan *Message, len) s.requestId = 0 s.requestMap = make(map[uint64]requestCB) s.callChanMap = make(map[uint64]chan []interface{}) diff --git a/lotou.go b/lotou.go index 1522484..1c20dce 100644 --- a/lotou.go +++ b/lotou.go @@ -14,11 +14,6 @@ import ( "time" ) -type ModuleParam struct { - N string - M core.Module -} - type CloseFunc func() //Start start lotou with given modules which in is in data. @@ -30,7 +25,7 @@ type CloseFunc func() //capture system's SIGKILL SIGTERM signal //and wait until all service are closed. //f will be called when SIGKILL or SIGTERM is received. -func Start(f CloseFunc, data ...*ModuleParam) { +func Start(f CloseFunc, data ...*core.ModuleParam) { rand.Seed(time.Now().UnixNano()) logger := log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize) logger.SetColored(conf.LogHasColor) @@ -48,7 +43,7 @@ func Start(f CloseFunc, data ...*ModuleParam) { } for _, m := range data { - core.StartService(m.N, m.M) + core.StartService(m) } /*err := http.ListenAndServe(":10000", nil) @@ -74,7 +69,7 @@ func Start(f CloseFunc, data ...*ModuleParam) { } //RawStart start lotou, with no wait -func RawStart(data ...*ModuleParam) { +func RawStart(data ...*core.ModuleParam) { log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize) core.InitNode(conf.CoreIsStandalone, conf.CoreIsMaster) @@ -89,6 +84,6 @@ func RawStart(data ...*ModuleParam) { } for _, m := range data { - core.StartService(m.N, m.M) + core.StartService(m) } } diff --git a/master_test.go b/master_test.go index eba4ed2..f65a51e 100644 --- a/master_test.go +++ b/master_test.go @@ -52,5 +52,9 @@ func TestMaster(t *testing.T) { conf.CoreIsStandalone = false conf.CoreIsMaster = true game := &Game{core.NewSkeleton(0), 0} - lotou.Start(nil, &lotou.ModuleParam{"game1", game}) + lotou.Start(nil, &core.ModuleParam{ + N: "game1", + M: game, + L: 0, + }) } diff --git a/network/tcp/client_test.go b/network/tcp/client_test.go index ce2e648..95f3e9a 100644 --- a/network/tcp/client_test.go +++ b/network/tcp/client_test.go @@ -52,12 +52,20 @@ func TestClient(t *testing.T) { for i := 0; i < 1; i++ { c := &C{Skeleton: core.NewSkeleton(1000)} - core.StartService(".client", c) + core.StartService(&core.ModuleParam{ + N: ".client", + M: c, + L: 0, + }) c.encoder = binary.NewEncoder() c.decoder = binary.NewDecoder() client := tcp.NewClient("127.0.0.1", "3333", c.Id) - c.client = core.StartService(".cc", client) + c.client = core.StartService(&core.ModuleParam{ + N: ".cc", + M: client, + L: 0, + }) } for { diff --git a/network/tcp/server.go b/network/tcp/server.go index a3ba143..0452767 100644 --- a/network/tcp/server.go +++ b/network/tcp/server.go @@ -68,7 +68,11 @@ func (self *Server) Listen() error { break } a := NewAgent(tcpCon, self.hostService) - core.StartService("", a) + core.StartService(&core.ModuleParam{ + N: "", + M: a, + L: 0, + }) } }() diff --git a/network/tcp/server_test.go b/network/tcp/server_test.go index 2572733..4016efb 100644 --- a/network/tcp/server_test.go +++ b/network/tcp/server_test.go @@ -40,7 +40,11 @@ func TestServer(t *testing.T) { log.Init("test", log.FATAL_LEVEL, log.DEBUG_LEVEL, 10000, 1000) m := &M{Skeleton: core.NewSkeleton(0)} m.decoder = binary.NewDecoder() - core.StartService(".m", m) + core.StartService(&core.ModuleParam{ + N: ".m", + M: m, + L: 0, + }) s := tcp.NewServer("", "3333", m.Id) s.Listen() diff --git a/slave_test.go b/slave_test.go index 03d607f..d1e3473 100644 --- a/slave_test.go +++ b/slave_test.go @@ -36,5 +36,9 @@ func TestSlave(t *testing.T) { conf.CoreIsMaster = false game := &Game{core.NewSkeleton(1000), 0} - lotou.Start(nil, &lotou.ModuleParam{"game2", game}) + lotou.Start(nil, &core.ModuleParam{ + N: "game2", + M: game, + L: 0, + }) } diff --git a/topology/master.go b/topology/master.go index 08d26ec..1298a3e 100644 --- a/topology/master.go +++ b/topology/master.go @@ -19,7 +19,11 @@ func StartMaster(ip, port string) { m := &master{Skeleton: core.NewSkeleton(0)} m.nodesMap = make(map[uint64]core.ServiceID) m.globalNameMap = make(map[string]core.ServiceID) - core.StartService(".router", m) + core.StartService(&core.ModuleParam{ + N: ".router", + M: m, + L: 0, + }) if !conf.CoreIsStandalone { m.tcpServer = tcp.NewServer(ip, port, m.Id) diff --git a/topology/master_test.go b/topology/master_test.go index d6419a0..29b9a4e 100644 --- a/topology/master_test.go +++ b/topology/master_test.go @@ -34,7 +34,11 @@ func TestMaster(t *testing.T) { core.RegisterNode() game := &Game{core.NewSkeleton(0)} - id := core.StartService("game1", game) + id := core.StartService(&core.ModuleParam{ + N: "game1", + M: game, + L: 0, + }) log.Info("game1's id :%v", id) log.Info("test") diff --git a/topology/slave.go b/topology/slave.go index e0b59f0..73ca5d3 100644 --- a/topology/slave.go +++ b/topology/slave.go @@ -14,9 +14,17 @@ type slave struct { func StartSlave(ip, port string) { m := &slave{Skeleton: core.NewSkeleton(0)} - core.StartService(".router", m) + core.StartService(&core.ModuleParam{ + N: ".router", + M: m, + L: 0, + }) c := tcp.NewClient(ip, port, m.Id) - m.client = core.StartService("", c) + m.client = core.StartService(&core.ModuleParam{ + N: "", + M: c, + L: 0, + }) } func (s *slave) OnNormalMSG(msg *core.Message) { diff --git a/topology/slave_test.go b/topology/slave_test.go index 80d602d..fdf40d3 100644 --- a/topology/slave_test.go +++ b/topology/slave_test.go @@ -37,7 +37,11 @@ func TestSlavea(t *testing.T) { core.RegisterNode() log.Info("start create service") game := &Game{core.NewSkeleton(1000)} - core.StartService("game2", game) + core.StartService(&core.ModuleParam{ + N: "game2", + M: game, + L: 0, + }) log.Info("game2's id: %v", game.Id) var err error