Пример #1
0
            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());
                }
            }
Пример #2
0
            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());
                }
            }
Пример #3
0
            private async Task<IUploadFile> FetchUserThumbnail(IUser user, Telegram telegramService, bool small)
            {
                var thumbnailLocation = TelegramUtils.GetUserPhotoLocation(user, small);

                if (thumbnailLocation == null)
                {
                    return null;
                }

                if (thumbnailLocation.DcId == telegramService.Settings.NearestDcId)
                {
                    using (var clientDisposable = new Telegram.FullClientDisposable(telegramService))
                    {
                        return await FetchFileBytes(clientDisposable.Client, thumbnailLocation);
                    }
                }
                else
                {
                    try
                    {
                        var telegramClient = telegramService.GetClient((int) thumbnailLocation.DcId);
                        return await FetchFileBytes(telegramClient, thumbnailLocation);
                    }
                    catch (Exception ex)
                    {
                        Utils.DebugPrint("Failed to obtain client from DC manager: " + ex);
                        return null;
                    }
                }
            }