From 01274e994370d487fa8a0e3c7af93b1ce896cc30 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Tue, 5 Apr 2022 16:41:57 +0300 Subject: [PATCH] Apply new time offset on connection status change --- src/lib/mtproto/networker.ts | 16 ++++++++++++++-- src/lib/mtproto/timeManager.ts | 29 ++++++++++++++++------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/lib/mtproto/networker.ts b/src/lib/mtproto/networker.ts index 9a41da01..8ec148a0 100644 --- a/src/lib/mtproto/networker.ts +++ b/src/lib/mtproto/networker.ts @@ -184,6 +184,7 @@ export default class MTPNetworker { //private debugRequests: Array<{before: Uint8Array, after: Uint8Array}> = []; private delays: typeof delays[keyof typeof delays]; + private getNewTimeOffset: boolean; constructor( public dcId: number, @@ -889,6 +890,8 @@ export default class MTPNetworker { this.clearPingDelayDisconnect(); this.sendPingDelayDisconnect(); } + + this.getNewTimeOffset = true; } /* if(this.onConnectionStatusChange) { this.onConnectionStatusChange(this.isOnline); @@ -1640,6 +1643,10 @@ export default class MTPNetworker { }); } */ + private applyServerTime(messageId: string) { + return timeManager.applyServerTime(bigInt(messageId).shiftRight(32).toJSNumber()); + } + // * https://core.telegram.org/mtproto/service_messages_about_messages#notice-of-ignored-error-message public processMessage(message: any, messageId: MTLong, sessionId: Uint8Array | number[]) { if(message._ === 'messageEmpty') { @@ -1663,6 +1670,12 @@ export default class MTPNetworker { this.pingDelayDisconnectDeferred.resolve('any message'); } + let changedTimeOffset: boolean; + if(this.getNewTimeOffset) { + changedTimeOffset = this.applyServerTime(messageId); + this.getNewTimeOffset = undefined; + } + switch(message._) { case 'msg_container': { for(const innerMessage of message.messages) { @@ -1704,8 +1717,7 @@ export default class MTPNetworker { case 32: // * msg_seqno too low case 33: // * msg_seqno too high case 64: { // * invalid container - const changedOffset = timeManager.applyServerTime(bigInt(messageId).shiftRight(32).toJSNumber()); - if(message.error_code === 17 || changedOffset) { + if(message.error_code === 17 || changedTimeOffset) { this.log('Update session'); this.updateSession(); } diff --git a/src/lib/mtproto/timeManager.ts b/src/lib/mtproto/timeManager.ts index 380cb3d1..998cbe68 100644 --- a/src/lib/mtproto/timeManager.ts +++ b/src/lib/mtproto/timeManager.ts @@ -70,23 +70,26 @@ export class TimeManager { localTime = (localTime || Date.now()) / 1000 | 0; const newTimeOffset = serverTime - localTime; const changed = Math.abs(this.timeOffset - newTimeOffset) > 10; - sessionStorage.set({ - server_time_offset: newTimeOffset - }); - this.lastMessageId = [0, 0]; - this.timeOffset = newTimeOffset; + + if(this.timeOffset !== newTimeOffset) { + sessionStorage.set({ + server_time_offset: newTimeOffset + }); + + this.timeOffset = newTimeOffset; + + /// #if MTPROTO_WORKER + const task: ApplyServerTimeOffsetTask = { + type: 'applyServerTimeOffset', + payload: newTimeOffset + }; + notifySomeone(task); + /// #endif + } //console.log('[TimeManager]: Apply server time', serverTime, localTime, newTimeOffset, changed); - /// #if MTPROTO_WORKER - const task: ApplyServerTimeOffsetTask = { - type: 'applyServerTimeOffset', - payload: newTimeOffset - }; - notifySomeone(task); - /// #endif - return changed; } }