HostManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using BestHTTP.PlatformSupport.FileSystem;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace BestHTTP.Core
  5. {
  6. public static class HostManager
  7. {
  8. private const int Version = 1;
  9. private static string LibraryPath = string.Empty;
  10. private static bool IsSaveAndLoadSupported = false;
  11. private static bool IsLoaded = false;
  12. private static Dictionary<string, HostDefinition> hosts = new Dictionary<string, HostDefinition>();
  13. public static HostDefinition GetHost(string hostStr)
  14. {
  15. HostDefinition host;
  16. if (!hosts.TryGetValue(hostStr, out host))
  17. hosts.Add(hostStr, host = new HostDefinition(hostStr));
  18. return host;
  19. }
  20. public static void RemoveAllIdleConnections()
  21. {
  22. HTTPManager.Logger.Information("HostManager", "RemoveAllIdleConnections");
  23. foreach (var host_kvp in hosts)
  24. foreach (var variant_kvp in host_kvp.Value.hostConnectionVariant)
  25. variant_kvp.Value.RemoveAllIdleConnections();
  26. }
  27. public static void TryToSendQueuedRequests()
  28. {
  29. foreach (var kvp in hosts)
  30. kvp.Value.TryToSendQueuedRequests();
  31. }
  32. public static void Shutdown()
  33. {
  34. HTTPManager.Logger.Information("HostManager", "Shutdown initiated!");
  35. foreach (var kvp in hosts)
  36. kvp.Value.Shutdown();
  37. }
  38. public static void Clear()
  39. {
  40. HTTPManager.Logger.Information("HostManager", "Clearing hosts!");
  41. hosts.Clear();
  42. }
  43. private static void SetupFolder()
  44. {
  45. if (string.IsNullOrEmpty(LibraryPath))
  46. {
  47. try
  48. {
  49. LibraryPath = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "Hosts");
  50. HTTPManager.IOService.FileExists(LibraryPath);
  51. IsSaveAndLoadSupported = true;
  52. }
  53. catch
  54. {
  55. IsSaveAndLoadSupported = false;
  56. HTTPManager.Logger.Warning("HostManager", "Save and load Disabled!");
  57. }
  58. }
  59. }
  60. public static void Save()
  61. {
  62. if (!IsSaveAndLoadSupported || string.IsNullOrEmpty(LibraryPath))
  63. return;
  64. try
  65. {
  66. using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.Create))
  67. using (var bw = new System.IO.BinaryWriter(fs))
  68. {
  69. bw.Write(Version);
  70. bw.Write(hosts.Count);
  71. foreach (var kvp in hosts)
  72. {
  73. bw.Write(kvp.Key.ToString());
  74. kvp.Value.SaveTo(bw);
  75. }
  76. }
  77. HTTPManager.Logger.Information("HostManager", hosts.Count + " hosts saved!");
  78. }
  79. catch
  80. { }
  81. }
  82. public static void Load()
  83. {
  84. if (IsLoaded)
  85. return;
  86. IsLoaded = true;
  87. SetupFolder();
  88. if (!IsSaveAndLoadSupported || string.IsNullOrEmpty(LibraryPath) || !HTTPManager.IOService.FileExists(LibraryPath))
  89. return;
  90. try
  91. {
  92. using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.OpenRead))
  93. using (var br = new System.IO.BinaryReader(fs))
  94. {
  95. int version = br.ReadInt32();
  96. int hostCount = br.ReadInt32();
  97. for (int i = 0; i < hostCount; ++i)
  98. {
  99. GetHost(br.ReadString())
  100. .LoadFrom(version, br);
  101. }
  102. HTTPManager.Logger.Information("HostManager", hostCount.ToString() + " HostDefinitions loaded!");
  103. }
  104. }
  105. catch
  106. {
  107. try
  108. {
  109. HTTPManager.IOService.FileDelete(LibraryPath);
  110. }
  111. catch
  112. { }
  113. }
  114. }
  115. }
  116. }