diff --git a/battle_srv/models/room.go b/battle_srv/models/room.go index a4c157f..0d1097b 100644 --- a/battle_srv/models/room.go +++ b/battle_srv/models/room.go @@ -622,17 +622,18 @@ func (pR *Room) OnBattleCmdReceived(pReq *pb.WsReq) { inputsBufferSnapshot := pR.markConfirmationIfApplicable(inputFrameUpsyncBatch, playerId, player) if nil != inputsBufferSnapshot { pR.downsyncToAllPlayers(inputsBufferSnapshot) - } else { - // no new all-confirmed - toSendInputFrameDownsyncs := pR.cloneInputsBuffer(inputFrameUpsyncBatch[0].InputFrameId, inputFrameUpsyncBatch[len(inputFrameUpsyncBatch)-1].InputFrameId+1) + } /*else { + // FIXME: Enable this block after we can proactively detect whether there's any "secondary ws session player" in the battle to avoid waste of resource in creating the snapshot + // no new all-confirmed + toSendInputFrameDownsyncs := pR.cloneInputsBuffer(inputFrameUpsyncBatch[0].InputFrameId, inputFrameUpsyncBatch[len(inputFrameUpsyncBatch)-1].InputFrameId+1) - inputsBufferSnapshot = &pb.InputsBufferSnapshot{ - ToSendInputFrameDownsyncs: toSendInputFrameDownsyncs, - PeerJoinIndex: player.JoinIndex, - } - //Logger.Info(fmt.Sprintf("OnBattleCmdReceived no new all-confirmed: roomId=%v, fromPlayerId=%v, forming peer broadcasting snapshot=%v", pR.Id, playerId, inputsBufferSnapshot)) - pR.broadcastPeerUpsyncForBetterPrediction(inputsBufferSnapshot) - } + inputsBufferSnapshot = &pb.InputsBufferSnapshot{ + ToSendInputFrameDownsyncs: toSendInputFrameDownsyncs, + PeerJoinIndex: player.JoinIndex, + } + //Logger.Info(fmt.Sprintf("OnBattleCmdReceived no new all-confirmed: roomId=%v, fromPlayerId=%v, forming peer broadcasting snapshot=%v", pR.Id, playerId, inputsBufferSnapshot)) + pR.broadcastPeerUpsyncForBetterPrediction(inputsBufferSnapshot) + }*/ } func (pR *Room) onInputFrameDownsyncAllConfirmed(inputFrameDownsync *battle.InputFrameDownsync, playerId int32) { @@ -1733,7 +1734,7 @@ func (pR *Room) startBattleUdpTunnel() { continue } playerId := pReq.PlayerId - Logger.Info(fmt.Sprintf("`BattleUdpTunnel` for roomId=%d received decoded WsReq:", pR.Id), zap.Any("pReq", pReq)) + //Logger.Info(fmt.Sprintf("`BattleUdpTunnel` for roomId=%d received decoded WsReq:", pR.Id), zap.Any("pReq", pReq)) if player, exists1 := pR.Players[playerId]; exists1 { authKey := pReq.AuthKey if authKey != player.BattleUdpTunnelAuthKey { @@ -1742,7 +1743,7 @@ func (pR *Room) startBattleUdpTunnel() { } if _, existent := pR.PlayerDownsyncSessionDict[playerId]; existent { player.BattleUdpTunnelAddr = remote - Logger.Info(fmt.Sprintf("`BattleUdpTunnel` for roomId=%d updated battleUdpAddr for playerId=%d to be %s\n", pR.Id, playerId, remote)) + //Logger.Info(fmt.Sprintf("`BattleUdpTunnel` for roomId=%d updated battleUdpAddr for playerId=%d to be %s\n", pR.Id, playerId, remote)) nowBattleState := atomic.LoadInt32(&pR.State) if RoomBattleStateIns.IN_BATTLE == nowBattleState { @@ -1759,6 +1760,7 @@ func (pR *Room) startBattleUdpTunnel() { Logger.Warn(fmt.Sprintf("`BattleUdpTunnel` for roomId=%d failed to forward upsync from (playerId:%d, joinIndex:%d, addr:%s) to (otherPlayerId:%d, otherPlayerJoinIndex:%d, otherPlayerAddr:%s)\n", pR.Id, playerId, peerJoinIndex, remote, otherPlayer.Id, otherPlayer.JoinIndex, otherPlayer.BattleUdpTunnelAddr)) } } + pR.OnBattleCmdReceived(pReq) // To help advance "pR.LastAllConfirmedInputFrameId" asap } } diff --git a/frontend/assets/scripts/Map.js b/frontend/assets/scripts/Map.js index 7033c4a..c988072 100644 --- a/frontend/assets/scripts/Map.js +++ b/frontend/assets/scripts/Map.js @@ -37,39 +37,16 @@ window.PlayerBattleState = { window.onUdpMessage = (args) => { const self = window.mapIns; const ui8Arr = args; - cc.log(`#1 Js called back by CPP: onUdpMessage: args=${args}, typeof(args)=${typeof (args)}, argslen=${args.length}, ui8Arr=${ui8Arr}`); - - cc.log(`#2 Js called back by CPP for upsync: trying to decode by WsReq...`); + //cc.log(`#1 Js called back by CPP: onUdpMessage: args=${args}, typeof(args)=${typeof (args)}, argslen=${args.length}, ui8Arr=${ui8Arr}`); const req = window.pb.protos.WsReq.decode(ui8Arr); if (req) { - cc.log(`#2 Js called back by CPP for upsync: onUdpMessage: ${JSON.stringify(req)}`); + //cc.log(`#2 Js called back by CPP for upsync: onUdpMessage: ${JSON.stringify(req)}`); if (req.act && window.UPSYNC_MSG_ACT_PLAYER_CMD == req.act) { let effCnt = 0; const renderedInputFrameIdUpper = gopkgs.ConvertToDelayedInputFrameId(self.renderFrameId); const peerJoinIndex = req.joinIndex; const batch = req.inputFrameUpsyncBatch; - for (let k in batch) { - const inputFrameUpsync = batch[k]; - if (inputFrameUpsync.inputFrameId < renderedInputFrameIdUpper) { - // Avoid obfuscating already rendered history - continue; - } - if (inputFrameUpsync.inputFrameId <= self.lastAllConfirmedInputFrameId) { - continue; - } - self.getOrPrefabInputFrameUpsync(inputFrameUpsync.inputFrameId); // Make sure that inputFrame exists locally - const existingInputFrame = self.recentInputCache.GetByFrameId(inputFrameUpsync.inputFrameId); - if (0 < (existingInputFrame.confirmedList & (1 << (peerJoinIndex - 1)))) { - continue; - } - effCnt += 1; - existingInputFrame.InputList[inputFrameUpsync.joinIndex - 1] = inputFrameUpsync.encoded; - existingInputFrame.confirmedList |= (1 << (peerJoinIndex - 1)); - self.recentInputCache.SetByFrameId(existingInputFrame, inputFrameUpsync.inputFrameId); - } - if (0 < effCnt) { - self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId); - } + self.onPeerInputFrameUpsync(peerJoinIndex, batch); } } }; @@ -222,7 +199,7 @@ cc.Class({ */ if (null == currSelfInput) return false; - const shouldUpsyncForEarlyAllConfirmedOnBackend = (currInputFrameId - lastUpsyncInputFrameId >= 1); + const shouldUpsyncForEarlyAllConfirmedOnBackend = (currInputFrameId - lastUpsyncInputFrameId >= this.inputFrameUpsyncDelayTolerance); return shouldUpsyncForEarlyAllConfirmedOnBackend || (prevSelfInput != currSelfInput); }, @@ -532,6 +509,7 @@ cc.Class({ console.log(`Received parsedBattleColliderInfo via ws`); // TODO: Upon reconnection, the backend might have already been sending down data that'd trigger "onRoomDownsyncFrame & onInputFrameDownsyncBatch", but frontend could reject those data due to "battleState != PlayerBattleState.ACTIVE". Object.assign(self, parsedBattleColliderInfo); + self.inputFrameUpsyncDelayTolerance = parsedBattleColliderInfo.inputFrameUpsyncDelayTolerance; const tiledMapIns = self.node.getComponent(cc.TiledMap); @@ -911,7 +889,7 @@ batchInputFrameIdRange=[${batch[0].inputFrameId}, ${batch[batch.length - 1].inpu if (!self.recentInputCache) { return; } - if (ALL_BATTLE_STATES.IN_SETTLEMENT == self.battleState) { + if (ALL_BATTLE_STATES.IN_BATTLE != self.battleState) { return; } @@ -930,13 +908,19 @@ batchInputFrameIdRange=[${batch[0].inputFrameId}, ${batch[batch.length - 1].inpu } self.getOrPrefabInputFrameUpsync(inputFrameDownsyncId); // Make sure that inputFrame exists locally const existingInputFrame = self.recentInputCache.GetByFrameId(inputFrameDownsyncId); - if (0 < (existingInputFrame.confirmedList & (1 << (peerJoinIndex - 1)))) { + if (0 < (existingInputFrame.ConfirmedList & (1 << (peerJoinIndex - 1)))) { continue; } effCnt += 1; - existingInputFrame.confirmedList |= (1 << (peerJoinIndex - 1)); - existingInputFrame.InputList[peerJoinIndex - 1] = inputFrameDownsync.inputList[peerJoinIndex - 1]; // No need to change "confirmedList", leave it to "onInputFrameDownsyncBatch" -- we're just helping prediction here - self.recentInputCache.SetByFrameId(existingInputFrame, inputFrameDownsyncId); + // the returned "gopkgs.NewInputFrameDownsync.InputList" is immutable, thus we can only modify the values in "newInputList" and "newConfirmedList"! + let newInputList = new Array(self.playerRichInfoDict.size).fill(0); + for (let i in existingInputFrame.InputList) { + newInputList[i] = existingInputFrame.InputList[i]; + } + let newConfirmedList = (existingInputFrame.confirmedList | (1 << (peerJoinIndex - 1))); + // No need to change "lastAllConfirmedInputFrameId", leave it to "onInputFrameDownsyncBatch" -- we're just helping prediction here + const newInputFrameDownsyncLocal = gopkgs.NewInputFrameDownsync(inputFrameDownsyncId, newInputList, newConfirmedList); + self.recentInputCache.SetByFrameId(newInputFrameDownsyncLocal, inputFrameDownsyncId); } if (0 < effCnt) { self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId); diff --git a/frontend/build-templates/jsb-link/frameworks/runtime-src/Classes/udp_session.cpp b/frontend/build-templates/jsb-link/frameworks/runtime-src/Classes/udp_session.cpp index a2d1f4b..660f893 100644 --- a/frontend/build-templates/jsb-link/frameworks/runtime-src/Classes/udp_session.cpp +++ b/frontend/build-templates/jsb-link/frameworks/runtime-src/Classes/udp_session.cpp @@ -35,7 +35,7 @@ void _onRead(uv_udp_t* req, ssize_t nread, uv_buf_t const* buf, struct sockaddr struct sockaddr_in const* sockAddr = (struct sockaddr_in const*)addr; uv_inet_ntop(sockAddr->sin_family, &(sockAddr->sin_addr), ip, INET_ADDRSTRLEN); port = ntohs(sockAddr->sin_port); - CCLOG("UDP received %d bytes from %s:%d", nread, ip, port); + //CCLOG("UDP received %d bytes from %s:%d", nread, ip, port); break; } default: