Skip to content

Commit

Permalink
dispatcher cluster broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaonanln committed Jun 17, 2018
1 parent ea25b7b commit fef6bb0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion components/game/GameService.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (gs *GameService) terminate() {
func (gs *GameService) startFreeze() {
dispatcherNum := len(config.GetDispatcherIDs())
gs.dispatcherStartFreezeAcks = make([]bool, dispatcherNum)
dispatchercluster.SendStartFreezeGame(gameid)
dispatchercluster.SendStartFreezeGame()
}

func ConnectedGamesNum() int {
Expand Down
46 changes: 16 additions & 30 deletions engine/dispatchercluster/dispatchercluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ func SendMigrateRequest(entityID common.EntityID, spaceID common.EntityID, space
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{}) (anyerror error) {
// TODO: broadcast one packet instead of sending multiple packets
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 {
err := dcm.GetDispatcherClientForSend().SendCallFilterClientProxies(op, key, val, method, args)
if err != nil && anyerror == nil {
anyerror = err
}
dcm.GetDispatcherClientForSend().SendPacket(packet)
}
return
}

func SendNotifyCreateEntity(id common.EntityID) error {
Expand All @@ -87,38 +89,22 @@ func SendCreateEntityAnywhere(entityid common.EntityID, typeName string, data ma
return SelectByEntityID(entityid).SendCreateEntityAnywhere(entityid, typeName, data)
}

func SendStartFreezeGame(gameid uint16) (anyerror error) {
// TODO: broadcast one packet instead of sending multiple packets
for _, dcm := range dispatcherConns {
err := dcm.GetDispatcherClientForSend().SendStartFreezeGame(gameid)
if err != nil {
anyerror = err
}
}
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{}) (anyerror error) {
func SendCallNilSpaces(exceptGameID uint16, method string, args []interface{}) {
// construct one packet for multiple sending
packet := netutil.NewPacket()
packet.AppendUint16(proto.MT_CALL_NIL_SPACES)
packet.AppendUint16(exceptGameID)
packet.AppendVarStr(method)
packet.AppendArgs(args)

for _, dcm := range dispatcherConns {
err := dcm.GetDispatcherClientForSend().SendPacket(packet)
if err != nil {
anyerror = err
}
}

packet := proto.AllocCallNilSpacesPacket(exceptGameID, method, args)
broadcast(packet)
packet.Release()
return
}

func EntityIDToDispatcherID(entityid common.EntityID) uint16 {
Expand Down
30 changes: 23 additions & 7 deletions engine/proto/GoWorldConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,25 @@ func (gwc *GoWorldConnection) SendClearClientFilterProp(gateid uint16, clientid
}

// SendCallFilterClientProxies sends MT_CALL_FILTERED_CLIENTS message
func (gwc *GoWorldConnection) SendCallFilterClientProxies(op FilterClientsOpType, key, val string, method string, args []interface{}) (err error) {
packet := gwc.packetConn.NewPacket()
func AllocCallFilterClientProxiesPacket(op FilterClientsOpType, key, val string, method string, args []interface{}) *netutil.Packet {
packet := netutil.NewPacket()
packet.AppendUint16(MT_CALL_FILTERED_CLIENTS)
packet.AppendByte(byte(op))
packet.AppendVarStr(key)
packet.AppendVarStr(val)
packet.AppendVarStr(method)
packet.AppendArgs(args)
return gwc.SendPacketRelease(packet)
return packet
}

func AllocCallNilSpacesPacket(exceptGameID uint16, method string, args []interface{}) *netutil.Packet {
// construct one packet for multiple sending
packet := netutil.NewPacket()
packet.AppendUint16(MT_CALL_NIL_SPACES)
packet.AppendUint16(exceptGameID)
packet.AppendVarStr(method)
packet.AppendArgs(args)
return packet
}

// SendQuerySpaceGameIDForMigrate sends MT_QUERY_SPACE_GAMEID_FOR_MIGRATE message
Expand Down Expand Up @@ -348,11 +358,17 @@ func (gwc *GoWorldConnection) SendRealMigrate(eid common.EntityID, targetGame ui
return gwc.SendPacketRelease(packet)
}

// SendStartFreezeGame sends MT_START_FREEZE_GAME message
func (gwc *GoWorldConnection) SendStartFreezeGame(gameid uint16) error {
packet := gwc.packetConn.NewPacket()
//// SendStartFreezeGame sends MT_START_FREEZE_GAME message
//func (gwc *GoWorldConnection) SendStartFreezeGame(gameid uint16) error {
// packet := gwc.packetConn.NewPacket()
// packet.AppendUint16(MT_START_FREEZE_GAME)
// return gwc.SendPacketRelease(packet)
//}

func AllocStartFreezeGamePacket() *netutil.Packet {
packet := netutil.NewPacket()
packet.AppendUint16(MT_START_FREEZE_GAME)
return gwc.SendPacketRelease(packet)
return packet
}

func MakeNotifyGameConnectedPacket(gameid uint16) *netutil.Packet {
Expand Down

0 comments on commit fef6bb0

Please sign in to comment.