TlsStream.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.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. internal TlsStream(TlsProtocol handler)
  13. {
  14. this.m_handler = handler;
  15. }
  16. public override bool CanRead
  17. {
  18. get { return !m_handler.IsClosed; }
  19. }
  20. public override bool CanSeek
  21. {
  22. get { return false; }
  23. }
  24. public override bool CanWrite
  25. {
  26. get { return !m_handler.IsClosed; }
  27. }
  28. #if PORTABLE || NETFX_CORE
  29. protected override void Dispose(bool disposing)
  30. {
  31. if (disposing)
  32. {
  33. m_handler.Close();
  34. }
  35. base.Dispose(disposing);
  36. }
  37. #else
  38. public override void Close()
  39. {
  40. m_handler.Close();
  41. base.Close();
  42. }
  43. #endif
  44. public override void Flush()
  45. {
  46. m_handler.Flush();
  47. }
  48. public override long Length
  49. {
  50. get { throw new NotSupportedException(); }
  51. }
  52. public override long Position
  53. {
  54. get { throw new NotSupportedException(); }
  55. set { throw new NotSupportedException(); }
  56. }
  57. public override int Read(byte[] buf, int off, int len)
  58. {
  59. return m_handler.ReadApplicationData(buf, off, len);
  60. }
  61. public override int ReadByte()
  62. {
  63. byte[] buf = new byte[1];
  64. int ret = Read(buf, 0, 1);
  65. return ret <= 0 ? -1 : buf[0];
  66. }
  67. public override long Seek(long offset, SeekOrigin origin)
  68. {
  69. throw new NotSupportedException();
  70. }
  71. public override void SetLength(long value)
  72. {
  73. throw new NotSupportedException();
  74. }
  75. public override void Write(byte[] buf, int off, int len)
  76. {
  77. m_handler.WriteApplicationData(buf, off, len);
  78. }
  79. public override void WriteByte(byte b)
  80. {
  81. Write(new byte[]{ b }, 0, 1);
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif