1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-26 02:25:58 +01:00

Minor tweaks to file transfer.

Avoid copying data when sending, improve error handling.
This commit is contained in:
Juliusz Chroboczek 2023-01-22 22:24:51 +01:00
parent dbeb75ee0e
commit 58ef60f974

View file

@ -1685,7 +1685,8 @@ TransferredFile.prototype.close = function() {
} }
/** /**
* Buffer a chunk of data received during a file transfer. Do not call this. * Buffer a chunk of data received during a file transfer.
* Do not call this, it is called automatically when data is received.
* *
* @param {Blob|ArrayBuffer} data * @param {Blob|ArrayBuffer} data
*/ */
@ -1884,7 +1885,8 @@ TransferredFile.prototype.receive = async function() {
} }
/** /**
* Negotiate a file transfer on the sender side. Don't call this. * Negotiate a file transfer on the sender side.
* Don't call this, it is called automatically we receive an offer.
* *
* @param {string} sdp * @param {string} sdp
*/ */
@ -1974,6 +1976,7 @@ TransferredFile.prototype.send = async function() {
f.dc.bufferedAmountLowThreshold = 65536; f.dc.bufferedAmountLowThreshold = 65536;
/** @param {Uint8Array} a */
async function write(a) { async function write(a) {
while(f.dc.bufferedAmount > f.dc.bufferedAmountLowThreshold) { while(f.dc.bufferedAmount > f.dc.bufferedAmountLowThreshold) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
@ -2012,9 +2015,7 @@ TransferredFile.prototype.send = async function() {
await write(data); await write(data);
} else { } else {
for(let i = 0; i < v.value.length; i += 16384) { for(let i = 0; i < v.value.length; i += 16384) {
let d = new Uint8Array( let d = data.subarray(i, Math.min(i + 16384, data.length));
data.buffer, i, Math.min(16384, data.length - i),
);
await write(d); await write(d);
} }
} }
@ -2056,13 +2057,19 @@ TransferredFile.prototype.receiveData = async function(data) {
f.dc.onmessage = null; f.dc.onmessage = null;
if(f.datalen != f.size) { if(f.datalen != f.size) {
f.cancel('unexpected file size'); f.cancel('extra data at end of file');
return; return;
} }
let blob = f.getBufferedData(); let blob = f.getBufferedData();
if(blob.size != f.size) {
f.cancel("inconsistent data size (this shouldn't happen)");
return;
}
f.event('done', blob); f.event('done', blob);
// we've received the whole file. Send the final handshake, but don't
// complain if the peer has closed the channel in the meantime.
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
let timer = setTimeout(function(e) { resolve(); }, 2000); let timer = setTimeout(function(e) { resolve(); }, 2000);
f.dc.onclose = function(e) { f.dc.onclose = function(e) {
@ -2100,8 +2107,10 @@ ServerConnection.prototype.fileTransfer = function(id, username, message) {
try { try {
if(sc.onfiletransfer) if(sc.onfiletransfer)
sc.onfiletransfer.call(sc, f); sc.onfiletransfer.call(sc, f);
else else {
f.cancel('this client does not implement file transfer'); f.cancel('this client does not implement file transfer');
return;
}
} catch(e) { } catch(e) {
f.cancel(e); f.cancel(e);
return; return;