TlsStream.cs 2.2 KB

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