|
|
|
@ -24,9 +24,9 @@ import DEBUG from '../../config/debug';
@@ -24,9 +24,9 @@ import DEBUG from '../../config/debug';
|
|
|
|
|
import Modes from '../../config/modes'; |
|
|
|
|
import noop from '../../helpers/noop'; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
import HTTP from './transports/http'; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
import type TcpObfuscated from './transports/tcpObfuscated'; |
|
|
|
|
import bigInt from 'big-integer'; |
|
|
|
@ -135,7 +135,7 @@ export default class MTPNetworker {
@@ -135,7 +135,7 @@ export default class MTPNetworker {
|
|
|
|
|
private nextReqTimeout: number; |
|
|
|
|
private nextReq: number = 0; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
private longPollInterval: number; |
|
|
|
|
private longPollPending: number; |
|
|
|
|
private checkConnectionRetryAt: number; |
|
|
|
@ -144,7 +144,7 @@ export default class MTPNetworker {
@@ -144,7 +144,7 @@ export default class MTPNetworker {
|
|
|
|
|
private sleepAfter: number; |
|
|
|
|
private offline = false; |
|
|
|
|
private sendingLongPoll: boolean; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
private seqNo: number; |
|
|
|
|
private prevSessionId: Uint8Array; |
|
|
|
@ -173,13 +173,13 @@ export default class MTPNetworker {
@@ -173,13 +173,13 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
public transport: MTTransport; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
private pingDelayDisconnectDeferred: CancellablePromise<string>; |
|
|
|
|
private pingPromise: Promise<void>; |
|
|
|
|
// private pingInterval: number;
|
|
|
|
|
private lastPingTime: number; |
|
|
|
|
private lastPingDelayDisconnectId: string; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
//public onConnectionStatusChange: (online: boolean) => void;
|
|
|
|
|
|
|
|
|
|
//private debugRequests: Array<{before: Uint8Array, after: Uint8Array}> = [];
|
|
|
|
@ -413,19 +413,19 @@ export default class MTPNetworker {
@@ -413,19 +413,19 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
this.connectionInited = false; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
if(this.longPollInterval !== undefined) { |
|
|
|
|
clearInterval(this.longPollInterval); |
|
|
|
|
this.longPollInterval = undefined; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.clearCheckConnectionTimeout(); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.log('change transport', transport, oldTransport); |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
this.clearPingDelayDisconnect(); |
|
|
|
|
|
|
|
|
|
// if(this.pingInterval !== undefined) {
|
|
|
|
@ -434,7 +434,7 @@ export default class MTPNetworker {
@@ -434,7 +434,7 @@ export default class MTPNetworker {
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// this.clearPing();
|
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
this.transport = transport; |
|
|
|
|
if(!transport) { |
|
|
|
@ -443,19 +443,19 @@ export default class MTPNetworker {
@@ -443,19 +443,19 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
transport.networker = this; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(transport instanceof HTTP) { |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
this.longPollInterval = ctx.setInterval(this.checkLongPoll, 10000); |
|
|
|
|
this.checkLongPoll(); |
|
|
|
|
this.checkConnection('changed transport'); |
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
// * handle outcoming dead socket, server will close the connection
|
|
|
|
|
if((transport as TcpObfuscated).connection) { |
|
|
|
|
// this.sendPingDelayDisconnect();
|
|
|
|
@ -467,7 +467,7 @@ export default class MTPNetworker {
@@ -467,7 +467,7 @@ export default class MTPNetworker {
|
|
|
|
|
// this.pingInterval = ctx.setInterval(this.sendPing, PING_INTERVAL);
|
|
|
|
|
// this.sendPing();
|
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
this.resend(); |
|
|
|
|
} |
|
|
|
@ -612,7 +612,7 @@ export default class MTPNetworker {
@@ -612,7 +612,7 @@ export default class MTPNetworker {
|
|
|
|
|
.finally(onFinally); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
private checkLongPoll = () => { |
|
|
|
|
const isClean = this.cleanupSent(); |
|
|
|
|
//this.log.error('Check lp', this.longPollPending, this.dcId, isClean, this);
|
|
|
|
@ -725,20 +725,20 @@ export default class MTPNetworker {
@@ -725,20 +725,20 @@ export default class MTPNetworker {
|
|
|
|
|
this.checkConnectionTimeout = ctx.setTimeout(() => this.checkConnection('from toggleOfline'), delay); |
|
|
|
|
this.checkConnectionPeriod = Math.min(30, (1 + this.checkConnectionPeriod) * 1.5); |
|
|
|
|
|
|
|
|
|
/// #if !MTPROTO_WORKER
|
|
|
|
|
// #if !MTPROTO_WORKER
|
|
|
|
|
document.body.addEventListener('online', this.checkConnection, false); |
|
|
|
|
document.body.addEventListener('focus', this.checkConnection, false); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
} else { |
|
|
|
|
this.setConnectionStatus(ConnectionStatus.Connected); |
|
|
|
|
this.checkLongPoll(); |
|
|
|
|
|
|
|
|
|
this.scheduleRequest(); |
|
|
|
|
|
|
|
|
|
/// #if !MTPROTO_WORKER
|
|
|
|
|
// #if !MTPROTO_WORKER
|
|
|
|
|
document.body.removeEventListener('online', this.checkConnection); |
|
|
|
|
document.body.removeEventListener('focus', this.checkConnection); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -784,7 +784,7 @@ export default class MTPNetworker {
@@ -784,7 +784,7 @@ export default class MTPNetworker {
|
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
// тут можно сделать таймаут и выводить дисконнект
|
|
|
|
|
private pushMessage(message: { |
|
|
|
@ -1029,10 +1029,10 @@ export default class MTPNetworker {
@@ -1029,10 +1029,10 @@ export default class MTPNetworker {
|
|
|
|
|
//const currentTime = Date.now();
|
|
|
|
|
let messagesByteLen = 0; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
let hasApiCall = false; |
|
|
|
|
let hasHttpWait = false; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
let lengthOverflow = false; |
|
|
|
|
|
|
|
|
@ -1062,13 +1062,13 @@ export default class MTPNetworker {
@@ -1062,13 +1062,13 @@ export default class MTPNetworker {
|
|
|
|
|
messages.push(message); |
|
|
|
|
messagesByteLen += messageByteLength; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
if(message.isAPI) { |
|
|
|
|
hasApiCall = true; |
|
|
|
|
} else if(message.longPoll) { |
|
|
|
|
hasHttpWait = true; |
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
outMessage = message; |
|
|
|
|
} else { |
|
|
|
@ -1079,10 +1079,10 @@ export default class MTPNetworker {
@@ -1079,10 +1079,10 @@ export default class MTPNetworker {
|
|
|
|
|
//}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(this.transport instanceof HTTP) |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
if(hasApiCall && !hasHttpWait) { |
|
|
|
|
const serializer = new TLSerialization({mtproto: true}); |
|
|
|
|
serializer.storeMethod('http_wait', { |
|
|
|
@ -1097,16 +1097,16 @@ export default class MTPNetworker {
@@ -1097,16 +1097,16 @@ export default class MTPNetworker {
|
|
|
|
|
body: serializer.getBytes(true) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
if(!messages.length) { |
|
|
|
|
// this.log('no scheduled messages')
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
const noResponseMsgs: Array<string> = messages.filter((message) => message.noResponse).map((message) => message.msg_id); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
if(messages.length > 1) { |
|
|
|
|
const container = this.generateContainerMessage(messagesByteLen, messages); |
|
|
|
@ -1121,19 +1121,19 @@ export default class MTPNetworker {
@@ -1121,19 +1121,19 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
const promise = this.sendEncryptedRequest(outMessage); |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(this.transport instanceof HTTP) |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
this.handleSentEncryptedRequestHTTP(promise, outMessage, noResponseMsgs); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
if(!(this.transport instanceof HTTP)) |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
this.cleanupSent(); // ! WARNING
|
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
if(lengthOverflow) { |
|
|
|
|
this.scheduleRequest(); |
|
|
|
@ -1302,13 +1302,13 @@ export default class MTPNetworker {
@@ -1302,13 +1302,13 @@ export default class MTPNetworker {
|
|
|
|
|
const promise: Promise<Uint8Array> = this.transport ? this.transport.send(requestData) as any : Promise.reject({}); |
|
|
|
|
// this.debug && this.log.debug('sendEncryptedRequest: launched message into space:', message, promise);
|
|
|
|
|
|
|
|
|
|
/// #if !MTPROTO_HAS_HTTP
|
|
|
|
|
// #if !MTPROTO_HAS_HTTP
|
|
|
|
|
return promise; |
|
|
|
|
/// #else
|
|
|
|
|
// #else
|
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(!(this.transport instanceof HTTP)) return promise; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
const baseError: ApiError = { |
|
|
|
|
code: 406, |
|
|
|
@ -1334,7 +1334,7 @@ export default class MTPNetworker {
@@ -1334,7 +1334,7 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
throw error; |
|
|
|
|
}); |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public parseResponse(responseBuffer: Uint8Array) { |
|
|
|
@ -1475,19 +1475,19 @@ export default class MTPNetworker {
@@ -1475,19 +1475,19 @@ export default class MTPNetworker {
|
|
|
|
|
return; |
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(this.transport instanceof HTTP) { |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
if(this.offline) { |
|
|
|
|
this.checkConnection('forced schedule'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delay ||= 0; // set zero timeout to pack other messages too
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
const nextReq = Date.now() + (delay || 0); |
|
|
|
|
if(this.nextReq && (delay === undefined || this.nextReq <= nextReq)) { |
|
|
|
@ -1512,15 +1512,15 @@ export default class MTPNetworker {
@@ -1512,15 +1512,15 @@ export default class MTPNetworker {
|
|
|
|
|
this.nextReqTimeout = 0; |
|
|
|
|
this.nextReq = 0; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(this.transport instanceof HTTP) |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
if(this.offline) { |
|
|
|
|
//this.log('Cancel scheduled');
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
this.performScheduledRequest(); |
|
|
|
|
}; |
|
|
|
@ -1540,12 +1540,12 @@ export default class MTPNetworker {
@@ -1540,12 +1540,12 @@ export default class MTPNetworker {
|
|
|
|
|
|
|
|
|
|
let delay: number; |
|
|
|
|
|
|
|
|
|
/// #if MTPROTO_HAS_HTTP
|
|
|
|
|
/// #if MTPROTO_HAS_WS
|
|
|
|
|
// #if MTPROTO_HAS_HTTP
|
|
|
|
|
// #if MTPROTO_HAS_WS
|
|
|
|
|
if(this.transport instanceof HTTP) |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
delay = 30000; |
|
|
|
|
/// #endif
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
this.scheduleRequest(delay); |
|
|
|
|
} |
|
|
|
|