PeekableContentProviderStream.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Best.HTTP.Shared.PlatformSupport.Network.Tcp;
  2. namespace Best.HTTP.Shared.Streams
  3. {
  4. /// <summary>
  5. /// A PeekableStream implementation that also implements the <see cref="IPeekableContentProvider"/> interface too.
  6. /// </summary>
  7. public abstract class PeekableContentProviderStream : PeekableStream, IPeekableContentProvider
  8. {
  9. public PeekableContentProviderStream Peekable => this;
  10. public IContentConsumer Consumer { get; private set; }
  11. public void SetTwoWayBinding(IContentConsumer consumer)
  12. {
  13. this.Consumer = consumer;
  14. this.Consumer?.SetBinding(this);
  15. }
  16. /// <summary>
  17. /// This will set Consumer to null.
  18. /// </summary>
  19. public void Unbind()
  20. {
  21. this.Consumer?.UnsetBinding();
  22. this.Consumer = null;
  23. }
  24. /// <summary>
  25. /// Set Consumer to null if the current one is the one passed in the parameter.
  26. /// </summary>
  27. public void UnbindIf(IContentConsumer consumer)
  28. {
  29. if (consumer == null || consumer == this.Consumer)
  30. {
  31. this.Consumer?.UnsetBinding();
  32. this.Consumer = null;
  33. }
  34. }
  35. }
  36. }