buffer messages as long as seq numbers in between haven't been received

This commit is contained in:
Bruno Windels 2022-06-02 10:59:14 +02:00
parent a139571e20
commit 513c059459

View file

@ -18,7 +18,7 @@ import {PeerCall, CallState} from "../PeerCall";
import {makeTxnId, makeId} from "../../common"; import {makeTxnId, makeId} from "../../common";
import {EventType, CallErrorCode} from "../callEventTypes"; import {EventType, CallErrorCode} from "../callEventTypes";
import {formatToDeviceMessagesPayload} from "../../common"; import {formatToDeviceMessagesPayload} from "../../common";
import {sortedIndex} from "../../../utils/sortedIndex";
import type {MuteSettings} from "../common"; import type {MuteSettings} from "../common";
import type {Options as PeerCallOptions, RemoteMedia} from "../PeerCall"; import type {Options as PeerCallOptions, RemoteMedia} from "../PeerCall";
import type {LocalMedia} from "../LocalMedia"; import type {LocalMedia} from "../LocalMedia";
@ -53,6 +53,8 @@ const errorCodesWithoutRetry = [
class MemberConnection { class MemberConnection {
public retryCount: number = 0; public retryCount: number = 0;
public peerCall?: PeerCall; public peerCall?: PeerCall;
public lastProcessedSeqNr: number | undefined;
public queuedSignallingMessages: SignallingMessage<MGroupCallBase>[] = [];
public outboundSeqCounter: number = 0; public outboundSeqCounter: number = 0;
constructor( constructor(
@ -253,11 +255,19 @@ export class Member {
if (message.type === EventType.Invite && !connection.peerCall) { if (message.type === EventType.Invite && !connection.peerCall) {
connection.peerCall = this._createPeerCall(message.content.call_id); connection.peerCall = this._createPeerCall(message.content.call_id);
} }
const idx = sortedIndex(connection.queuedSignallingMessages, message, (a, b) => a.content.seq - b.content.seq);
connection.queuedSignallingMessages.splice(idx, 0, message);
if (connection.peerCall) { if (connection.peerCall) {
const item = connection.peerCall.handleIncomingSignallingMessage(message, this.deviceId, connection.logItem); while (
syncLog.refDetached(item); connection.queuedSignallingMessages.length && (
} else { connection.lastProcessedSeqNr === undefined ||
// TODO: need to buffer events until invite comes? connection.queuedSignallingMessages[0].content.seq === connection.lastProcessedSeqNr + 1
)) {
const dequeuedMessage = connection.queuedSignallingMessages.shift()!;
const item = connection.peerCall!.handleIncomingSignallingMessage(dequeuedMessage, this.deviceId, connection.logItem);
connection.lastProcessedSeqNr = dequeuedMessage.content.seq;
syncLog.refDetached(item);
}
} }
} else { } else {
syncLog.log({l: "member not connected", userId: this.userId, deviceId: this.deviceId}); syncLog.log({l: "member not connected", userId: this.userId, deviceId: this.deviceId});