Skip to content

Commit

Permalink
Ensure image receive order.
Browse files Browse the repository at this point in the history
  • Loading branch information
jq-rs committed Oct 2, 2021
1 parent 781af3e commit 755644b
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions js/mlestalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2019-2020 MlesTalk developers
* Copyright (c) 2019-2021 MlesTalk developers
*/
let gMyName = '';
let gMyChannel = '';
Expand Down Expand Up @@ -47,18 +47,18 @@ const PRESENCE_SHOW_TIMER = 5000; /* ms */
const RETIMEOUT = 1500; /* ms */
const MAXTIMEOUT = 1000 * 60 * 5; /* ms */
const MAXQLEN = 32;
const RESYNC_TIMEOUT = 2000; /* ms */
const RESYNC_TIMEOUT = 5000; /* ms */
const LED_ON_TIME = 500; /* ms */
const LED_OFF_TIME = 2500; /* ms */
const SCROLL_TIME = 500; /* ms */
const ASYNC_SLEEP = 1 /* ms */
const ASYNC_SLEEP = 3 /* ms */
let gReconnTimeout = RETIMEOUT;
let gReconnAttempts = 0;

let gMultipartDict = {};
let gMultipartSendDict = {};
let gMultipartContinue = false;
const MULTIPART_SLICE = 768; //B
const MULTIPART_SLICE = 4096; //B

const DATELEN = 13;

Expand Down Expand Up @@ -638,7 +638,7 @@ async function processData(uid, channel, msgTimestamp,
message, isFull, isPresence, isPresenceAck, presAckRequired, isImage,
isMultipart, isFirst, isLast, fsEnabled) {

await sleep(++gReadMsgDelayedQueueLen);
await sleep(++gReadMsgDelayedQueueLen*ASYNC_SLEEP);
gReadMsgDelayedQueueLen--;

//update hash
Expand Down Expand Up @@ -673,20 +673,29 @@ async function processData(uid, channel, msgTimestamp,

const mHash = hashMessage(uid, isFull ? msgTimestamp + message + '\n' : msgTimestamp + message);
if (isMultipart) {
//strip index
const index = message.substr(0,4);
const numIndex = parseInt(index);
message = message.substr(4);
//console.log("Received image index " + numIndex);

if (!gMultipartDict[get_uniq(uid, channel)]) {
if (!isFirst) {
//invalid frame
return 0;
}
gMultipartDict[get_uniq(uid, channel)] = "";
gMultipartDict[get_uniq(uid, channel)] = {};
}
// handle multipart hashing here
if (msgHashHandle(uid, channel, msgTimestamp, mHash)) {
gMultipartDict[get_uniq(uid, channel)] += message;
gMultipartDict[get_uniq(uid, channel)][numIndex] = message;
if (!isLast) {
return 0;
}
message = gMultipartDict[get_uniq(uid, channel)];
message = "";
for (let i = 0; i <= numIndex; i++) {
message += gMultipartDict[get_uniq(uid, channel)][i];
}
gMultipartDict[get_uniq(uid, channel)] = null;
}
else
Expand Down Expand Up @@ -1098,18 +1107,28 @@ async function sendDataurl(dataUrl, uid, channel) {
msgtype |= MSGISMULTIPART;
gMultipartSendDict[get_uniq(uid, channel)] = true;
for (let i = 0; i < dataUrl.length; i += MULTIPART_SLICE) {
let data = "";
if(i/MULTIPART_SLICE < 10)
data += "000";
else if(i/MULTIPART_SLICE < 100)
data += "00";
else if(i/MULTIPART_SLICE < 1000)
data += "0";
data += (i/MULTIPART_SLICE).toString();
//console.log("Adding image index " + data);

if (0 == i) {
msgtype |= MSGISFIRST;
}
else if (i + MULTIPART_SLICE >= dataUrl.length) {
msgtype |= MSGISLAST;
let data = dataUrl.slice(i, dataUrl.length);
data += dataUrl.slice(i, dataUrl.length);
sendData("send", gMyName, gMyChannel, data, msgtype);
gMultipartSendDict[get_uniq(uid, channel)] = false;
gMultipartContinue = false;
break;
}
let data = dataUrl.slice(i, i + MULTIPART_SLICE);
data += dataUrl.slice(i, i + MULTIPART_SLICE);
sendData("send", gMyName, gMyChannel, data, msgtype);
while (false == gMultipartContinue) {
await sleep(ASYNC_SLEEP);
Expand Down

0 comments on commit 755644b

Please sign in to comment.