1 Star 0 Fork 0

wuzpdev/goworld

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
dispatchercluster.go 4.51 KB
Copy Edit Raw Blame History
seis authored 2018-06-17 14:19 . dispatcher cluster broadcast
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 SendClearClientFilterProp(gateid uint16, clientid common.ClientID) (err error) {
return SelectByGateID(gateid).SendClearClientFilterProp(gateid, clientid)
}
func SendSetClientFilterProp(gateid uint16, clientid common.ClientID, key, val string) (err error) {
return SelectByGateID(gateid).SendSetClientFilterProp(gateid, clientid, key, val)
}
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 SendCreateEntityAnywhere(entityid common.EntityID, typeName string, data map[string]interface{}) error {
return SelectByEntityID(entityid).SendCreateEntityAnywhere(entityid, typeName, data)
}
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()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.5

Search