LockedBufferSegmenStream.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2 && !BESTHTTP_DISABLE_WEBSOCKET
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using BestHTTP.Extensions;
  6. using BestHTTP.PlatformSupport.Memory;
  7. namespace BestHTTP.WebSocket.Implementations.Utils
  8. {
  9. public sealed class LockedBufferSegmenStream : BufferSegmentStream
  10. {
  11. public bool IsClosed { get; private set; }
  12. public override int Read(byte[] buffer, int offset, int count)
  13. {
  14. lock (base.bufferList)
  15. {
  16. if (this.IsClosed && base.bufferList.Count == 0)
  17. return 0;
  18. int sumReadCount = base.Read(buffer, offset, count);
  19. return sumReadCount == 0 ? -1 : sumReadCount;
  20. }
  21. }
  22. public override void Write(BufferSegment bufferSegment)
  23. {
  24. lock (base.bufferList)
  25. {
  26. if (this.IsClosed)
  27. return;
  28. base.Write(bufferSegment);
  29. }
  30. }
  31. public override void Reset()
  32. {
  33. lock (base.bufferList)
  34. {
  35. base.Reset();
  36. }
  37. }
  38. protected override void Dispose(bool disposing)
  39. {
  40. base.Dispose(disposing);
  41. Reset();
  42. }
  43. public override void Close()
  44. {
  45. this.IsClosed = true;
  46. }
  47. }
  48. }
  49. #endif