Extensions.NET.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Linq;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace Renci.SshNet
  5. {
  6. /// <summary>
  7. /// Collection of different extension method specific for .NET 4.0
  8. /// </summary>
  9. public static partial class Extensions
  10. {
  11. /// <summary>
  12. /// Determines whether [is null or white space] [the specified value].
  13. /// </summary>
  14. /// <param name="value">The value.</param>
  15. /// <returns>
  16. /// <c>true</c> if [is null or white space] [the specified value]; otherwise, <c>false</c>.
  17. /// </returns>
  18. internal static bool IsNullOrWhiteSpace(this string value)
  19. {
  20. if (string.IsNullOrEmpty(value)) return true;
  21. return value.All(char.IsWhiteSpace);
  22. }
  23. internal static bool CanRead(this Socket socket)
  24. {
  25. return socket.Connected && socket.Poll(-1, SelectMode.SelectRead) && socket.Available > 0;
  26. }
  27. internal static bool CanWrite(this Socket socket)
  28. {
  29. return socket.Connected && socket.Poll(-1, SelectMode.SelectWrite);
  30. }
  31. internal static IPAddress GetIPAddress(this string host)
  32. {
  33. IPAddress ipAddress;
  34. if (!IPAddress.TryParse(host, out ipAddress))
  35. ipAddress = Dns.GetHostAddresses(host).First();
  36. return ipAddress;
  37. }
  38. }
  39. }