Enhanced backend udp tunnel workflow.

This commit is contained in:
genxium 2023-01-30 09:21:06 +08:00
parent 2b6cb57050
commit a35de9b83c
3 changed files with 31 additions and 45 deletions

View File

@ -622,7 +622,8 @@ func (pR *Room) OnBattleCmdReceived(pReq *pb.WsReq) {
inputsBufferSnapshot := pR.markConfirmationIfApplicable(inputFrameUpsyncBatch, playerId, player) inputsBufferSnapshot := pR.markConfirmationIfApplicable(inputFrameUpsyncBatch, playerId, player)
if nil != inputsBufferSnapshot { if nil != inputsBufferSnapshot {
pR.downsyncToAllPlayers(inputsBufferSnapshot) pR.downsyncToAllPlayers(inputsBufferSnapshot)
} else { } /*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 // no new all-confirmed
toSendInputFrameDownsyncs := pR.cloneInputsBuffer(inputFrameUpsyncBatch[0].InputFrameId, inputFrameUpsyncBatch[len(inputFrameUpsyncBatch)-1].InputFrameId+1) toSendInputFrameDownsyncs := pR.cloneInputsBuffer(inputFrameUpsyncBatch[0].InputFrameId, inputFrameUpsyncBatch[len(inputFrameUpsyncBatch)-1].InputFrameId+1)
@ -632,7 +633,7 @@ func (pR *Room) OnBattleCmdReceived(pReq *pb.WsReq) {
} }
//Logger.Info(fmt.Sprintf("OnBattleCmdReceived no new all-confirmed: roomId=%v, fromPlayerId=%v, forming peer broadcasting snapshot=%v", pR.Id, playerId, inputsBufferSnapshot)) //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) pR.broadcastPeerUpsyncForBetterPrediction(inputsBufferSnapshot)
} }*/
} }
func (pR *Room) onInputFrameDownsyncAllConfirmed(inputFrameDownsync *battle.InputFrameDownsync, playerId int32) { func (pR *Room) onInputFrameDownsyncAllConfirmed(inputFrameDownsync *battle.InputFrameDownsync, playerId int32) {
@ -1733,7 +1734,7 @@ func (pR *Room) startBattleUdpTunnel() {
continue continue
} }
playerId := pReq.PlayerId 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 { if player, exists1 := pR.Players[playerId]; exists1 {
authKey := pReq.AuthKey authKey := pReq.AuthKey
if authKey != player.BattleUdpTunnelAuthKey { if authKey != player.BattleUdpTunnelAuthKey {
@ -1742,7 +1743,7 @@ func (pR *Room) startBattleUdpTunnel() {
} }
if _, existent := pR.PlayerDownsyncSessionDict[playerId]; existent { if _, existent := pR.PlayerDownsyncSessionDict[playerId]; existent {
player.BattleUdpTunnelAddr = remote 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) nowBattleState := atomic.LoadInt32(&pR.State)
if RoomBattleStateIns.IN_BATTLE == nowBattleState { 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)) 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
} }
} }

View File

@ -37,39 +37,16 @@ window.PlayerBattleState = {
window.onUdpMessage = (args) => { window.onUdpMessage = (args) => {
const self = window.mapIns; const self = window.mapIns;
const ui8Arr = args; 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(`#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...`);
const req = window.pb.protos.WsReq.decode(ui8Arr); const req = window.pb.protos.WsReq.decode(ui8Arr);
if (req) { 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) { if (req.act && window.UPSYNC_MSG_ACT_PLAYER_CMD == req.act) {
let effCnt = 0; let effCnt = 0;
const renderedInputFrameIdUpper = gopkgs.ConvertToDelayedInputFrameId(self.renderFrameId); const renderedInputFrameIdUpper = gopkgs.ConvertToDelayedInputFrameId(self.renderFrameId);
const peerJoinIndex = req.joinIndex; const peerJoinIndex = req.joinIndex;
const batch = req.inputFrameUpsyncBatch; const batch = req.inputFrameUpsyncBatch;
for (let k in batch) { self.onPeerInputFrameUpsync(peerJoinIndex, 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);
}
} }
} }
}; };
@ -222,7 +199,7 @@ cc.Class({
*/ */
if (null == currSelfInput) return false; if (null == currSelfInput) return false;
const shouldUpsyncForEarlyAllConfirmedOnBackend = (currInputFrameId - lastUpsyncInputFrameId >= 1); const shouldUpsyncForEarlyAllConfirmedOnBackend = (currInputFrameId - lastUpsyncInputFrameId >= this.inputFrameUpsyncDelayTolerance);
return shouldUpsyncForEarlyAllConfirmedOnBackend || (prevSelfInput != currSelfInput); return shouldUpsyncForEarlyAllConfirmedOnBackend || (prevSelfInput != currSelfInput);
}, },
@ -532,6 +509,7 @@ cc.Class({
console.log(`Received parsedBattleColliderInfo via ws`); 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". // 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); Object.assign(self, parsedBattleColliderInfo);
self.inputFrameUpsyncDelayTolerance = parsedBattleColliderInfo.inputFrameUpsyncDelayTolerance;
const tiledMapIns = self.node.getComponent(cc.TiledMap); const tiledMapIns = self.node.getComponent(cc.TiledMap);
@ -911,7 +889,7 @@ batchInputFrameIdRange=[${batch[0].inputFrameId}, ${batch[batch.length - 1].inpu
if (!self.recentInputCache) { if (!self.recentInputCache) {
return; return;
} }
if (ALL_BATTLE_STATES.IN_SETTLEMENT == self.battleState) { if (ALL_BATTLE_STATES.IN_BATTLE != self.battleState) {
return; return;
} }
@ -930,13 +908,19 @@ batchInputFrameIdRange=[${batch[0].inputFrameId}, ${batch[batch.length - 1].inpu
} }
self.getOrPrefabInputFrameUpsync(inputFrameDownsyncId); // Make sure that inputFrame exists locally self.getOrPrefabInputFrameUpsync(inputFrameDownsyncId); // Make sure that inputFrame exists locally
const existingInputFrame = self.recentInputCache.GetByFrameId(inputFrameDownsyncId); const existingInputFrame = self.recentInputCache.GetByFrameId(inputFrameDownsyncId);
if (0 < (existingInputFrame.confirmedList & (1 << (peerJoinIndex - 1)))) { if (0 < (existingInputFrame.ConfirmedList & (1 << (peerJoinIndex - 1)))) {
continue; continue;
} }
effCnt += 1; effCnt += 1;
existingInputFrame.confirmedList |= (1 << (peerJoinIndex - 1)); // the returned "gopkgs.NewInputFrameDownsync.InputList" is immutable, thus we can only modify the values in "newInputList" and "newConfirmedList"!
existingInputFrame.InputList[peerJoinIndex - 1] = inputFrameDownsync.inputList[peerJoinIndex - 1]; // No need to change "confirmedList", leave it to "onInputFrameDownsyncBatch" -- we're just helping prediction here let newInputList = new Array(self.playerRichInfoDict.size).fill(0);
self.recentInputCache.SetByFrameId(existingInputFrame, inputFrameDownsyncId); 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) { if (0 < effCnt) {
self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId); self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId);

View File

@ -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; struct sockaddr_in const* sockAddr = (struct sockaddr_in const*)addr;
uv_inet_ntop(sockAddr->sin_family, &(sockAddr->sin_addr), ip, INET_ADDRSTRLEN); uv_inet_ntop(sockAddr->sin_family, &(sockAddr->sin_addr), ip, INET_ADDRSTRLEN);
port = ntohs(sockAddr->sin_port); 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; break;
} }
default: default: