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 3.6 kB

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