public async Task <MTProtoGateway> GetFileGateway(ulong salt) { await _lock.WaitAsync(); // fileGateway = new MTProtoGateway(this, fileSession); try { if (fileGateway != null) { return(fileGateway); } if (fileSession == null) { fileSession = new TelegramFileSession(Helpers.GenerateRandomUlong(), 0); } fileGateway = new MTProtoGateway(this, fileSession, false, salt); await fileGateway.ConnectAsync(); return(fileGateway); } finally { _lock.Release(); } }
private void GatewayOnBrokenSessionEvent() { logger.info("creating new session... TODO: destroy old session"); // creating new session id = Helpers.GenerateRandomUlong(); sequence = 0; gateway.Dispose(); gateway = new MTProtoGateway(MainDc, this, true, cachedSalt); gateway.UpdatesEvent += updates.ProcessUpdates; gateway.BrokenSessionEvent += GatewayOnBrokenSessionEvent; }
public async Task <TLApi> GetFileSession(int dc) { logger.debug("Getting file session for dc {0}", dc); await Established; ConfigConstructor config = (ConfigConstructor)gateway.Config; TelegramDC targetDc; if (dcs.ContainsKey(dc)) { targetDc = dcs[dc]; } else { targetDc = new TelegramDC(); foreach (var dcOption in config.dc_options) { DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption; if (optionConstructor.id == dc) { TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port); targetDc.Endpoints.Add(endpoint); } } dcs[dc] = targetDc; } MTProtoGateway fileGateway = await targetDc.GetFileGateway(gateway.Salt); TLApi fileGatewayApi = new TLApi(fileGateway); if (targetDc.FileAuthorized || dc == mainDcId) { return(fileGatewayApi); } Task <auth_ExportedAuthorization> exportAuthTask = Api.auth_exportAuthorization(dc); Auth_exportedAuthorizationConstructor exportedAuth = (Auth_exportedAuthorizationConstructor)await exportAuthTask; auth_Authorization authorization = await fileGatewayApi.auth_importAuthorization(exportedAuth.id, exportedAuth.bytes); targetDc.SaveFileAuthorization(authorization); return(fileGatewayApi); }
// save timer end public async Task ConnectAsync() { try { if (gateway == null) { logger.info("creating new mtproto gateway..."); gateway = new MTProtoGateway(MainDc, this, true, cachedSalt); gateway.UpdatesEvent += updates.ProcessUpdates; gateway.BrokenSessionEvent += GatewayOnBrokenSessionEvent; while (true) { try { await gateway.ConnectAsync(); break; } catch (MTProtoBrokenSessionException e) { logger.info("broken session exception"); } } api = new TLApi(gateway); logger.info("connection established, notifying"); //establishedTask.SetResult(null); updates.RequestDifference(); gateway.ReconnectEvent += () => { GoToOnline(); updates.RequestDifference(); }; GoToOnline(); if (!saveSessionTimerInitialized) { SaveSessionTimer(); saveSessionTimerInitialized = true; } } } catch (Exception ex) { logger.error("session exception: {0}", ex); throw ex; } }
public async Task Migrate(int dc) { if (gateway == null) { logger.error("gateway not found, migration impossible"); return; } if (gateway.Config == null) { logger.error("config in gateway not found, migration impossible"); return; } ConfigConstructor config = (ConfigConstructor)gateway.Config; if (config.this_dc == dc) { logger.warning("migration to same dc: {0}", dc); return; } TelegramDC newDc = new TelegramDC(); foreach (var dcOption in config.dc_options) { DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption; if (optionConstructor.id == dc) { TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port); newDc.Endpoints.Add(endpoint); } } dcs[dc] = newDc; mainDcId = dc; gateway.Dispose(); gateway = null; establishedTask = new TaskCompletionSource <object>(); ConnectAsync(); await Established; }