DtlsTransport.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. #if !PORTABLE || NETFX_CORE || DOTNET
  6. using System.Net.Sockets;
  7. #endif
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  9. {
  10. public class DtlsTransport
  11. : DatagramTransport
  12. {
  13. private readonly DtlsRecordLayer m_recordLayer;
  14. internal DtlsTransport(DtlsRecordLayer recordLayer)
  15. {
  16. this.m_recordLayer = recordLayer;
  17. }
  18. /// <exception cref="IOException"/>
  19. public virtual int GetReceiveLimit()
  20. {
  21. return m_recordLayer.GetReceiveLimit();
  22. }
  23. /// <exception cref="IOException"/>
  24. public virtual int GetSendLimit()
  25. {
  26. return m_recordLayer.GetSendLimit();
  27. }
  28. /// <exception cref="IOException"/>
  29. public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
  30. {
  31. if (null == buf)
  32. throw new ArgumentNullException("buf");
  33. if (off < 0 || off >= buf.Length)
  34. throw new ArgumentException("invalid offset: " + off, "off");
  35. if (len < 0 || len > buf.Length - off)
  36. throw new ArgumentException("invalid length: " + len, "len");
  37. if (waitMillis < 0)
  38. throw new ArgumentException("cannot be negative", "waitMillis");
  39. try
  40. {
  41. return m_recordLayer.Receive(buf, off, len, waitMillis);
  42. }
  43. catch (TlsFatalAlert fatalAlert)
  44. {
  45. m_recordLayer.Fail(fatalAlert.AlertDescription);
  46. throw fatalAlert;
  47. }
  48. catch (TlsTimeoutException e)
  49. {
  50. throw e;
  51. }
  52. #if !PORTABLE || NETFX_CORE || DOTNET
  53. catch (SocketException e)
  54. {
  55. if (TlsUtilities.IsTimeout(e))
  56. throw e;
  57. m_recordLayer.Fail(AlertDescription.internal_error);
  58. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  59. }
  60. #endif
  61. // TODO[tls-port] Can we support interrupted IO on .NET?
  62. //catch (InterruptedIOException e)
  63. //{
  64. // throw e;
  65. //}
  66. catch (IOException e)
  67. {
  68. m_recordLayer.Fail(AlertDescription.internal_error);
  69. throw e;
  70. }
  71. catch (Exception e)
  72. {
  73. m_recordLayer.Fail(AlertDescription.internal_error);
  74. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  75. }
  76. }
  77. /// <exception cref="IOException"/>
  78. public virtual void Send(byte[] buf, int off, int len)
  79. {
  80. if (null == buf)
  81. throw new ArgumentNullException("buf");
  82. if (off < 0 || off >= buf.Length)
  83. throw new ArgumentException("invalid offset: " + off, "off");
  84. if (len < 0 || len > buf.Length - off)
  85. throw new ArgumentException("invalid length: " + len, "len");
  86. try
  87. {
  88. m_recordLayer.Send(buf, off, len);
  89. }
  90. catch (TlsFatalAlert fatalAlert)
  91. {
  92. m_recordLayer.Fail(fatalAlert.AlertDescription);
  93. throw fatalAlert;
  94. }
  95. catch (TlsTimeoutException e)
  96. {
  97. throw e;
  98. }
  99. #if !PORTABLE || NETFX_CORE || DOTNET
  100. catch (SocketException e)
  101. {
  102. if (TlsUtilities.IsTimeout(e))
  103. throw e;
  104. m_recordLayer.Fail(AlertDescription.internal_error);
  105. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  106. }
  107. #endif
  108. // TODO[tls-port] Can we support interrupted IO on .NET?
  109. //catch (InterruptedIOException e)
  110. //{
  111. // throw e;
  112. //}
  113. catch (IOException e)
  114. {
  115. m_recordLayer.Fail(AlertDescription.internal_error);
  116. throw e;
  117. }
  118. catch (Exception e)
  119. {
  120. m_recordLayer.Fail(AlertDescription.internal_error);
  121. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  122. }
  123. }
  124. /// <exception cref="IOException"/>
  125. public virtual void Close()
  126. {
  127. m_recordLayer.Close();
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif