TlsStream.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Crypto.Tls
  6. {
  7. internal class TlsStream
  8. : Stream
  9. {
  10. private readonly TlsProtocol handler;
  11. internal TlsStream(TlsProtocol handler)
  12. {
  13. this.handler = handler;
  14. }
  15. public override bool CanRead
  16. {
  17. get { return !handler.IsClosed; }
  18. }
  19. public override bool CanSeek
  20. {
  21. get { return false; }
  22. }
  23. public override bool CanWrite
  24. {
  25. get { return !handler.IsClosed; }
  26. }
  27. #if PORTABLE || NETFX_CORE
  28. protected override void Dispose(bool disposing)
  29. {
  30. if (disposing)
  31. {
  32. handler.Close();
  33. }
  34. base.Dispose(disposing);
  35. }
  36. #else
  37. public override void Close()
  38. {
  39. handler.Close();
  40. base.Close();
  41. }
  42. #endif
  43. public override void Flush()
  44. {
  45. handler.Flush();
  46. }
  47. public override long Length
  48. {
  49. get { throw new NotSupportedException(); }
  50. }
  51. public override long Position
  52. {
  53. get { throw new NotSupportedException(); }
  54. set { throw new NotSupportedException(); }
  55. }
  56. public override int Read(byte[] buf, int off, int len)
  57. {
  58. return this.handler.ReadApplicationData(buf, off, len);
  59. }
  60. public override int ReadByte()
  61. {
  62. byte[] buf = new byte[1];
  63. if (this.Read(buf, 0, 1) <= 0)
  64. return -1;
  65. return 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. this.handler.WriteData(buf, off, len);
  78. }
  79. public override void WriteByte(byte b)
  80. {
  81. this.handler.WriteData(new byte[] { b }, 0, 1);
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif