func (agent *gateway_agent) Run() { defer util.LogPanicStack() for { agent.conn.SetReadDeadline(time.Now().Add(120 * time.Second)) dataBytes, err := agent.msgParser.Read(agent.conn) if err != nil { if !agent.isClosed { if ne, ok := err.(net.Error); ok { logger.Error("网关消息接收错误:%v", ne) } } return } if agent.check_msg_speed_lager() { logger.Error("发送消息太快") agent.Close(4) return } id, msg, err := proto.DecodeProto(dataBytes) if err != nil { logger.Error("网关协议解析错误:%s", err.Error()) return } logger.Debug("receive msg:%d %#v", id, msg) agent.dispatcher(id, msg) } }
func main() { //以守护进程方式运行 // if os.Getppid() != 1 { // filePath, _ := filepath.Abs(os.Args[0]) // fmt.Printf("filePath=%s\n", filePath) // cmd := exec.Command(filePath, os.Args[1:]...) // cmd.Start() // return // } runtime.GOMAXPROCS(runtime.NumCPU() * 2) err := config.LoadConfig("setting") if err != nil { panic(err) } l, err := logger.StartLog(config.DataSetting.LogDir, config.DataSetting.LogLevel, true) if err != nil { panic(err) } logger.Export(l) defer util.LogPanicStack() logger.Info("开始加载配置....") err = config.LoadAllConfig() if err != nil { logger.Critical("%s", err.Error()) return } logger.Info("===>>配置加载成功") err = db.Start() if err != nil { logger.Critical("===>数据库启动错误:%v", err) } logger.Info("db start...") //加载所有名字 name.InitAllRoleName() word.RegisterModule() gateway.RegisterModule() console.RegisterModule(config.DataSetting.ConsoleAddr, 10, 1024) //注册方法运行时间统计命令 util.RegisterMTCommand() module.Init() logger.Info("服务器启动成功") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) sig := <-c module.Destroy() db.Stop() logger.Info("服务器退出:%v", sig) }
//发送goroutine func (agent *gateway_agent) send_loop() { defer util.LogPanicStack() for { select { case msg := <-agent.recv: send_msg(agent.conn, msg, agent.rpk) case <-agent.exitCnt: return } } }
func (rc *roleAgent) start() { defer util.LogPanicStack() defer rc.stop() rc.timer = timer.NewTimerFunQueue() role.SetRoleClient(rc.rd) err := role.LoadData(rc.rd) if err != nil { logger.Error("加载数据出错:%s", err.Error()) return } role.HookOnline(rc.rd) rc.isOK = true rc.addTimerFun(2*time.Minute, func() { persiteData(rc) }) rc.loop() }