TlsStream.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls
  6. {
  7. internal class TlsStream
  8. : Stream
  9. {
  10. private readonly TlsProtocol m_handler;
  11. public TlsProtocol Protocol { get => this.m_handler; }
  12. byte[] oneByteBuf = new byte[1];
  13. internal TlsStream(TlsProtocol handler)
  14. {
  15. m_handler = handler;
  16. }
  17. public override bool CanRead
  18. {
  19. get { return true; }
  20. }
  21. public override bool CanSeek
  22. {
  23. get { return false; }
  24. }
  25. public override bool CanWrite
  26. {
  27. get { return true; }
  28. }
  29. protected override void Dispose(bool disposing)
  30. {
  31. if (disposing)
  32. {
  33. m_handler.Close();
  34. }
  35. base.Dispose(disposing);
  36. }
  37. public override void Flush()
  38. {
  39. m_handler.Flush();
  40. }
  41. public override long Length
  42. {
  43. get { throw new NotSupportedException(); }
  44. }
  45. public override long Position
  46. {
  47. get { throw new NotSupportedException(); }
  48. set { throw new NotSupportedException(); }
  49. }
  50. public override int Read(byte[] buffer, int offset, int count)
  51. {
  52. return m_handler.ReadApplicationData(buffer, offset, count);
  53. }
  54. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  55. public override int Read(Span<byte> buffer)
  56. {
  57. return m_handler.ReadApplicationData(buffer);
  58. }
  59. #endif
  60. public override int ReadByte()
  61. {
  62. int ret = m_handler.ReadApplicationData(oneByteBuf, 0, 1);
  63. return ret <= 0 ? -1 : oneByteBuf[0];
  64. }
  65. public override long Seek(long offset, SeekOrigin origin)
  66. {
  67. throw new NotSupportedException();
  68. }
  69. public override void SetLength(long value)
  70. {
  71. throw new NotSupportedException();
  72. }
  73. public override void Write(byte[] buffer, int offset, int count)
  74. {
  75. m_handler.WriteApplicationData(buffer, offset, count);
  76. }
  77. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  78. public override void Write(ReadOnlySpan<byte> buffer)
  79. {
  80. m_handler.WriteApplicationData(buffer);
  81. }
  82. #endif
  83. public override void WriteByte(byte value)
  84. {
  85. oneByteBuf[0] = value;
  86. Write(oneByteBuf, 0, 1);
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif