Service.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.ServiceProcess;
  7. using System.Threading;
  8. namespace ZeroTierOneService
  9. {
  10. public partial class Service : ServiceBase
  11. {
  12. public Service()
  13. {
  14. InitializeComponent();
  15. this.ztHome = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + "ZeroTier" + Path.DirectorySeparatorChar + "One";
  16. this.ztUpdatesFolder = this.ztHome + Path.DirectorySeparatorChar + "updates.d";
  17. this.ztBinary = this.ztHome + Path.DirectorySeparatorChar + (Environment.Is64BitOperatingSystem ? "zerotier-one_x64.exe" : "zerotier-one_x86.exe");
  18. this.ztService = null;
  19. this.ztKiller = null;
  20. }
  21. protected override void OnStart(string[] args)
  22. {
  23. startZeroTierDaemon();
  24. }
  25. protected override void OnStop()
  26. {
  27. stopZeroTierDaemon();
  28. }
  29. private void startZeroTierDaemon()
  30. {
  31. if (ztService != null)
  32. return;
  33. ztService = new Process();
  34. try
  35. {
  36. ztService.StartInfo.UseShellExecute = false;
  37. ztService.StartInfo.FileName = ztBinary;
  38. ztService.StartInfo.Arguments = "";
  39. ztService.StartInfo.CreateNoWindow = true;
  40. ztService.Exited += ztService_Exited;
  41. ztService.Start();
  42. }
  43. catch (Exception e)
  44. {
  45. Console.WriteLine(e.ToString());
  46. ztService = null;
  47. }
  48. }
  49. private void stopZeroTierDaemon()
  50. {
  51. while (ztKiller != null)
  52. Thread.Sleep(250);
  53. ztKiller = new Process();
  54. try
  55. {
  56. ztKiller.StartInfo.UseShellExecute = false;
  57. ztKiller.StartInfo.FileName = ztBinary;
  58. ztKiller.StartInfo.Arguments = "-q terminate ServiceShutdown";
  59. ztKiller.StartInfo.CreateNoWindow = true;
  60. ztKiller.Exited += ztKiller_Exited;
  61. ztKiller.Start();
  62. }
  63. catch (Exception e)
  64. {
  65. ztKiller = null;
  66. }
  67. int waited = 0;
  68. while (ztKiller != null)
  69. {
  70. Thread.Sleep(250);
  71. if (++waited > 100)
  72. break;
  73. }
  74. if (ztService != null)
  75. {
  76. ztService.Kill();
  77. ztService = null;
  78. }
  79. }
  80. // Event generated when ztService exits
  81. private void ztService_Exited(object sender, System.EventArgs e)
  82. {
  83. ztService = null;
  84. }
  85. // Event generated when ztKiller is done
  86. private void ztKiller_Exited(object sender, System.EventArgs e)
  87. {
  88. ztKiller = null;
  89. }
  90. private string ztHome;
  91. private string ztUpdatesFolder;
  92. private string ztBinary;
  93. private volatile Process ztService;
  94. private volatile Process ztKiller;
  95. }
  96. }