コード例 #1
0
 public void FindAllLiveSdkRequests(ICollection <BackgroundTransferRequest> matchingRequests)
 {
     foreach (BackgroundTransferRequest request in BackgroundTransferService.Requests)
     {
         // We only care about Transfers that we added.
         // Transfers we added are marked with our tag.
         if (BackgroundTransferHelper.BelongsToLiveSdk(request))
         {
             matchingRequests.Add(request);
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Called when a BackgroundTransferRequest's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and convert
        /// the result over to a LiveOperationResult and set it on the TaskCompletionSource.
        /// </summary>
        /// <param name="request"></param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsUploadRequest(request));

            // Remove the transfer request in order to make room in the queue for more transfers.
            // Transfers are not automatically removed by the system.
            // Cancelled requests have already been removed from the system and cannot be removed twice.
            if (!BackgroundTransferHelper.IsCanceledRequest(request))
            {
                try
                {
                    this.backgroundTransferService.Remove(request);
                }
                catch (Exception exception)
                {
                    this.tcs.TrySetException(new LiveConnectException(
                                                 ApiOperation.ApiClientErrorCode,
                                                 ResourceHelper.GetString("BackgroundTransferServiceRemoveError"),
                                                 exception));
                    return;
                }
            }

            this.OnBackgroundTransferRequestCompleted(request);

            if (request.TransferError != null)
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"),
                                                         request.TransferError);
                this.tcs.TrySetException(exception);
                return;
            }

            if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
                return;
            }

            // Once we know we have a *good* upload, we have to send it to the response handler
            // to read it's JSON response body. We are an observer to this class, so it will call us back
            // with its result.
            var responseHandler = new BackgroundUploadResponseHandler(
                request.DownloadLocation,
                this);

            responseHandler.ReadJsonResponseFromDownloadLocation();
        }
コード例 #3
0
        /// <summary>
        /// Called when a BackgroundTransferRequet's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and call
        /// the LiveOperationEventArgs event handler.
        /// </summary>
        /// <param name="request">request with a TransferStatus that is set to Completed</param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));

            this.OnBackgroundTransferRequestCompleted(request);

            // Remove the transfer request in order to make room in the queue for more transfers.
            // Transfers are not automatically removed by the system.
            // Cancelled requests have already been removed from the system and cannot be removed twice.
            if (!BackgroundTransferHelper.IsCanceledRequest(request))
            {
                try
                {
                    this.backgroundTransferService.Remove(request);
                }
                catch (Exception exception)
                {
                    this.tcs.TrySetException(new LiveConnectException(
                                                 ApiOperation.ApiClientErrorCode,
                                                 ResourceHelper.GetString("BackgroundTransferServiceRemoveError"),
                                                 exception));
                    return;
                }
            }

            if (request.TransferError != null)
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"),
                                                         request.TransferError);

                this.tcs.TrySetException(exception);
            }
            else if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
            }
            else
            {
                string jsonResponse = string.Format(JsonResponse, request.DownloadLocation.OriginalString.Replace("\\", "\\\\"));
                var    jsonReader   = new JsonReader(jsonResponse);
                var    jsonObject   = jsonReader.ReadValue() as IDictionary <string, object>;
                var    result       = new LiveOperationResult(jsonObject, jsonResponse);

                this.tcs.TrySetResult(result);
            }
        }
コード例 #4
0
        /// <summary>
        /// Attaches to this BackgroundTransferRequest's TransferStatusChanged event.
        /// </summary>
        /// <param name="request">request to attach to</param>
        public Task <LiveOperationResult> ConvertTransferStatusChangedToTask(BackgroundTransferRequest request)
        {
            Debug.Assert(BackgroundTransferHelper.IsUploadRequest(request));
            if (request.TransferStatus != TransferStatus.Completed)
            {
                request.TransferStatusChanged += this.HandleTransferStatusChanged;
            }
            else
            {
                // If we are working with an already completed request just handle it now.
                this.OnTransferStatusComplete(request);
            }

            return(this.tcs.Task);
        }
コード例 #5
0
 /// <summary>
 /// Attaches to the request's TransferProgressChanged event.
 /// It then converts these events to LiveOperationProgresses and gives them to the IProgress
 /// interface that the object was constructed with.
 /// NOTE: Call DetachFromCompletedRequest from this instance when the given request is Completed.
 /// </summary>
 /// <param name="request">request to attach to</param>
 public void ConvertTransferProgressChanged(BackgroundTransferRequest request)
 {
     Debug.Assert(BackgroundTransferHelper.IsUploadRequest(request));
     request.TransferProgressChanged += this.HandleTransferProgressChanged;
 }