Fix the issue where the sender and receiver (without requesting existing files) have inconsistent states in resumeable file transfer with broken connections

This commit is contained in:
david_bai
2025-07-21 23:06:32 +08:00
parent 27cb5c3be8
commit 34d1897093
3 changed files with 42 additions and 3 deletions
+19
View File
@@ -15,6 +15,7 @@ import {
FileHandlers,
FileMeta,
FileRequest,
FolderComplete,
} from "@/types/webrtc";
/**
@@ -255,6 +256,24 @@ class FileReceiver {
}
}
this.currentFolderName = null;
// After the loop, the receiver has requested all necessary files.
// Send a completion message to the sender to sync the final state.
const folderComplete: FolderComplete = {
type: "FolderComplete",
folderName: folderName,
};
if (this.peerId) {
this.webrtcConnection.sendData(
JSON.stringify(folderComplete),
this.peerId
);
this.log(
"log",
`Sent folderComplete message for ${folderName} to peer ${this.peerId}`
);
}
}
// endregion
+16 -2
View File
@@ -12,7 +12,9 @@ import {
WebRTCMessage,
PeerState,
FolderMeta,
FileAck,
FileRequest,
FolderComplete,
} from "@/types/webrtc";
class FileSender {
@@ -97,9 +99,21 @@ class FileSender {
case "fileAck":
peerState.isSending = false;
this.log("log", `Received file-finish ack from peer ${peerId}`, {
fileId: (message as any).fileId,
fileId: (message as FileAck).fileId,
});
break;
case "folderComplete":
const folderName = (message as FolderComplete).folderName;
this.log(
"log",
`Received folderComplete message for ${folderName} from peer ${peerId}`
);
// The receiver has confirmed the folder is complete.
// Force the progress to 100% for the sender's UI.
if (this.pendingFolerMeta[folderName]) {
peerState.progressCallback?.(folderName, 1, 0);
}
break;
default:
this.log("warn", `Unknown signaling message type received`, {
type: message.type,
@@ -295,7 +309,7 @@ class FileSender {
// This is more robust and correct for resumed transfers.
let folderTotalSent = 0;
if (folderMeta) {
folderMeta.fileIds.forEach(fId => {
folderMeta.fileIds.forEach((fId) => {
folderTotalSent += peerState.totalBytesSent[fId] || 0;
});
}
+7 -1
View File
@@ -51,13 +51,19 @@ export interface FileEnd {
fileId: string;
}
export interface FolderComplete {
type: "FolderComplete";
folderName: string;
}
export type WebRTCMessage =
| fileMetadata
| FileRequest
| FileAck
| StringMetadata
| StringChunk
| FileEnd;
| FileEnd
| FolderComplete;
export interface FolderMeta {
totalSize: number;