Exemple #1
0
        public void GetWebFileAsync(int dcId, TLInputWebFileLocation location, int offset, int limit, Action <TLUploadWebFile> callback, Action <TLRPCError> faultCallback = null)
        {
            var obj = new TLUploadGetWebFile {
                Location = location, Offset = offset, Limit = limit
            };

            const string caption = "upload.getWebFile";

            SendInformativeMessage(caption, obj, callback, faultCallback, null, dcId, ConnectionType.Download, RequestFlag.ForceDownload | RequestFlag.FailOnServerError, true);
        }
        private void startDownloadRequest()
        {
            if (state != stateDownloading || totalBytesCount > 0 && nextDownloadOffset >= totalBytesCount || requestInfos.Count + delayedRequestInfos.Count >= currentMaxDownloadRequests)
            {
                return;
            }
            int count = 1;

            if (totalBytesCount > 0)
            {
                count = Math.Max(0, currentMaxDownloadRequests - requestInfos.Count /* - delayedRequestInfos.size()*/);
            }

            for (int a = 0; a < count; a++)
            {
                if (totalBytesCount > 0 && nextDownloadOffset >= totalBytesCount)
                {
                    break;
                }
                bool     isLast = totalBytesCount <= 0 || a == count - 1 || totalBytesCount > 0 && nextDownloadOffset + currentDownloadChunkSize >= totalBytesCount;
                TLObject request;
                int      offset;
                int      flags;
                if (isCdn)
                {
                    TLUploadGetCdnFile req = new TLUploadGetCdnFile();
                    req.FileToken = cdnToken;
                    req.Offset    = offset = nextDownloadOffset;
                    req.Limit     = currentDownloadChunkSize;
                    request       = req;
                    //!!!flags = ConnectionsManager.ConnectionTypeGeneric;
                    //flags = requestsCount % 2 == 0 ? ConnectionsManager.ConnectionTypeDownload : ConnectionsManager.ConnectionTypeDownload2;
                }
                else if (webLocation != null)
                {
                    TLUploadGetWebFile req = new TLUploadGetWebFile();
                    req.Location = webLocation;
                    req.Offset   = offset = nextDownloadOffset;
                    req.Limit    = currentDownloadChunkSize;
                    request      = req;
                    //1!!flags = ConnectionsManager.ConnectionTypeGeneric;
                    //flags = requestsCount % 2 == 0 ? ConnectionsManager.ConnectionTypeDownload : ConnectionsManager.ConnectionTypeDownload2;
                }
                else
                {
                    TLUploadGetFile req = new TLUploadGetFile();
                    req.Location = location;
                    req.Offset   = offset = nextDownloadOffset;
                    req.Limit    = currentDownloadChunkSize;
                    request      = req;
                    //flags = requestsCount % 2 == 0 ? ConnectionsManager.ConnectionTypeDownload : ConnectionsManager.ConnectionTypeDownload2;
                }
                nextDownloadOffset += currentDownloadChunkSize;
                RequestInfo requestInfo = new RequestInfo();
                requestInfos.Add(requestInfo);
                requestInfo.offset = offset;

                int dcId;
                if (isCdn)
                {
                    dcId = cdnDatacenterId;
                }
                else
                {
                    dcId = datacenter_id;
                }

                //var reset = new ManualResetEvent(false);
                MTProtoService.Current.SendRequestAsync <TLObject>("", request, dcId, isCdn, result =>
                {
                    //reset.Set();
                    if (result is TLUploadFileCdnRedirect redirect)
                    {
                        isCdn           = true;
                        cdnDatacenterId = redirect.DCId;
                        cdnIv           = redirect.EncryptionIV;
                        cdnKey          = redirect.EncryptionKey;
                        cdnToken        = redirect.FileToken;
                        ClearOperation(requestInfo);
                        startDownloadRequest();
                    }
                    else if (result is TLUploadCdnFileReuploadNeeded reuploadNeeded && !reuploadingCdn)
                    {
                        ClearOperation(requestInfo);
                        reuploadingCdn = true;
                        TLUploadReuploadCdnFile req = new TLUploadReuploadCdnFile();
                        req.FileToken    = cdnToken;
                        req.RequestToken = reuploadNeeded.RequestToken;

                        MTProtoService.Current.SendRequestAsync <TLObject>("upload.reuploadCdnFile", req, datacenter_id, isCdn, resultReupload =>
                        {
                            reuploadingCdn = false;
                            startDownloadRequest();
                        }, faultReupload =>
                        {
                            reuploadingCdn = false;
                            if (faultReupload.ErrorMessage.Equals("FILE_TOKEN_INVALID") && faultReupload.ErrorMessage.Equals("REQUEST_TOKEN_INVALID"))
                            {
                                isCdn = false;
                                ClearOperation(requestInfo);
                                startDownloadRequest();
                            }
                            else
                            {
                                onFail(false, 0);
                            }
                        });
                    }