Browse Source

Some work on background service that runs the actual zerotier-one process.

Adam Ierymenko 11 years ago
parent
commit
a154d660d9

+ 58 - 5
windows/ZeroTierOneService/Service.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Data;
 using System.Diagnostics;
 using System.ServiceProcess;
+using System.Threading;
 
 namespace ZeroTierOneService
 {
@@ -18,24 +19,68 @@ namespace ZeroTierOneService
             this.ztBinary = this.ztHome + Path.DirectorySeparatorChar + (Environment.Is64BitOperatingSystem ? "zerotier-one_x64.exe" : "zerotier-one_x86.exe");
 
             this.ztService = null;
+            this.ztKiller = null;
         }
 
         protected override void OnStart(string[] args)
         {
-            startZeroTierService();
+            startZeroTierDaemon();
         }
 
         protected override void OnStop()
         {
-            stopZeroTierService();
+            stopZeroTierDaemon();
         }
 
-        private void startZeroTierService()
+        private void startZeroTierDaemon()
         {
+            if (ztService != null)
+                return;
+            ztService = new Process();
+            try
+            {
+                ztService.StartInfo.UseShellExecute = false;
+                ztService.StartInfo.FileName = ztBinary;
+                ztService.StartInfo.Arguments = "";
+                ztService.StartInfo.CreateNoWindow = true;
+                ztService.Exited += ztService_Exited;
+                ztService.Start();
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine(e.ToString());
+                ztService = null;
+            }
         }
 
-        private void stopZeroTierService()
+        private void stopZeroTierDaemon()
         {
+            while (ztKiller != null)
+                Thread.Sleep(250);
+
+            ztKiller = new Process();
+            try
+            {
+                ztKiller.StartInfo.UseShellExecute = false;
+                ztKiller.StartInfo.FileName = ztBinary;
+                ztKiller.StartInfo.Arguments = "-q terminate ServiceShutdown";
+                ztKiller.StartInfo.CreateNoWindow = true;
+                ztKiller.Exited += ztKiller_Exited;
+                ztKiller.Start();
+            }
+            catch (Exception e)
+            {
+                ztKiller = null;
+            }
+
+            int waited = 0;
+            while (ztKiller != null)
+            {
+                Thread.Sleep(250);
+                if (++waited > 100)
+                    break;
+            }
+
             if (ztService != null)
             {
                 ztService.Kill();
@@ -43,15 +88,23 @@ namespace ZeroTierOneService
             }
         }
 
+        // Event generated when ztService exits
         private void ztService_Exited(object sender, System.EventArgs e)
         {
             ztService = null;
         }
 
+        // Event generated when ztKiller is done
+        private void ztKiller_Exited(object sender, System.EventArgs e)
+        {
+            ztKiller = null;
+        }
+
         private string ztHome;
         private string ztUpdatesFolder;
         private string ztBinary;
 
-        private Process ztService;
+        private volatile Process ztService;
+        private volatile Process ztKiller;
     }
 }

+ 41 - 0
windows/ZeroTierOneService/ZeroTierOneService.csproj

@@ -11,6 +11,21 @@
     <AssemblyName>ZeroTierOneService</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <PublishUrl>publish\</PublishUrl>
+    <Install>true</Install>
+    <InstallFrom>Disk</InstallFrom>
+    <UpdateEnabled>false</UpdateEnabled>
+    <UpdateMode>Foreground</UpdateMode>
+    <UpdateInterval>7</UpdateInterval>
+    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
+    <UpdatePeriodically>false</UpdatePeriodically>
+    <UpdateRequired>false</UpdateRequired>
+    <MapFileExtensions>true</MapFileExtensions>
+    <ApplicationRevision>0</ApplicationRevision>
+    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
+    <IsWebBootstrapper>false</IsWebBootstrapper>
+    <UseApplicationTrust>false</UseApplicationTrust>
+    <BootstrapperEnabled>true</BootstrapperEnabled>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -35,6 +50,15 @@
   <PropertyGroup>
     <StartupObject>ZeroTierOneService.Program</StartupObject>
   </PropertyGroup>
+  <PropertyGroup>
+    <SignAssembly>false</SignAssembly>
+  </PropertyGroup>
+  <PropertyGroup>
+    <SignManifests>false</SignManifests>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ManifestCertificateThumbprint>5809BB3255B4F32DD93619C1CF26A7DF6C282A89</ManifestCertificateThumbprint>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
@@ -64,6 +88,23 @@
       <DependentUpon>Service.cs</DependentUpon>
     </EmbeddedResource>
   </ItemGroup>
+  <ItemGroup>
+    <BootstrapperPackage Include=".NETFramework,Version=v4.5">
+      <Visible>False</Visible>
+      <ProductName>Microsoft .NET Framework 4.5 %28x86 and x64%29</ProductName>
+      <Install>true</Install>
+    </BootstrapperPackage>
+    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
+      <Visible>False</Visible>
+      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
+      <Install>false</Install>
+    </BootstrapperPackage>
+    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
+      <Visible>False</Visible>
+      <ProductName>.NET Framework 3.5 SP1</ProductName>
+      <Install>false</Install>
+    </BootstrapperPackage>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.