Ejemplo n.º 1
0
        public void DownloadFile(TLFileLocation file, TLObject owner, TLInt fileSize)
        {
            var downloadableItem = GetDownloadableItem(file, owner, fileSize, null);

            lock (_itemsSyncRoot)
            {
                bool addFile           = true;
                var  inputFileLocation = file.ToInputFileLocation();
                foreach (var item in _items)
                {
                    if (item.InputLocation.LocationEquals(inputFileLocation) &&
                        item.Owner == owner)
                    {
                        addFile = false;
                        break;
                    }
                }

                if (addFile)
                {
                    _items.Add(downloadableItem);
                }
            }

            StartAwaitingWorkers();
        }
Ejemplo n.º 2
0
        private TLFile GetFile(TLFileLocation location, TLInt offset, TLInt limit)
        {
            var    manualResetEvent = new ManualResetEvent(false);
            TLFile result           = null;

            _mtProtoService.GetFileAsync(location.DCId, location.ToInputFileLocation(), offset, limit,
                                         file =>
            {
                result = file;
                manualResetEvent.Set();
            },
                                         error =>
            {
                int delay;
                lock (_randomRoot)
                {
                    delay = _random.Next(1000, 3000);
                }

                Execute.BeginOnThreadPool(TimeSpan.FromMilliseconds(delay), () => manualResetEvent.Set());
            });

            manualResetEvent.WaitOne();
            return(result);
        }
Ejemplo n.º 3
0
        protected DownloadableItem GetDownloadableItem(TLFileLocation location, TLObject owner, TLInt fileSize, Action <DownloadableItem> callback)
        {
            var item = new DownloadableItem
            {
                Owner         = owner,
                DCId          = location.DCId,
                Callback      = callback,
                InputLocation = location.ToInputFileLocation()
            };

            item.Parts = GetItemParts(fileSize, item);

            return(item);
        }
Ejemplo n.º 4
0
        private TLUploadFileBase GetFile(TLFileLocation location, int offset, int limit, out TLRPCError er, out bool isCanceled)
        {
            var manualResetEvent      = new ManualResetEvent(false);
            TLUploadFileBase result   = null;
            TLRPCError       outError = null;
            var outIsCanceled         = false;

            _protoService.GetFileAsync(location.DCId, location.ToInputFileLocation(), offset, limit,
                                       callback =>
            {
                result = callback;
                manualResetEvent.Set();

                if (callback is TLUploadFile file)
                {
                    _statsService.IncrementReceivedBytesCount(_protoService.NetworkType, _dataType, 4 + 4 + file.Bytes.Length + 4);
                }
            },
                                       error =>
            {
                outError = error;

                if (error.CodeEquals(TLErrorCode.INTERNAL) ||
                    (error.CodeEquals(TLErrorCode.BAD_REQUEST) && (error.TypeEquals(TLErrorType.LOCATION_INVALID) || error.TypeEquals(TLErrorType.VOLUME_LOC_NOT_FOUND))) ||
                    (error.CodeEquals(TLErrorCode.NOT_FOUND) && error.ErrorMessage != null && error.ErrorMessage.StartsWith("Incorrect dhGen")))
                {
                    outIsCanceled = true;

                    manualResetEvent.Set();
                    return;
                }

                int delay;
                lock (_randomRoot)
                {
                    delay = _random.Next(1000, 3000);
                }

                Execute.BeginOnThreadPool(TimeSpan.FromMilliseconds(delay), () => manualResetEvent.Set());
            });

            manualResetEvent.WaitOne(20 * 1000);
            er         = outError;
            isCanceled = outIsCanceled;

            return(result);
        }
Ejemplo n.º 5
0
        public void DownloadFile(TLFileLocation file, TLObject owner, TLInt fileSize, Action <DownloadableItem> callback)
        {
            var downloadableItem = GetDownloadableItem(file, owner, fileSize, callback);

            lock (_itemsSyncRoot)
            {
                bool addFile           = true;
                var  inputFileLocation = file.ToInputFileLocation();
                foreach (var item in _items)
                {
                    if (item.InputLocation.LocationEquals(inputFileLocation))
                    {
                        if (callback != null)
                        {
                            if (item.Callbacks == null)
                            {
                                item.Callbacks = new List <Action <DownloadableItem> >();
                            }
                            item.Callbacks.Add(callback);

                            addFile = false;
                            break;
                        }

                        if (item.Owner == owner)
                        {
                            addFile = false;
                            break;
                        }
                    }
                }

                if (addFile)
                {
                    _items.Add(downloadableItem);
                }
            }

            StartAwaitingWorkers();
        }