Pausable stream takes the given stream and causes the reading or writing thread to pause if pause is called until resume is called.
Inheritance: StreamWrapper
Ejemplo n.º 1
0
 public void PausableStreamDoesNotPauseWithoutCallingPause() {
     int length = 1024 * 1024 * 10;
     byte[] content = new byte[length];
     using (var inputStream = new MemoryStream(content))
         using (var underTest = new PausableStream(inputStream)) {
         var task = Task.Factory.StartNew(() => {
             using (var outputStream = new MemoryStream()) {
                 underTest.CopyTo(outputStream);
                 Assert.That(outputStream.Length, Is.EqualTo(length));
             }
         });
         task.Wait();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission) {
            if (transmission == null) {
                throw new ArgumentNullException("transmission");
            }

            this.abort = new AbortableStream(wrappedStream);
            this.pause = new PausableStream(this.abort);
            this.bandwidthNotify = new BandwidthNotifyingStream(this.pause);
            this.progress = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception)) {
                    transmission.Status = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond)) {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                    transmission.Position = p.Position;
                } else if (e.PropertyName == Utils.NameOf(() => p.Length)) {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status)) {
                    if (t.Status == TransmissionStatus.ABORTING) {
                        this.abort.Abort();
                        this.pause.Resume();
                    } else if (t.Status == TransmissionStatus.PAUSED) {
                        this.pause.Pause();
                    } else if (t.Status == TransmissionStatus.TRANSMITTING) {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED) {
                this.abort.Abort();
            }
        }
Ejemplo n.º 3
0
 public void PauseAndResumeStream([Values(1, 2, 5)]int seconds) {
     int length = 1024 * 1024 * 10;
     var start = DateTime.Now;
     byte[] content = new byte[length];
     using (var inputStream = new MemoryStream(content))
     using (var underTest = new PausableStream(inputStream)) {
         underTest.Pause();
         var task = Task.Factory.StartNew(() => {
             using (var outputStream = new MemoryStream()) {
                 underTest.CopyTo(outputStream);
                 Assert.That(outputStream.Length, Is.EqualTo(length));
                 var duration = DateTime.Now - start;
                 Assert.That(Math.Round(duration.TotalSeconds), Is.InRange(seconds, seconds + 1));
             }
         });
         System.Threading.Thread.Sleep(seconds * 1000);
         underTest.Resume();
         task.Wait();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission)
        {
            if (transmission == null)
            {
                throw new ArgumentNullException("transmission");
            }

            this.abort                  = new AbortableStream(wrappedStream);
            this.pause                  = new PausableStream(this.abort);
            this.bandwidthNotify        = new BandwidthNotifyingStream(this.pause);
            this.progress               = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception))
                {
                    transmission.Status          = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond))
                {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position))
                {
                    transmission.Position = p.Position;
                }
                else if (e.PropertyName == Utils.NameOf(() => p.Length))
                {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status))
                {
                    if (t.Status == TransmissionStatus.ABORTING)
                    {
                        this.abort.Abort();
                        this.pause.Resume();
                    }
                    else if (t.Status == TransmissionStatus.PAUSED)
                    {
                        this.pause.Pause();
                    }
                    else if (t.Status == TransmissionStatus.TRANSMITTING)
                    {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED)
            {
                this.abort.Abort();
            }
        }