You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.cs 4.9 kB

10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using Shadowsocks.Controller;
  8. namespace Shadowsocks.Util
  9. {
  10. public class Utils
  11. {
  12. private static string TempPath = null;
  13. // return path to store temporary files
  14. public static string GetTempPath()
  15. {
  16. if (TempPath == null)
  17. {
  18. if (File.Exists(Path.Combine(Application.StartupPath, "shadowsocks_portable_mode.txt")))
  19. try
  20. {
  21. Directory.CreateDirectory(Path.Combine(Application.StartupPath, "temp"));
  22. }
  23. catch (Exception e)
  24. {
  25. TempPath = Path.GetTempPath();
  26. Logging.LogUsefulException(e);
  27. }
  28. finally
  29. {
  30. // don't use "/", it will fail when we call explorer /select xxx/temp\xxx.log
  31. TempPath = Path.Combine(Application.StartupPath, "temp");
  32. }
  33. else
  34. TempPath = Path.GetTempPath();
  35. }
  36. return TempPath;
  37. }
  38. // return a full path with filename combined which pointed to the temporary directory
  39. public static string GetTempPath(string filename)
  40. {
  41. return Path.Combine(GetTempPath(), filename);
  42. }
  43. public static void ReleaseMemory(bool removePages)
  44. {
  45. // release any unused pages
  46. // making the numbers look good in task manager
  47. // this is totally nonsense in programming
  48. // but good for those users who care
  49. // making them happier with their everyday life
  50. // which is part of user experience
  51. GC.Collect(GC.MaxGeneration);
  52. GC.WaitForPendingFinalizers();
  53. if (removePages)
  54. {
  55. // as some users have pointed out
  56. // removing pages from working set will cause some IO
  57. // which lowered user experience for another group of users
  58. //
  59. // so we do 2 more things here to satisfy them:
  60. // 1. only remove pages once when configuration is changed
  61. // 2. add more comments here to tell users that calling
  62. // this function will not be more frequent than
  63. // IM apps writing chat logs, or web browsers writing cache files
  64. // if they're so concerned about their disk, they should
  65. // uninstall all IM apps and web browsers
  66. //
  67. // please open an issue if you're worried about anything else in your computer
  68. // no matter it's GPU performance, monitor contrast, audio fidelity
  69. // or anything else in the task manager
  70. // we'll do as much as we can to help you
  71. //
  72. // just kidding
  73. SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
  74. (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF);
  75. }
  76. }
  77. public static string UnGzip(byte[] buf)
  78. {
  79. byte[] buffer = new byte[1024];
  80. int n;
  81. using (MemoryStream sb = new MemoryStream())
  82. {
  83. using (GZipStream input = new GZipStream(new MemoryStream(buf),
  84. CompressionMode.Decompress, false))
  85. {
  86. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  87. {
  88. sb.Write(buffer, 0, n);
  89. }
  90. }
  91. return System.Text.Encoding.UTF8.GetString(sb.ToArray());
  92. }
  93. }
  94. public static string FormatBandwide(long n)
  95. {
  96. float f = n;
  97. string unit = "B";
  98. if (f > 1024)
  99. {
  100. f = f / 1024;
  101. unit = "KiB";
  102. }
  103. if (f > 1024)
  104. {
  105. f = f / 1024;
  106. unit = "MiB";
  107. }
  108. if (f > 1024)
  109. {
  110. f = f / 1024;
  111. unit = "GiB";
  112. }
  113. if (f > 1024)
  114. {
  115. f = f / 1024;
  116. unit = "TiB";
  117. }
  118. return $"{f:.##}{unit}";
  119. }
  120. [DllImport("kernel32.dll")]
  121. [return: MarshalAs(UnmanagedType.Bool)]
  122. private static extern bool SetProcessWorkingSetSize(IntPtr process,
  123. UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize);
  124. }
  125. }