Apply new time offset on connection status change

This commit is contained in:
Eduard Kuzmenko 2022-04-05 16:41:57 +03:00
parent d695dac257
commit 01274e9943
2 changed files with 30 additions and 15 deletions

View File

@ -184,6 +184,7 @@ export default class MTPNetworker {
//private debugRequests: Array<{before: Uint8Array, after: Uint8Array}> = []; //private debugRequests: Array<{before: Uint8Array, after: Uint8Array}> = [];
private delays: typeof delays[keyof typeof delays]; private delays: typeof delays[keyof typeof delays];
private getNewTimeOffset: boolean;
constructor( constructor(
public dcId: number, public dcId: number,
@ -889,6 +890,8 @@ export default class MTPNetworker {
this.clearPingDelayDisconnect(); this.clearPingDelayDisconnect();
this.sendPingDelayDisconnect(); this.sendPingDelayDisconnect();
} }
this.getNewTimeOffset = true;
} }
/* if(this.onConnectionStatusChange) { /* if(this.onConnectionStatusChange) {
this.onConnectionStatusChange(this.isOnline); 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 // * https://core.telegram.org/mtproto/service_messages_about_messages#notice-of-ignored-error-message
public processMessage(message: any, messageId: MTLong, sessionId: Uint8Array | number[]) { public processMessage(message: any, messageId: MTLong, sessionId: Uint8Array | number[]) {
if(message._ === 'messageEmpty') { if(message._ === 'messageEmpty') {
@ -1663,6 +1670,12 @@ export default class MTPNetworker {
this.pingDelayDisconnectDeferred.resolve('any message'); this.pingDelayDisconnectDeferred.resolve('any message');
} }
let changedTimeOffset: boolean;
if(this.getNewTimeOffset) {
changedTimeOffset = this.applyServerTime(messageId);
this.getNewTimeOffset = undefined;
}
switch(message._) { switch(message._) {
case 'msg_container': { case 'msg_container': {
for(const innerMessage of message.messages) { for(const innerMessage of message.messages) {
@ -1704,8 +1717,7 @@ export default class MTPNetworker {
case 32: // * msg_seqno too low case 32: // * msg_seqno too low
case 33: // * msg_seqno too high case 33: // * msg_seqno too high
case 64: { // * invalid container case 64: { // * invalid container
const changedOffset = timeManager.applyServerTime(bigInt(messageId).shiftRight(32).toJSNumber()); if(message.error_code === 17 || changedTimeOffset) {
if(message.error_code === 17 || changedOffset) {
this.log('Update session'); this.log('Update session');
this.updateSession(); this.updateSession();
} }

View File

@ -70,15 +70,15 @@ export class TimeManager {
localTime = (localTime || Date.now()) / 1000 | 0; localTime = (localTime || Date.now()) / 1000 | 0;
const newTimeOffset = serverTime - localTime; const newTimeOffset = serverTime - localTime;
const changed = Math.abs(this.timeOffset - newTimeOffset) > 10; const changed = Math.abs(this.timeOffset - newTimeOffset) > 10;
this.lastMessageId = [0, 0];
if(this.timeOffset !== newTimeOffset) {
sessionStorage.set({ sessionStorage.set({
server_time_offset: newTimeOffset server_time_offset: newTimeOffset
}); });
this.lastMessageId = [0, 0];
this.timeOffset = newTimeOffset; this.timeOffset = newTimeOffset;
//console.log('[TimeManager]: Apply server time', serverTime, localTime, newTimeOffset, changed);
/// #if MTPROTO_WORKER /// #if MTPROTO_WORKER
const task: ApplyServerTimeOffsetTask = { const task: ApplyServerTimeOffsetTask = {
type: 'applyServerTimeOffset', type: 'applyServerTimeOffset',
@ -86,6 +86,9 @@ export class TimeManager {
}; };
notifySomeone(task); notifySomeone(task);
/// #endif /// #endif
}
//console.log('[TimeManager]: Apply server time', serverTime, localTime, newTimeOffset, changed);
return changed; return changed;
} }