public DownloadManager(Document document, Telegram telegram) { _document = document; _fileSize = document.Size; _telegram = telegram; int dcId = (int)_document.DcId; //initialize the dc we want to download from if (_document.DcId != _telegram.Settings.NearestDcId) { Retry: // if its not the nearest dc, it could be unintialized. so lets check and init it. var dc = DcDatabase.Get(dcId); if (dc == null) { _telegram.GetClient(dcId); //after this our dc configuration is ready, the connection should die out in a 60 secs. } var dcCached = DcDatabase.Get(dcId); if (dcCached == null) { return; } var dcOption = _telegram.Config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dcId); var tcpConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port); var authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key, BitConverter.ToUInt64(dcCached.Salt, 0)); _client = new TelegramClient(tcpConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); TelegramUtils.RunSynchronously(_client.Connect()); try { TelegramUtils.RunSynchronously(_client.Methods.AccountGetAccountTTLAsync(new AccountGetAccountTTLArgs())); } catch (Exception x) { if (x.Message != null && x.Message.Contains("401")) { telegram.DebugPrint("Yikes! DC went down. Let's reinstate it..."); _client.Dispose(); DcDatabase.Delete(dcId); goto Retry; } } } else { var tcpConfig = new TcpClientTransportConfig(_telegram.Settings.NearestDcIp, (int)_telegram.Settings.NearestDcPort); var authInfo = new SharpMTProto.Authentication.AuthInfo(_telegram.Settings.AuthKey, _telegram.Settings.Salt); _client = new TelegramClient(tcpConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); TelegramUtils.RunSynchronously(_client.Connect()); } }
public DownloadManager(Document document, Telegram telegram) { _document = document; _fileSize = document.Size; _telegram = telegram; int dcId = (int)_document.DcId; //initialize the dc we want to download from if (_document.DcId != _telegram.Settings.NearestDcId) { // if its not the nearest dc, it could be unintialized. so lets check and init it. var dc = DcDatabase.Get(dcId); if (dc == null) { _telegram.GetClient(dcId); //after this our dc configuration is ready, the connection should die out in a 60 secs. } var dcCached = DcDatabase.Get(dcId); var dcOption = _telegram.Config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dcId); var tcpConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port); var authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key, BitConverter.ToUInt64(dcCached.Salt, 0)); _client = new TelegramClient(tcpConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); TelegramUtils.RunSynchronously(_client.Connect()); } else { var tcpConfig = new TcpClientTransportConfig(_telegram.Settings.NearestDcIp, (int)_telegram.Settings.NearestDcPort); var authInfo = new SharpMTProto.Authentication.AuthInfo(_telegram.Settings.AuthKey, _telegram.Settings.Salt); _client = new TelegramClient(tcpConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); TelegramUtils.RunSynchronously(_client.Connect()); } }
private TelegramClient SpinUpClient(int dc) { object dcLock; lock (_spinUpLocks) { if (_spinUpLocks.ContainsKey(dc)) { dcLock = _spinUpLocks[dc]; } else { dcLock = new object(); _spinUpLocks[dc] = dcLock; } } lock (dcLock) { var client = GetClientInternal(dc); if (client != null) { return(client); } if (_config == null) { DebugPrint("Config is null. Unable to resolve DC information."); return(null); } var dcOption = _config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dc); if (dcOption == null) { DebugPrint("Unable to find DC for DC ID: " + dc); return(null); } var dcCached = DcDatabase.Get(dc); var transportConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port); SharpMTProto.Authentication.AuthInfo authInfo; AuthExportedAuthorization exportedAuth = null; if (dcCached == null) { DebugPrint("Looks like we'll have to authenticate a new connection to: " + ObjectDumper.Dump(dcOption)); DebugPrint(">>>>>>>> Exporting auth..."); using (var clientDisposable = new TelegramClientDisposable(this)) { exportedAuth = (AuthExportedAuthorization)TelegramUtils.RunSynchronously(clientDisposable.Client.Methods.AuthExportAuthorizationAsync( new SharpTelegram.Schema.Layer18.AuthExportAuthorizationArgs { DcId = (uint)dc, })); } DebugPrint(">>>>>>> Got exported auth."); if (exportedAuth == null) { DebugPrint("Exported auth is null for some weird reason. DC ID: " + dc); return(null); } DebugPrint(">>>>>>> Fetching new authentication..."); authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(transportConfig)); } else { authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key, BitConverter.ToUInt64(dcCached.Salt, 0)); } DebugPrint(">>>>>>>> Starting new client..."); var newClient = new TelegramClient(transportConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); newClient.OnClosedInternally += (sender, e) => { DebugPrint("Removing connection to DC: " + dc); lock (_activeClients) { _activeClients.Remove(dc); } }; var result = TelegramUtils.RunSynchronously(newClient.Connect()); if (result != MTProtoConnectResult.Success) { DebugPrint("Failed to connect to DC: " + dc + ": " + result); return(null); } if (exportedAuth != null) { TelegramUtils.RunSynchronously(newClient.Methods.AuthImportAuthorizationAsync(new AuthImportAuthorizationArgs { Id = exportedAuth.Id, Bytes = exportedAuth.Bytes, })); } PingDelay(client, 60); lock (_activeClients) { _activeClients[dc] = newClient; } if (dcCached == null) { DcDatabase.Set(new TelegramDc { Dc = dc, Key = authInfo.AuthKey, Salt = BitConverter.GetBytes(authInfo.Salt), }); } return(newClient); } }