Blame view

cmd/gameserver/agent.go 2.58 KB
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
1
2
3
  package main
  
  import (
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
4
5
6
7
  	"github.com/golang/protobuf/proto"
  	"math"
  	"pro2d/common"
  	"pro2d/common/components"
765431a4   zhangqijia   增加schema接口, 抽象 mo...
8
  	"pro2d/common/logger"
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
9
  	"pro2d/models"
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
10
  	"pro2d/utils"
58e37bfe   zhangqijia   add sync.Pool to ...
11
  	"sync"
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
12
13
14
15
16
17
18
19
  	"sync/atomic"
  )
  
  type Agent struct {
  	components.IConnection
  	Server components.IServer
  
  	Role			*models.RoleModel
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
20
21
22
23
24
  	nextCheckTime   	int64 //下一次检查的时间
  	lastHeartCheckTime 	int64
  	heartTimeoutCount  	int   //超时次数
  }
  
58e37bfe   zhangqijia   add sync.Pool to ...
25
  var agentPool = sync.Pool{New: func() interface{} { return new(Agent)}}
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
26
  
58e37bfe   zhangqijia   add sync.Pool to ...
27
28
29
  func NewAgent(s components.IServer) *Agent {
  	a := agentPool.Get().(*Agent)
  	a.Server = s
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
30
  
58e37bfe   zhangqijia   add sync.Pool to ...
31
32
33
34
  	a.nextCheckTime =      0
  	a.lastHeartCheckTime = utils.Timex()
  	a.heartTimeoutCount=  0
  	return a
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
35
36
  }
  
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
37
38
  func (c *Agent) OnConnection(conn components.IConnection)  {
  	c.IConnection = conn
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
39
40
41
  }
  
  func (c *Agent) OnMessage(msg components.IMessage)  {
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
42
  	atomic.StoreInt64(&c.lastHeartCheckTime, utils.Timex())
765431a4   zhangqijia   增加schema接口, 抽象 mo...
43
  	md := c.Server.GetAction(msg.GetHeader().GetMsgID())
77f5eec7   zhangqijia   plugin 插件热更 接口
44
  	if md == nil {
58e37bfe   zhangqijia   add sync.Pool to ...
45
  		logger.Debug("cmd: %d, handler is nil", msg.GetHeader().GetMsgID())
77f5eec7   zhangqijia   plugin 插件热更 接口
46
47
  		return
  	}
58e37bfe   zhangqijia   add sync.Pool to ...
48
49
  	logger.Debug("protocolID: %d", msg.GetHeader().GetMsgID())
  	//fmt.Printf("errCode: %d, protoMsg:%v\n", errCode, protoMsg)
77f5eec7   zhangqijia   plugin 插件热更 接口
50
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
51
  	f := md.(func (msg components.IMessage)  (int32, interface{}))
58e37bfe   zhangqijia   add sync.Pool to ...
52
53
54
55
56
57
  	errCode, protoMsg := f(msg)
  	if protoMsg == nil {
  		return
  	}
  
  	rsp, err := proto.Marshal(protoMsg.(proto.Message))
77f5eec7   zhangqijia   plugin 插件热更 接口
58
  	if err != nil {
765431a4   zhangqijia   增加schema接口, 抽象 mo...
59
  		conn := msg.GetSession()
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
60
  		if conn != nil {
77f5eec7   zhangqijia   plugin 插件热更 接口
61
  			conn.Send(-100, msg.GetHeader().GetMsgID(), nil)
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
62
63
64
  		}
  		return
  	}
765431a4   zhangqijia   增加schema接口, 抽象 mo...
65
  	conn := msg.GetSession()
77f5eec7   zhangqijia   plugin 插件热更 接口
66
67
  	if conn != nil {
  		conn.Send(errCode, msg.GetHeader().GetMsgID(), rsp)
58e37bfe   zhangqijia   add sync.Pool to ...
68
  		return
77f5eec7   zhangqijia   plugin 插件热更 接口
69
  	}
58e37bfe   zhangqijia   add sync.Pool to ...
70
  	logger.Error("protocol not handler: %d", msg.GetHeader().GetMsgID())
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  }
  
  func (c *Agent) OnTimer()  {
  	nextCheckTime := atomic.LoadInt64(&c.nextCheckTime)
  	now := utils.Timex()
  	if now >= nextCheckTime {
  		//检查心跳
  		c.checkHeartBeat(now)
  		nextCheckTime = now + common.HeartTimerInterval
  		atomic.StoreInt64(&c.nextCheckTime, nextCheckTime)
  	}
  
  	if c.Role != nil {
  		//role 恢复数据
  		c.Role.OnRecoverTimer(now)
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
86
  	}
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
87
88
89
  }
  
  func (c *Agent) OnClose()  {
9a9d092e   zhangqijia   每条连接增加一个定时器,每条连接增...
90
  	c.Close()
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
91
92
93
  }
  
  func (c *Agent) Close()  {
58e37bfe   zhangqijia   add sync.Pool to ...
94
95
  	agentPool.Put(c)
  
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
96
97
98
99
100
101
102
103
104
  	if c.Role == nil {
  		return
  	}
  
  	c.Role.OnOfflineEvent()
  }
  
  func (c *Agent) checkHeartBeat(now int64)  {
  	lastHeartCheckTime := atomic.LoadInt64(&c.lastHeartCheckTime)
58e37bfe   zhangqijia   add sync.Pool to ...
105
  	//logger.Debug("checkHeartBeat ID: %d, last: %d, now: %d", c.GetID(), lastHeartCheckTime, now)
0e5d52de   zhangqijia   reactor: 重构底层框架1.0
106
107
108
109
110
111
112
113
114
115
116
  	if math.Abs(float64(lastHeartCheckTime - now)) > common.HeartTimerInterval {
  		c.heartTimeoutCount++
  		if c.heartTimeoutCount >= common.HeartTimeoutCountMax {
  			c.Stop()
  			return
  		}
  		logger.Debug("timeout count: %d", c.heartTimeoutCount)
  	}else {
  		c.heartTimeoutCount = 0
  	}
  }