diff options
author | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2014-02-02 23:48:44 -0800 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2014-02-02 23:48:44 -0800 |
commit | a154d660d936e448bb24fb7c2691ddfd73763b45 (patch) | |
tree | 5cd76e6a12a08adb3fb7307dc0f07af5ccb22436 /windows | |
parent | 7a49d50187ff7328933f03d05569576d846dc3e0 (diff) | |
download | infinitytier-a154d660d936e448bb24fb7c2691ddfd73763b45.tar.gz infinitytier-a154d660d936e448bb24fb7c2691ddfd73763b45.zip |
Some work on background service that runs the actual zerotier-one process.
Diffstat (limited to 'windows')
-rw-r--r-- | windows/ZeroTierOneService/Service.cs | 63 | ||||
-rw-r--r-- | windows/ZeroTierOneService/ZeroTierOneService.csproj | 41 |
2 files changed, 99 insertions, 5 deletions
diff --git a/windows/ZeroTierOneService/Service.cs b/windows/ZeroTierOneService/Service.cs index 4ac11bd0..306e8e4d 100644 --- a/windows/ZeroTierOneService/Service.cs +++ b/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; } } diff --git a/windows/ZeroTierOneService/ZeroTierOneService.csproj b/windows/ZeroTierOneService/ZeroTierOneService.csproj index b7c75abc..f409bbb8 100644 --- a/windows/ZeroTierOneService/ZeroTierOneService.csproj +++ b/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. |