예제 #1
0
        private TLUploadFileBase ReuploadFile(TLUploadFileCdnRedirect redirect, byte[] requestToken, 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;

            var req = new TLUploadReuploadCdnFile();

            req.FileToken    = redirect.FileToken;
            req.RequestToken = requestToken;

            _mtProtoService.SendRequestAsync <bool>("upload.reuploadCdnFile", req, location.DCId, false, callback =>
            {
                if (callback)
                {
                    result = GetCdnFile(redirect, location, offset, limit, out outError, out outIsCanceled);
                    while (result == null)
                    {
                        result = GetCdnFile(redirect, location, offset, limit, out outError, out outIsCanceled);
                        if (outIsCanceled)
                        {
                            break;
                        }
                    }

                    manualResetEvent.Set();
                }
            },
                                                    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.ToString().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);
        }
예제 #2
0
        public void ReuploadCdnFileAsync(byte[] fileToken, byte[] requestToken, Action <TLVector <TLCdnFileHash> > callback, Action <TLRPCError> faultCallback = null)
        {
            var obj = new TLUploadReuploadCdnFile {
                FileToken = fileToken, RequestToken = requestToken
            };

            const string caption = "upload.reuploadCdnFile";

            SendInformativeMessage(caption, obj, callback, faultCallback);
        }
예제 #3
0
        public void ReuploadCdnFileAsync(int dcId, byte[] fileToken, byte[] requestToken, Action <TLVector <TLCdnFileHash> > callback, Action <TLRPCError> faultCallback = null)
        {
            var obj = new TLUploadReuploadCdnFile {
                FileToken = fileToken, RequestToken = requestToken
            };

            const string caption = "upload.reuploadCdnFile";

            SendInformativeMessage(caption, obj, callback, faultCallback, null, dcId, ConnectionType.Generic, RequestFlag.FailOnServerError, true);
        }
예제 #4
0
        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);
                            }
                        });
                    }