Extensions.SilverlightShared.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Security.Cryptography;
  8. using System.Diagnostics;
  9. using System.Net;
  10. namespace Renci.SshNet.Common
  11. {
  12. /// <summary>
  13. /// Collection of different extension method specific for Silverlight
  14. /// </summary>
  15. public static partial class Extensions
  16. {
  17. /// <summary>
  18. /// Determines whether [is null or white space] [the specified value].
  19. /// </summary>
  20. /// <param name="value">The value.</param>
  21. /// <returns>
  22. /// <c>true</c> if [is null or white space] [the specified value]; otherwise, <c>false</c>.
  23. /// </returns>
  24. internal static bool IsNullOrWhiteSpace(this string value)
  25. {
  26. if (string.IsNullOrEmpty(value)) return true;
  27. return value.All(char.IsWhiteSpace);
  28. }
  29. /// <summary>
  30. /// Disposes the specified socket.
  31. /// </summary>
  32. /// <param name="socket">The socket.</param>
  33. [DebuggerNonUserCode]
  34. internal static void Dispose(this Socket socket)
  35. {
  36. if (socket == null)
  37. throw new NullReferenceException();
  38. socket.Close();
  39. }
  40. /// <summary>
  41. /// Disposes the specified handle.
  42. /// </summary>
  43. /// <param name="handle">The handle.</param>
  44. [DebuggerNonUserCode]
  45. internal static void Dispose(this WaitHandle handle)
  46. {
  47. if (handle == null)
  48. throw new NullReferenceException();
  49. handle.Close();
  50. }
  51. /// <summary>
  52. /// Disposes the specified algorithm.
  53. /// </summary>
  54. /// <param name="algorithm">The algorithm.</param>
  55. [DebuggerNonUserCode]
  56. internal static void Dispose(this HashAlgorithm algorithm)
  57. {
  58. if (algorithm == null)
  59. throw new NullReferenceException();
  60. algorithm.Clear();
  61. }
  62. internal static bool CanRead(this Socket socket)
  63. {
  64. return socket.Connected;
  65. }
  66. internal static bool CanWrite(this Socket socket)
  67. {
  68. return socket.Connected;
  69. }
  70. internal static IPAddress GetIPAddress(this string host)
  71. {
  72. IPAddress ipAddress;
  73. if (!IPAddress.TryParse(host, out ipAddress))
  74. {
  75. throw new ProxyException("Silverlight supports only IP addresses.");
  76. }
  77. return ipAddress;
  78. }
  79. }
  80. }