forked from xiaonanln/goworld
/
dispatchercluster.go
137 lines (113 loc) · 4.32 KB
/
dispatchercluster.go
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
126
127
128
129
130
131
132
133
134
135
136
137
package dispatchercluster
import (
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/config"
"github.com/xiaonanln/goworld/engine/dispatchercluster/dispatcherclient"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/netutil"
"github.com/xiaonanln/goworld/engine/proto"
)
var (
dispatcherConns []*dispatcherclient.DispatcherConnMgr
dispatcherNum int
gid uint16
)
func Initialize(_gid uint16, dctype dispatcherclient.DispatcherClientType, isRestoreGame, isBanBootEntity bool, delegate dispatcherclient.IDispatcherClientDelegate) {
gid = _gid
if gid == 0 {
gwlog.Fatalf("gid is 0")
}
dispIds := config.GetDispatcherIDs()
dispatcherNum = len(dispIds)
if dispatcherNum == 0 {
gwlog.Fatalf("dispatcher number is 0")
}
dispatcherConns = make([]*dispatcherclient.DispatcherConnMgr, dispatcherNum)
for _, dispid := range dispIds {
dispatcherConns[dispid-1] = dispatcherclient.NewDispatcherConnMgr(gid, dctype, dispid, isRestoreGame, isBanBootEntity, delegate)
}
for _, dispConn := range dispatcherConns {
dispConn.Connect()
}
}
func SendNotifyDestroyEntity(id common.EntityID) error {
return SelectByEntityID(id).SendNotifyDestroyEntity(id)
}
func SendMigrateRequest(entityID common.EntityID, spaceID common.EntityID, spaceGameID uint16) error {
return SelectByEntityID(entityID).SendMigrateRequest(entityID, spaceID, spaceGameID)
}
func SendRealMigrate(eid common.EntityID, targetGame uint16, data []byte) error {
return SelectByEntityID(eid).SendRealMigrate(eid, targetGame, data)
}
func SendCallFilterClientProxies(op proto.FilterClientsOpType, key, val string, method string, args []interface{}) {
pkt := proto.AllocCallFilterClientProxiesPacket(op, key, val, method, args)
broadcast(pkt)
pkt.Release()
return
}
func broadcast(packet *netutil.Packet) {
for _, dcm := range dispatcherConns {
dcm.GetDispatcherClientForSend().SendPacket(packet)
}
}
func SendNotifyCreateEntity(id common.EntityID) error {
if gid != 0 {
return SelectByEntityID(id).SendNotifyCreateEntity(id)
} else {
// goes here when creating nil space or restoring freezed entities
return nil
}
}
func SendLoadEntityAnywhere(typeName string, entityID common.EntityID) error {
return SelectByEntityID(entityID).SendLoadEntitySomewhere(typeName, entityID, 0)
}
func SendLoadEntityOnGame(typeName string, entityID common.EntityID, gameid uint16) error {
return SelectByEntityID(entityID).SendLoadEntitySomewhere(typeName, entityID, gameid)
}
func SendCreateEntitySomewhere(gameid uint16, entityid common.EntityID, typeName string, data map[string]interface{}) error {
return SelectByEntityID(entityid).SendCreateEntitySomewhere(gameid, entityid, typeName, data)
}
func SendGameLBCInfo(lbcinfo proto.GameLBCInfo) {
packet := proto.AllocGameLBCInfoPacket(lbcinfo)
broadcast(packet)
packet.Release()
}
func SendStartFreezeGame() {
pkt := proto.AllocStartFreezeGamePacket()
broadcast(pkt)
pkt.Release()
return
}
func SendSrvdisRegister(srvid string, info string, force bool) {
SelectBySrvID(srvid).SendSrvdisRegister(srvid, info, force)
}
func SendCallNilSpaces(exceptGameID uint16, method string, args []interface{}) {
// construct one packet for multiple sending
packet := proto.AllocCallNilSpacesPacket(exceptGameID, method, args)
broadcast(packet)
packet.Release()
}
func EntityIDToDispatcherID(entityid common.EntityID) uint16 {
return uint16((hashEntityID(entityid) % dispatcherNum) + 1)
}
func SrvIDToDispatcherID(srvid string) uint16 {
return uint16((hashSrvID(srvid) % dispatcherNum) + 1)
}
func SelectByEntityID(entityid common.EntityID) *dispatcherclient.DispatcherClient {
idx := hashEntityID(entityid) % dispatcherNum
return dispatcherConns[idx].GetDispatcherClientForSend()
}
func SelectByGateID(gateid uint16) *dispatcherclient.DispatcherClient {
idx := hashGateID(gateid) % dispatcherNum
return dispatcherConns[idx].GetDispatcherClientForSend()
}
func SelectByDispatcherID(dispid uint16) *dispatcherclient.DispatcherClient {
return dispatcherConns[dispid-1].GetDispatcherClientForSend()
}
func SelectBySrvID(srvid string) *dispatcherclient.DispatcherClient {
idx := hashSrvID(srvid) % dispatcherNum
return dispatcherConns[idx].GetDispatcherClientForSend()
}
func Select(dispidx int) *dispatcherclient.DispatcherClient {
return dispatcherConns[dispidx].GetDispatcherClientForSend()
}