game.go
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"pro2d/common/components"
"pro2d/conf"
"pro2d/models"
_ "pro2d/plugin"
"pro2d/utils/db"
"pro2d/utils/etcd"
"pro2d/utils/logger"
"sync"
"syscall"
"time"
)
type GameServer struct {
components.IServer
EtcdClient *etcd.EtcdClient
Agents *sync.Map
}
func NewGameServer(sconf *conf.SConf) (*GameServer, error) {
s := &GameServer{
IServer: components.NewServer(sconf.Port, conf.GlobalConf.GameConf.PluginPath, components.NewPBSplitter()),
Agents: new(sync.Map),
}
s.SetConnectionCallback(s.OnConnection)
s.SetMessageCallback(s.OnMessage)
s.SetCloseCallback(s.OnClose)
//mongo 初始化
db.MongoDatabase = db.MongoClient.Database(sconf.DBName)
models.InitGameServerModels()
//Etcd 初始化
var err error
s.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd)
if err != nil {
return nil, err
}
s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5)
go s.handleTimeOut()
return s, nil
}
func (s *GameServer) OnConnection(conn components.IConnection) {
agent := NewAgent(s)
agent.OnConnection(conn)
s.Agents.Store(conn.GetID(),agent)
}
func (s *GameServer) OnMessage(msg components.IMessage) {
agent, ok := s.Agents.Load(msg.GetSessId())
if !ok {
return
}
agent.(*Agent).OnMessage(msg)
}
func (s *GameServer) OnClose(conn components.IConnection) {
agent, ok := s.Agents.Load(conn.GetID())
if !ok {
return
}
agent.(*Agent).OnClose()
s.Agents.Delete(conn.GetID())
}
func (s *GameServer) Stop() {
s.IServer.Stop()
db.MongoClient.Disconnect(context.TODO())
}
func (s *GameServer) handleTimeOut() {
s.Agents.Range(func(key, value interface{}) bool {
agent := value.(*Agent)
agent.update()
return true
})
components.TimeOut(1*time.Second, s.handleTimeOut)
}
func main() {
err := make(chan error)
stopChan := make(chan os.Signal)
signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
userChan := make(chan os.Signal)
signal.Notify(userChan, syscall.SIGUSR1, syscall.SIGUSR2)
s,err1 := NewGameServer(conf.GlobalConf.GameConf)
if err1 != nil {
fmt.Errorf(err1.Error())
return
}
go func() {
err <- s.Start()
}()
for {
select {
case e := <- err:
logger.Error("game server error: %v", e)
return
case <-stopChan:
s.Stop()
logger.Debug("game stop...")
return
case u := <-userChan:
logger.Debug("userChan .. %v",u.String())
//s.LoadPlugin()
}
}
}