Commit 9a9d092e9e6d19caa673b3dd99c551c51bd18064
1 parent
0e5d52de
每条连接增加一个定时器,每条连接增加一条协程用于监听定时器事件和网络数据事件,通过回调函数投递到上层
Showing
11 changed files
with
152 additions
and
109 deletions
Show diff stats
Makefile
@@ -9,14 +9,14 @@ gen: | @@ -9,14 +9,14 @@ gen: | ||
9 | test: | 9 | test: |
10 | go run cmd/test/client.go | 10 | go run cmd/test/client.go |
11 | http: | 11 | http: |
12 | - go run -race cmd/httpserver/*.go | 12 | + go run -race cmd/httpserver/http.go cmd/httpserver/AccountAction.go |
13 | 13 | ||
14 | game: | 14 | game: |
15 | go run -race cmd/gameserver/*.go | 15 | go run -race cmd/gameserver/*.go |
16 | build: | 16 | build: |
17 | go build -race -o bin/account cmd/http.go | 17 | go build -race -o bin/account cmd/http.go |
18 | - go build -race -o bin/game cmd/game.go | ||
19 | - go build -race -o bin/test test/client.go | 18 | + go build -race -o bin/game cmd/gameserver/*.go |
19 | + go build -race -o bin/test cmd/test/client.go | ||
20 | regame:plugin | 20 | regame:plugin |
21 | lsof -i:8849 | grep "game" | grep -v grep | awk '{print $$2}' | xargs -I {} kill -USR1 {} | 21 | lsof -i:8849 | grep "game" | grep -v grep | awk '{print $$2}' | xargs -I {} kill -USR1 {} |
22 | 22 |
cmd/gameserver/agent.go
@@ -19,8 +19,7 @@ type Agent struct { | @@ -19,8 +19,7 @@ type Agent struct { | ||
19 | 19 | ||
20 | Role *models.RoleModel | 20 | Role *models.RoleModel |
21 | 21 | ||
22 | - readFunc chan func() | ||
23 | - timerFunc chan func() | 22 | + |
24 | Quit chan *Agent | 23 | Quit chan *Agent |
25 | 24 | ||
26 | nextCheckTime int64 //下一次检查的时间 | 25 | nextCheckTime int64 //下一次检查的时间 |
@@ -31,8 +30,7 @@ type Agent struct { | @@ -31,8 +30,7 @@ type Agent struct { | ||
31 | func NewAgent(s components.IServer) *Agent { | 30 | func NewAgent(s components.IServer) *Agent { |
32 | return &Agent{ | 31 | return &Agent{ |
33 | Server: s, | 32 | Server: s, |
34 | - readFunc: make(chan func(), 10), | ||
35 | - timerFunc: make(chan func(), 10), | 33 | + |
36 | Quit: make(chan *Agent), | 34 | Quit: make(chan *Agent), |
37 | 35 | ||
38 | nextCheckTime: 0, | 36 | nextCheckTime: 0, |
@@ -41,53 +39,51 @@ func NewAgent(s components.IServer) *Agent { | @@ -41,53 +39,51 @@ func NewAgent(s components.IServer) *Agent { | ||
41 | } | 39 | } |
42 | } | 40 | } |
43 | 41 | ||
44 | -func (c *Agent) listen() { | ||
45 | - defer c.Close() | ||
46 | - for { | ||
47 | - select { | ||
48 | - case timerFunc := <- c.timerFunc: | ||
49 | - timerFunc() | ||
50 | - case readFunc := <- c.readFunc: | ||
51 | - readFunc() | ||
52 | - case <- c.Quit: | ||
53 | - return | ||
54 | - } | ||
55 | - } | ||
56 | -} | ||
57 | - | ||
58 | func (c *Agent) OnConnection(conn components.IConnection) { | 42 | func (c *Agent) OnConnection(conn components.IConnection) { |
59 | c.IConnection = conn | 43 | c.IConnection = conn |
60 | - go c.listen() | ||
61 | } | 44 | } |
62 | 45 | ||
63 | func (c *Agent) OnMessage(msg components.IMessage) { | 46 | func (c *Agent) OnMessage(msg components.IMessage) { |
64 | - f := func() { | ||
65 | - atomic.StoreInt64(&c.lastHeartCheckTime, utils.Timex()) | ||
66 | - if md, ok := components.ActionMap[pb.ProtoCode(msg.GetHeader().GetMsgID())]; ok { | ||
67 | - logger.Debug("protocode handler: %d", msg.GetHeader().GetMsgID()) | ||
68 | - errCode, protomsg := md(msg) | ||
69 | - rsp, err := proto.Marshal(protomsg) | ||
70 | - fmt.Printf("errCode: %d, protomsg:%v\n", errCode, protomsg) | ||
71 | - if err != nil { | ||
72 | - conn := c.Server.GetIConnection(msg.GetSessId()) | ||
73 | - if conn != nil { | ||
74 | - conn.Send(-100, msg.GetHeader().GetMsgID(), nil) | ||
75 | - } | ||
76 | - return | ||
77 | - } | 47 | + atomic.StoreInt64(&c.lastHeartCheckTime, utils.Timex()) |
48 | + if md, ok := components.ActionMap[pb.ProtoCode(msg.GetHeader().GetMsgID())]; ok { | ||
49 | + logger.Debug("protocode handler: %d", msg.GetHeader().GetMsgID()) | ||
50 | + errCode, protomsg := md(msg) | ||
51 | + rsp, err := proto.Marshal(protomsg) | ||
52 | + fmt.Printf("errCode: %d, protomsg:%v\n", errCode, protomsg) | ||
53 | + if err != nil { | ||
78 | conn := c.Server.GetIConnection(msg.GetSessId()) | 54 | conn := c.Server.GetIConnection(msg.GetSessId()) |
79 | if conn != nil { | 55 | if conn != nil { |
80 | - conn.Send(errCode, msg.GetHeader().GetMsgID(), rsp) | 56 | + conn.Send(-100, msg.GetHeader().GetMsgID(), nil) |
81 | } | 57 | } |
82 | return | 58 | return |
83 | } | 59 | } |
84 | - logger.Error("protocode not handler: %d", msg.GetHeader().GetMsgID()) | 60 | + conn := c.Server.GetIConnection(msg.GetSessId()) |
61 | + if conn != nil { | ||
62 | + conn.Send(errCode, msg.GetHeader().GetMsgID(), rsp) | ||
63 | + } | ||
64 | + return | ||
65 | + } | ||
66 | + logger.Error("protocode not handler: %d", msg.GetHeader().GetMsgID()) | ||
67 | +} | ||
68 | + | ||
69 | +func (c *Agent) OnTimer() { | ||
70 | + nextCheckTime := atomic.LoadInt64(&c.nextCheckTime) | ||
71 | + now := utils.Timex() | ||
72 | + if now >= nextCheckTime { | ||
73 | + //检查心跳 | ||
74 | + c.checkHeartBeat(now) | ||
75 | + nextCheckTime = now + common.HeartTimerInterval | ||
76 | + atomic.StoreInt64(&c.nextCheckTime, nextCheckTime) | ||
77 | + } | ||
78 | + | ||
79 | + if c.Role != nil { | ||
80 | + //role 恢复数据 | ||
81 | + c.Role.OnRecoverTimer(now) | ||
85 | } | 82 | } |
86 | - c.readFunc <- f | ||
87 | } | 83 | } |
88 | 84 | ||
89 | func (c *Agent) OnClose() { | 85 | func (c *Agent) OnClose() { |
90 | - c.Quit <- c | 86 | + c.Close() |
91 | } | 87 | } |
92 | 88 | ||
93 | func (c *Agent) Close() { | 89 | func (c *Agent) Close() { |
@@ -112,21 +108,3 @@ func (c *Agent) checkHeartBeat(now int64) { | @@ -112,21 +108,3 @@ func (c *Agent) checkHeartBeat(now int64) { | ||
112 | c.heartTimeoutCount = 0 | 108 | c.heartTimeoutCount = 0 |
113 | } | 109 | } |
114 | } | 110 | } |
115 | - | ||
116 | -func (c *Agent) update() { | ||
117 | - nextCheckTime := atomic.LoadInt64(&c.nextCheckTime) | ||
118 | - now := utils.Timex() | ||
119 | - if now >= nextCheckTime { | ||
120 | - //检查心跳 | ||
121 | - c.checkHeartBeat(now) | ||
122 | - nextCheckTime = now + common.HeartTimerInterval | ||
123 | - atomic.StoreInt64(&c.nextCheckTime, nextCheckTime) | ||
124 | - } | ||
125 | - | ||
126 | - c.timerFunc <- func() { | ||
127 | - if c.Role != nil { | ||
128 | - //role 恢复数据 | ||
129 | - c.Role.OnRecoverTimer(now) | ||
130 | - } | ||
131 | - } | ||
132 | -} |
cmd/gameserver/game.go
@@ -3,6 +3,7 @@ package main | @@ -3,6 +3,7 @@ package main | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "fmt" | 5 | "fmt" |
6 | + "net/http" | ||
6 | _ "net/http/pprof" | 7 | _ "net/http/pprof" |
7 | "os" | 8 | "os" |
8 | "os/signal" | 9 | "os/signal" |
@@ -15,7 +16,6 @@ import ( | @@ -15,7 +16,6 @@ import ( | ||
15 | "pro2d/utils/logger" | 16 | "pro2d/utils/logger" |
16 | "sync" | 17 | "sync" |
17 | "syscall" | 18 | "syscall" |
18 | - "time" | ||
19 | ) | 19 | ) |
20 | 20 | ||
21 | type GameServer struct { | 21 | type GameServer struct { |
@@ -33,6 +33,7 @@ func NewGameServer(sconf *conf.SConf) (*GameServer, error) { | @@ -33,6 +33,7 @@ func NewGameServer(sconf *conf.SConf) (*GameServer, error) { | ||
33 | s.SetConnectionCallback(s.OnConnection) | 33 | s.SetConnectionCallback(s.OnConnection) |
34 | s.SetMessageCallback(s.OnMessage) | 34 | s.SetMessageCallback(s.OnMessage) |
35 | s.SetCloseCallback(s.OnClose) | 35 | s.SetCloseCallback(s.OnClose) |
36 | + s.SetTimerCallback(s.OnTimer) | ||
36 | 37 | ||
37 | //mongo 初始化 | 38 | //mongo 初始化 |
38 | db.MongoDatabase = db.MongoClient.Database(sconf.DBName) | 39 | db.MongoDatabase = db.MongoClient.Database(sconf.DBName) |
@@ -46,7 +47,6 @@ func NewGameServer(sconf *conf.SConf) (*GameServer, error) { | @@ -46,7 +47,6 @@ func NewGameServer(sconf *conf.SConf) (*GameServer, error) { | ||
46 | } | 47 | } |
47 | s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) | 48 | s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) |
48 | 49 | ||
49 | - go s.handleTimeOut() | ||
50 | return s, nil | 50 | return s, nil |
51 | } | 51 | } |
52 | 52 | ||
@@ -64,6 +64,14 @@ func (s *GameServer) OnMessage(msg components.IMessage) { | @@ -64,6 +64,14 @@ func (s *GameServer) OnMessage(msg components.IMessage) { | ||
64 | agent.(*Agent).OnMessage(msg) | 64 | agent.(*Agent).OnMessage(msg) |
65 | } | 65 | } |
66 | 66 | ||
67 | +func (s *GameServer) OnTimer(conn components.IConnection) { | ||
68 | + agent, ok := s.Agents.Load(conn.GetID()) | ||
69 | + if !ok { | ||
70 | + return | ||
71 | + } | ||
72 | + agent.(*Agent).OnTimer() | ||
73 | +} | ||
74 | + | ||
67 | func (s *GameServer) OnClose(conn components.IConnection) { | 75 | func (s *GameServer) OnClose(conn components.IConnection) { |
68 | agent, ok := s.Agents.Load(conn.GetID()) | 76 | agent, ok := s.Agents.Load(conn.GetID()) |
69 | if !ok { | 77 | if !ok { |
@@ -80,16 +88,6 @@ func (s *GameServer) Stop() { | @@ -80,16 +88,6 @@ func (s *GameServer) Stop() { | ||
80 | } | 88 | } |
81 | 89 | ||
82 | 90 | ||
83 | -func (s *GameServer) handleTimeOut() { | ||
84 | - s.Agents.Range(func(key, value interface{}) bool { | ||
85 | - agent := value.(*Agent) | ||
86 | - agent.update() | ||
87 | - return true | ||
88 | - }) | ||
89 | - | ||
90 | - components.TimeOut(1*time.Second, s.handleTimeOut) | ||
91 | -} | ||
92 | - | ||
93 | func main() { | 91 | func main() { |
94 | err := make(chan error) | 92 | err := make(chan error) |
95 | stopChan := make(chan os.Signal) | 93 | stopChan := make(chan os.Signal) |
@@ -103,6 +101,9 @@ func main() { | @@ -103,6 +101,9 @@ func main() { | ||
103 | fmt.Errorf(err1.Error()) | 101 | fmt.Errorf(err1.Error()) |
104 | return | 102 | return |
105 | } | 103 | } |
104 | + go func() { | ||
105 | + err <- http.ListenAndServe("localhost:6061", nil) | ||
106 | + }() | ||
106 | 107 | ||
107 | go func() { | 108 | go func() { |
108 | err <- s.Start() | 109 | err <- s.Start() |
cmd/httpserver/http.go
@@ -35,7 +35,8 @@ func (s *AccountServer) Start() error { | @@ -35,7 +35,8 @@ func (s *AccountServer) Start() error { | ||
35 | return err | 35 | return err |
36 | } | 36 | } |
37 | s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) | 37 | s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) |
38 | - return nil | 38 | + |
39 | + return s.IHttp.Start() | ||
39 | } | 40 | } |
40 | 41 | ||
41 | func main() { | 42 | func main() { |
@@ -43,7 +44,7 @@ func main() { | @@ -43,7 +44,7 @@ func main() { | ||
43 | stopChan := make(chan os.Signal) | 44 | stopChan := make(chan os.Signal) |
44 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) | 45 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) |
45 | 46 | ||
46 | - web := NewAccountServer("v1") | 47 | + web := NewAccountServer("v1", fmt.Sprintf(":%d", conf.GlobalConf.AccountConf.Port)) |
47 | web.BindHandler(&AccountAction{HttpServer: web}) | 48 | web.BindHandler(&AccountAction{HttpServer: web}) |
48 | go func() { | 49 | go func() { |
49 | err <- web.Start() | 50 | err <- web.Start() |
common/components/conn.go
@@ -2,10 +2,11 @@ package components | @@ -2,10 +2,11 @@ package components | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bufio" | 4 | "bufio" |
5 | - "errors" | 5 | + "fmt" |
6 | "net" | 6 | "net" |
7 | "pro2d/common" | 7 | "pro2d/common" |
8 | "pro2d/utils/logger" | 8 | "pro2d/utils/logger" |
9 | + "sync/atomic" | ||
9 | "time" | 10 | "time" |
10 | ) | 11 | ) |
11 | 12 | ||
@@ -20,24 +21,37 @@ type Connection struct { | @@ -20,24 +21,37 @@ type Connection struct { | ||
20 | writer *bufio.Writer | 21 | writer *bufio.Writer |
21 | WBuffer chan []byte | 22 | WBuffer chan []byte |
22 | Quit chan *Connection | 23 | Quit chan *Connection |
24 | + readFunc chan func() | ||
25 | + timerFunc chan func() | ||
23 | 26 | ||
24 | messageCallback MessageCallback | 27 | messageCallback MessageCallback |
25 | connectionCallback ConnectionCallback | 28 | connectionCallback ConnectionCallback |
26 | closeCallback CloseCallback | 29 | closeCallback CloseCallback |
27 | timerCallback TimerCallback | 30 | timerCallback TimerCallback |
31 | + | ||
32 | + Status uint32 | ||
28 | } | 33 | } |
29 | 34 | ||
30 | func NewConn(id int, conn net.Conn, s IServer) *Connection { | 35 | func NewConn(id int, conn net.Conn, s IServer) *Connection { |
31 | - return &Connection{ | ||
32 | - Id: id, | ||
33 | - Conn: conn, | ||
34 | - Server: s, | ||
35 | - | ||
36 | - scanner: bufio.NewScanner(conn), | ||
37 | - writer: bufio.NewWriter(conn), | ||
38 | - WBuffer: make(chan []byte, common.MaxMsgChan), | ||
39 | - Quit: make(chan *Connection), | 36 | + c := &Connection{ |
37 | + Id: id, | ||
38 | + Conn: conn, | ||
39 | + Server: s, | ||
40 | + | ||
41 | + scanner: bufio.NewScanner(conn), | ||
42 | + writer: bufio.NewWriter(conn), | ||
43 | + WBuffer: make(chan []byte, common.MaxMsgChan), | ||
44 | + Quit: make(chan *Connection), | ||
45 | + readFunc: make(chan func(), 10), | ||
46 | + timerFunc: make(chan func(), 10), | ||
47 | + | ||
48 | + Status: 0, | ||
40 | } | 49 | } |
50 | + c.connectionCallback = c.defaultConnectionCallback | ||
51 | + c.messageCallback = c.defaultMessageCallback | ||
52 | + c.closeCallback = c.defaultCloseCallback | ||
53 | + c.timerCallback = c.defaultTimerCallback | ||
54 | + return c | ||
41 | } | 55 | } |
42 | 56 | ||
43 | func (c *Connection) GetID() int { | 57 | func (c *Connection) GetID() int { |
@@ -56,6 +70,22 @@ func (c *Connection) SetCloseCallback(cb CloseCallback) { | @@ -56,6 +70,22 @@ func (c *Connection) SetCloseCallback(cb CloseCallback) { | ||
56 | c.closeCallback = cb | 70 | c.closeCallback = cb |
57 | } | 71 | } |
58 | 72 | ||
73 | +func (c *Connection) SetTimerCallback(cb TimerCallback) { | ||
74 | + c.timerCallback = cb | ||
75 | +} | ||
76 | + | ||
77 | +func (c *Connection) defaultConnectionCallback(conn IConnection) { | ||
78 | +} | ||
79 | + | ||
80 | +func (c *Connection) defaultMessageCallback(msg IMessage) { | ||
81 | +} | ||
82 | + | ||
83 | +func (c *Connection) defaultCloseCallback(conn IConnection) { | ||
84 | +} | ||
85 | + | ||
86 | +func (c *Connection) defaultTimerCallback(conn IConnection) { | ||
87 | +} | ||
88 | + | ||
59 | func (c *Connection) write() { | 89 | func (c *Connection) write() { |
60 | defer c.Stop() | 90 | defer c.Stop() |
61 | 91 | ||
@@ -73,7 +103,7 @@ func (c *Connection) write() { | @@ -73,7 +103,7 @@ func (c *Connection) write() { | ||
73 | } | 103 | } |
74 | 104 | ||
75 | func (c *Connection) read() { | 105 | func (c *Connection) read() { |
76 | - defer c.Stop() | 106 | + defer c.Quitting() |
77 | c.scanner.Split(c.Server.GetSplitter().ParseMsg) | 107 | c.scanner.Split(c.Server.GetSplitter().ParseMsg) |
78 | 108 | ||
79 | for c.scanner.Scan() { | 109 | for c.scanner.Scan() { |
@@ -83,7 +113,9 @@ func (c *Connection) read() { | @@ -83,7 +113,9 @@ func (c *Connection) read() { | ||
83 | } | 113 | } |
84 | 114 | ||
85 | req.SetSessId(c.Id) | 115 | req.SetSessId(c.Id) |
86 | - c.messageCallback(req) | 116 | + c.readFunc <- func() { |
117 | + c.messageCallback(req) | ||
118 | + } | ||
87 | } | 119 | } |
88 | 120 | ||
89 | if err := c.scanner.Err(); err != nil { | 121 | if err := c.scanner.Err(); err != nil { |
@@ -92,14 +124,53 @@ func (c *Connection) read() { | @@ -92,14 +124,53 @@ func (c *Connection) read() { | ||
92 | } | 124 | } |
93 | } | 125 | } |
94 | 126 | ||
127 | +func (c *Connection) listen(){ | ||
128 | + defer c.Stop() | ||
129 | + | ||
130 | + for { | ||
131 | + select { | ||
132 | + case timerFunc := <- c.timerFunc: | ||
133 | + timerFunc() | ||
134 | + case readFunc := <- c.readFunc: | ||
135 | + readFunc() | ||
136 | + case <- c.Quit: | ||
137 | + return | ||
138 | + } | ||
139 | + } | ||
140 | +} | ||
141 | + | ||
142 | + | ||
143 | +func (c *Connection) handleTimeOut() { | ||
144 | + c.timerFunc <- func() { | ||
145 | + c.timerCallback(c) | ||
146 | + } | ||
147 | + TimeOut(1*time.Second, c.handleTimeOut) | ||
148 | +} | ||
149 | + | ||
150 | +func (c *Connection) Quitting() { | ||
151 | + c.Quit <- c | ||
152 | +} | ||
153 | + | ||
95 | func (c *Connection) Start() { | 154 | func (c *Connection) Start() { |
96 | - c.connectionCallback(c) | ||
97 | go c.write() | 155 | go c.write() |
98 | go c.read() | 156 | go c.read() |
157 | + go c.listen() | ||
158 | + | ||
159 | + c.Status = 1 | ||
160 | + c.connectionCallback(c) | ||
161 | + c.handleTimeOut() | ||
99 | } | 162 | } |
100 | 163 | ||
101 | func (c *Connection) Stop() { | 164 | func (c *Connection) Stop() { |
102 | logger.Debug("ID: %d close", c.Id) | 165 | logger.Debug("ID: %d close", c.Id) |
166 | + closed := atomic.LoadUint32(&c.Status) | ||
167 | + if closed == 0 { | ||
168 | + return | ||
169 | + } | ||
170 | + atomic.StoreUint32(&c.Status, 0) | ||
171 | + | ||
172 | + close(c.WBuffer) | ||
173 | + close(c.Quit) | ||
103 | c.Conn.Close() | 174 | c.Conn.Close() |
104 | c.closeCallback(c) | 175 | c.closeCallback(c) |
105 | } | 176 | } |
@@ -115,8 +186,9 @@ func (c *Connection) Send(errCode int32, cmd uint32, data []byte) error{ | @@ -115,8 +186,9 @@ func (c *Connection) Send(errCode int32, cmd uint32, data []byte) error{ | ||
115 | // 发送超时 | 186 | // 发送超时 |
116 | select { | 187 | select { |
117 | case <-sendTimeout.C: | 188 | case <-sendTimeout.C: |
118 | - return errors.New("send buff msg timeout") | 189 | + return fmt.Errorf("send buff msg timeout") |
119 | case c.WBuffer <- buf: | 190 | case c.WBuffer <- buf: |
120 | return nil | 191 | return nil |
121 | } | 192 | } |
122 | } | 193 | } |
194 | + |
common/components/icompontents.go
@@ -29,6 +29,11 @@ type ISplitter interface { | @@ -29,6 +29,11 @@ type ISplitter interface { | ||
29 | GetHeadLen() uint32 | 29 | GetHeadLen() uint32 |
30 | } | 30 | } |
31 | 31 | ||
32 | +type ConnectionCallback func(IConnection) | ||
33 | +type CloseCallback func(IConnection) | ||
34 | +type MessageCallback func(IMessage) | ||
35 | +type TimerCallback func(IConnection) | ||
36 | + | ||
32 | //链接 | 37 | //链接 |
33 | type IConnection interface { | 38 | type IConnection interface { |
34 | GetID() int | 39 | GetID() int |
@@ -39,13 +44,9 @@ type IConnection interface { | @@ -39,13 +44,9 @@ type IConnection interface { | ||
39 | SetConnectionCallback(ConnectionCallback) | 44 | SetConnectionCallback(ConnectionCallback) |
40 | SetMessageCallback(MessageCallback) | 45 | SetMessageCallback(MessageCallback) |
41 | SetCloseCallback(CloseCallback) | 46 | SetCloseCallback(CloseCallback) |
47 | + SetTimerCallback(TimerCallback) | ||
42 | } | 48 | } |
43 | 49 | ||
44 | -type ConnectionCallback func(IConnection) | ||
45 | -type CloseCallback func(IConnection) | ||
46 | -type MessageCallback func(IMessage) | ||
47 | -type TimerCallback func(IConnection) | ||
48 | - | ||
49 | //server | 50 | //server |
50 | type IServer interface { | 51 | type IServer interface { |
51 | Start() error | 52 | Start() error |
common/components/server.go
@@ -16,9 +16,10 @@ var ActionMap map[pb.ProtoCode]ActionHandler | @@ -16,9 +16,10 @@ var ActionMap map[pb.ProtoCode]ActionHandler | ||
16 | type Server struct { | 16 | type Server struct { |
17 | IServer | 17 | IServer |
18 | 18 | ||
19 | - connectionCallback ConnectionCallback | ||
20 | - messageCallback MessageCallback | ||
21 | - closeCallback CloseCallback | 19 | + connectionCallback ConnectionCallback |
20 | + messageCallback MessageCallback | ||
21 | + closeCallback CloseCallback | ||
22 | + timerCallback TimerCallback | ||
22 | 23 | ||
23 | splitter ISplitter | 24 | splitter ISplitter |
24 | 25 | ||
@@ -62,6 +63,7 @@ func (s *Server) SetCloseCallback(cb CloseCallback) { | @@ -62,6 +63,7 @@ func (s *Server) SetCloseCallback(cb CloseCallback) { | ||
62 | } | 63 | } |
63 | 64 | ||
64 | func (s *Server) SetTimerCallback(cb TimerCallback) { | 65 | func (s *Server) SetTimerCallback(cb TimerCallback) { |
66 | + s.timerCallback = cb | ||
65 | } | 67 | } |
66 | 68 | ||
67 | func (s *Server) newConnection(conn IConnection) { | 69 | func (s *Server) newConnection(conn IConnection) { |
@@ -70,6 +72,7 @@ func (s *Server) newConnection(conn IConnection) { | @@ -70,6 +72,7 @@ func (s *Server) newConnection(conn IConnection) { | ||
70 | conn.SetConnectionCallback(s.connectionCallback) | 72 | conn.SetConnectionCallback(s.connectionCallback) |
71 | conn.SetCloseCallback(s.removeConnection) | 73 | conn.SetCloseCallback(s.removeConnection) |
72 | conn.SetMessageCallback(s.messageCallback) | 74 | conn.SetMessageCallback(s.messageCallback) |
75 | + conn.SetTimerCallback(s.timerCallback) | ||
73 | 76 | ||
74 | go conn.Start() | 77 | go conn.Start() |
75 | } | 78 | } |
conf/conf.yaml
@@ -5,16 +5,13 @@ go 1.17 | @@ -5,16 +5,13 @@ go 1.17 | ||
5 | require ( | 5 | require ( |
6 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108 | 6 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108 |
7 | github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 | 7 | github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 |
8 | - github.com/dgrijalva/jwt-go v3.2.0+incompatible | ||
9 | github.com/garyburd/redigo v1.6.3 | 8 | github.com/garyburd/redigo v1.6.3 |
10 | github.com/gin-gonic/gin v1.7.7 | 9 | github.com/gin-gonic/gin v1.7.7 |
11 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b | 10 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b |
12 | github.com/golang/protobuf v1.5.2 | 11 | github.com/golang/protobuf v1.5.2 |
13 | - github.com/ouqiang/timewheel v1.0.1 | ||
14 | go.etcd.io/etcd/api/v3 v3.5.2 | 12 | go.etcd.io/etcd/api/v3 v3.5.2 |
15 | go.etcd.io/etcd/client/v3 v3.5.2 | 13 | go.etcd.io/etcd/client/v3 v3.5.2 |
16 | go.mongodb.org/mongo-driver v1.8.3 | 14 | go.mongodb.org/mongo-driver v1.8.3 |
17 | - google.golang.org/grpc v1.38.0 | ||
18 | google.golang.org/protobuf v1.27.1 | 15 | google.golang.org/protobuf v1.27.1 |
19 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | 16 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b |
20 | ) | 17 | ) |
@@ -37,7 +34,6 @@ require ( | @@ -37,7 +34,6 @@ require ( | ||
37 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | 34 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect |
38 | github.com/modern-go/reflect2 v1.0.1 // indirect | 35 | github.com/modern-go/reflect2 v1.0.1 // indirect |
39 | github.com/pkg/errors v0.9.1 // indirect | 36 | github.com/pkg/errors v0.9.1 // indirect |
40 | - github.com/robfig/cron v1.2.0 // indirect | ||
41 | github.com/ugorji/go/codec v1.1.7 // indirect | 37 | github.com/ugorji/go/codec v1.1.7 // indirect |
42 | github.com/xdg-go/pbkdf2 v1.0.0 // indirect | 38 | github.com/xdg-go/pbkdf2 v1.0.0 // indirect |
43 | github.com/xdg-go/scram v1.0.2 // indirect | 39 | github.com/xdg-go/scram v1.0.2 // indirect |
@@ -53,7 +49,7 @@ require ( | @@ -53,7 +49,7 @@ require ( | ||
53 | golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect | 49 | golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect |
54 | golang.org/x/text v0.3.5 // indirect | 50 | golang.org/x/text v0.3.5 // indirect |
55 | google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect | 51 | google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect |
56 | - gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect | 52 | + google.golang.org/grpc v1.38.0 // indirect |
57 | gopkg.in/yaml.v2 v2.4.0 // indirect | 53 | gopkg.in/yaml.v2 v2.4.0 // indirect |
58 | ) | 54 | ) |
59 | 55 |
1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= |
2 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | 2 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= |
3 | -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= | ||
4 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= | 3 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= |
5 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108 h1:iPugyBI7oFtbDZXC4dnY093M1kZx6k/95sen92gafbY= | 4 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108 h1:iPugyBI7oFtbDZXC4dnY093M1kZx6k/95sen92gafbY= |
6 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108/go.mod h1:WAMLHwunr1hi3u7OjGV6/VWG9QbdMhGpEKjROiSFd10= | 5 | github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108/go.mod h1:WAMLHwunr1hi3u7OjGV6/VWG9QbdMhGpEKjROiSFd10= |
@@ -27,8 +26,6 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV | @@ -27,8 +26,6 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV | ||
27 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | 26 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
28 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | 27 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= |
29 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | 28 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
30 | -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= | ||
31 | -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= | ||
32 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= | 29 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= |
33 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= | 30 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= |
34 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= | 31 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= |
@@ -130,8 +127,6 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb | @@ -130,8 +127,6 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb | ||
130 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= | 127 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= |
131 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= | 128 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= |
132 | github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= | 129 | github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= |
133 | -github.com/ouqiang/timewheel v1.0.1 h1:XxhrYwqhJ3z8nthEnhZcHyZ/dcE29ACJEJR3Ika0W2g= | ||
134 | -github.com/ouqiang/timewheel v1.0.1/go.mod h1:896mz+8zvRU6i0PLVR0qaNuU5roxC874OB4TxUvUewY= | ||
135 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | 130 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
136 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | 131 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
137 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | 132 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= |
@@ -153,8 +148,6 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R | @@ -153,8 +148,6 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R | ||
153 | github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= | 148 | github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= |
154 | github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= | 149 | github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= |
155 | github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= | 150 | github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= |
156 | -github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= | ||
157 | -github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= | ||
158 | github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= | 151 | github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= |
159 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= | 152 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= |
160 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= | 153 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= |
@@ -317,8 +310,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 | @@ -317,8 +310,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 | ||
317 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | 310 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
318 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= | 311 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= |
319 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | 312 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
320 | -gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= | ||
321 | -gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= | ||
322 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | 313 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
323 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | 314 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
324 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | 315 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
No preview for this file type