Ejemplo n.º 1
0
        public static HttpTransfer FromNative(this NSUrlSessionTask task)
        {
            var       upload    = task is NSUrlSessionUploadTask;
            Exception exception = null;

            if (task.Error != null && task.State != NSUrlSessionTaskState.Canceling)
            {
                exception = new Exception(task.Error.LocalizedDescription);
            }

            var taskId = TaskIdentifier.FromString(task.TaskDescription);

            return(new HttpTransfer(
                       taskId.Value,
                       task.OriginalRequest.Url.ToString(),
                       taskId.File.FullName,
                       upload,
                       task.OriginalRequest.AllowsCellularAccess,
                       exception,
                       upload
                    ? task.BytesExpectedToSend
                    : task.BytesExpectedToReceive,
                       upload
                    ? task.BytesSent
                    : task.BytesReceived,

                       task.GetTransferStatus()
                       ));
        }
Ejemplo n.º 2
0
        public static HttpTransfer FromNative(this NSUrlSessionTask task)
        {
            var       status    = HttpTransferState.Unknown;
            var       upload    = task is NSUrlSessionUploadTask;
            Exception?exception = null;

            switch (task.State)
            {
            case NSUrlSessionTaskState.Canceling:
                status = HttpTransferState.Canceled;
                break;

            case NSUrlSessionTaskState.Completed:
                status = task.Error?.Code == -999
                        ? HttpTransferState.Canceled
                        : HttpTransferState.Completed;
                break;

            case NSUrlSessionTaskState.Suspended:
                status = HttpTransferState.Paused;
                break;

            case NSUrlSessionTaskState.Running:
                var bytes = task.BytesSent + task.BytesReceived;
                status = bytes == 0 ? HttpTransferState.Pending : HttpTransferState.InProgress;
                break;

            default:
                status = HttpTransferState.Error;
                if (task.Error == null)
                {
                    exception = new Exception("Unknown Error");
                }
                else
                {
                    status = HttpTransferState.Error;
                    var e   = task.Error;
                    var msg = $"{e.LocalizedDescription} - {e.LocalizedFailureReason}";
                    exception = new Exception(msg);
                }

                break;
            }
            var taskId = TaskIdentifier.FromString(task.TaskDescription);

            return(new HttpTransfer(
                       taskId.Value,
                       task.OriginalRequest.Url.ToString(),
                       taskId.File.FullName,
                       upload,
                       task.OriginalRequest.AllowsCellularAccess,
                       exception,
                       upload
                    ? task.BytesExpectedToSend
                    : task.BytesExpectedToReceive,
                       upload
                    ? task.BytesSent
                    : task.BytesReceived,

                       status
                       ));
        }