Beispiel #1
0
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)
	}
}
Beispiel #2
0
//消息分发
func (agent *gateway_agent) dispatcher(msgID uint16, msg proto.Messager) {
	id := msgID / 100
	if id == 100 {
		switch msgID {
		case proto.CS_ACCOUNT_HEART:
			agent.handle_heart(msg)
			return
		case proto.CS_ACCOUNT_LOGIN:
			agent.handle_login(msg)
			return
		case proto.CS_ACCOUNT_CREATE:
			agent.handle_create(msg)
			return
		case proto.CS_ACCOUNT_LOGOUT:
			agent.Close(0)
			return
		default:
			if agent.roleRecv == nil {
				logger.Error("消息状态错误:%d", msgID)
				agent.Close(0)
			} else {
				//分发消息到role处理
				agent.roleRecv <- proto.PMessage{ID: msgID, Msg: msg}
			}
		}
	} else {
		if agent.roleRecv == nil {
			logger.Error("消息状态错误:%d", msgID)
			agent.Close(0)
		} else {
			//分发消息到role处理
			agent.roleRecv <- proto.PMessage{ID: msgID, Msg: msg}
		}
	}
}
Beispiel #3
0
func (w *word) run() {
	//是否重启
	defer func() {
		if x := recover(); x != nil {
			w.restartNum++
			if w.restartNum <= w.maxRestartNum {
				go w.run()
				logger.Error("word restart: %v", x)
			} else {
				logger.Error("word exit: %v", x)
			}
		}
	}()
	w.worder.Run(w.v)
}
Beispiel #4
0
func persiteData(rc *roleAgent) {
	rc.addTimerFun(2*time.Minute, func() { persiteData(rc) })
	err := rc.rd.PersiteData()
	if err != nil {
		logger.Error("数据持久化错误:%v", err)
	}
}
Beispiel #5
0
func (this *db_table_role_info) loop_dump() {
	for {
		key, value := this.cmap.DeleteAnyOneWait()
		_, err := this.collection.Upsert(bson.M{"roleid": key}, value)
		if err != nil {
			logger.Error("持久化错误:%s", err.Error())
			this.cmap.PutAndSignal(key, value)
		}
	}
}
Beispiel #6
0
func (rc *roleAgent) loop() {
	for {
		select {
		case msg := <-rc.recv:
			if f := role.GetHandle(msg.ID); f != nil {
				f(rc.rd, msg.Msg)
			} else {
				logger.Error("无效消息:%#v", msg)
			}
		case <-rc.timer.Timer.C:
			rc.timer.Run()
		case <-rc.exitCnt:
			return
		}
	}
}
Beispiel #7
0
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()
}
Beispiel #8
0
func (server *TCPServer) run() {
	server.wgLn.Add(1)
	defer server.wgLn.Done()

	var tempDelay time.Duration
	for {
		conn, err := server.ln.Accept()
		if err != nil {
			if ne, ok := err.(net.Error); ok && ne.Temporary() {
				if tempDelay == 0 {
					tempDelay = 5 * time.Millisecond
				} else {
					tempDelay *= 2
				}
				if max := 1 * time.Second; tempDelay > max {
					tempDelay = max
				}
				logger.Error("accept error: %v; retrying in %v", err, tempDelay)
				time.Sleep(tempDelay)
				continue
			}
			return
		}
		tempDelay = 0

		//判断是否超最高在线个数
		agent := server.newAgent(conn, server.msgParser)
		server.mutexAgents.Lock()
		if len(server.agents) >= server.maxConnNum {
			server.mutexAgents.Unlock()
			agent.Close(1)
			logger.Info("too many connections")
			continue
		}
		server.agents[agent] = struct{}{}
		server.mutexAgents.Unlock()

		server.wgAgents.Add(1)
		go func() {
			agent.Run()
			agent.Close(0)
			conn.Close()
			server.wgAgents.Done()
		}()
	}
}
Beispiel #9
0
//用log模块记录错误调用函数栈 必须在defer中调用
func LogPanicStack() {
	if x := recover(); x != nil {
		logger.ErrorDepth(2, "%v", x)
		logger.Error(GetCallStack(10))
	}
}