From 364c48625288151c614843921fef2052a77007a6 Mon Sep 17 00:00:00 2001 From: James Botting Date: Mon, 14 Sep 2015 23:23:09 +0100 Subject: [PATCH] Lets see if reset events help fix this hang issue --- ARKUpdater/Classes/SteamKit.cs | 11 ++++++++--- ARKUpdater/Interfaces/ServerInterface.cs | 17 ++++++++++------- ARKUpdater/Interfaces/SteamInterface.cs | 11 +++++++---- ARKUpdater/Program.cs | 16 ++++++++++------ 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/ARKUpdater/Classes/SteamKit.cs b/ARKUpdater/Classes/SteamKit.cs index e29ba23..040ba53 100644 --- a/ARKUpdater/Classes/SteamKit.cs +++ b/ARKUpdater/Classes/SteamKit.cs @@ -43,6 +43,7 @@ class SteamKit : IDisposable private ARKUpdater _Parent; private SteamClient _Client; private CallbackManager _CManager; + private AutoResetEvent _ResetEvent; public bool Ready; public bool Failed; @@ -71,9 +72,9 @@ protected virtual void Dispose(bool disposing) #endregion Disposal #region Create Thread - public static ThreadPair SpawnThread(ARKUpdater p) + public static ThreadPair SpawnThread(ARKUpdater p, AutoResetEvent r) { - var thisThread = new SteamKit(p); + var thisThread = new SteamKit(p, r); Thread tThread = new Thread( () => thisThread.RunThread() ) { IsBackground = true @@ -85,9 +86,11 @@ public static ThreadPair SpawnThread(ARKUpdater p) #endregion Create Thread #region Thread Setup - public SteamKit(ARKUpdater p) + public SteamKit(ARKUpdater p, AutoResetEvent r) { this._Parent = p; + this._ResetEvent = r; + this.Ready = false; this.Failed = false; this._ThreadRunning = true; @@ -133,6 +136,7 @@ private void ConnectedCallback(SteamClient.ConnectedCallback connected) private void DisconnectedCallback(SteamClient.DisconnectedCallback disconnected) { _Parent.Log.ConsolePrint(LogLevel.Debug, "Disconnected from Steam3"); + _ResetEvent.Set(); _ThreadRunning = false; } @@ -147,6 +151,7 @@ private void LogOnCallback(SteamUser.LoggedOnCallback loggedOn) } _Parent.Log.ConsolePrint(LogLevel.Debug, "Logged in anonymously to Steam3"); + _ResetEvent.Set(); Ready = true; } #endregion Steam3 Callbacks diff --git a/ARKUpdater/Interfaces/ServerInterface.cs b/ARKUpdater/Interfaces/ServerInterface.cs index 41313e0..6edcb3e 100644 --- a/ARKUpdater/Interfaces/ServerInterface.cs +++ b/ARKUpdater/Interfaces/ServerInterface.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using System.Threading; using ARKUpdater.Classes; using System.Diagnostics; using System.Collections; @@ -19,9 +20,9 @@ public ServerInterface(ARKUpdater Parent) this._Parent = Parent; } - public abstract bool StopServer(SettingsLoader.ServerChild ServerData); public abstract int StartServer(SettingsLoader.ServerChild ServerData); public abstract bool ServerRunning(SettingsLoader.ServerChild ServerData); + public abstract bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent); } class ServerInterfaceWindows : ServerInterface @@ -35,7 +36,7 @@ static class NativeMethods #endregion W32API Imports public ServerInterfaceWindows(ARKUpdater parent) : base(parent) {} - public override bool StopServer(SettingsLoader.ServerChild ServerData) + public override bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent) { Process thisProcess = null; foreach( var tProcess in _ProcessDict ) @@ -45,8 +46,11 @@ public override bool StopServer(SettingsLoader.ServerChild ServerData) } if( thisProcess == null ) return false; - File.Delete(string.Format("{0}\\server.pid", ServerData.GameServerPath)); + thisProcess.Exited += new EventHandler( (object sender, EventArgs e) => { + ResetEvent.Set(); + }); + File.Delete(string.Format("{0}\\server.pid", ServerData.GameServerPath)); thisProcess.CloseMainWindow(); return true; } @@ -117,8 +121,8 @@ public override int StartServer(SettingsLoader.ServerChild ServerData) Proc.Exited += new EventHandler(_ProcessExited); // Set Window Title - System.Threading.Thread.Sleep(2000); - NativeMethods.SetWindowText(Proc.MainWindowHandle, string.Format("ARK: {0} (Managed by ARKUpdater)", ServerData.GameServerName)); + //System.Threading.Thread.Sleep(2000); + //NativeMethods.SetWindowText(Proc.MainWindowHandle, string.Format("ARK: {0} (Managed by ARKUpdater)", ServerData.GameServerName)); // Return with Process ID _Parent.Log.ConsolePrint(LogLevel.Debug, "Spawned new Server Process with ID {0}", Proc.Id); @@ -167,7 +171,6 @@ private void _ProcessExited(object sender, EventArgs e) } // We are expecting the server to send us an exit signal, which means we have an update pending for the server (holding the main loop) - // Set processid to 0 to signal that the server has exited and we are free to start the update process _Parent.Log.ConsolePrint(LogLevel.Debug, "Server '{0}' completed shutdown, marking as ready for update", Server.ServerData.GameServerName); Server.ProcessID = 0; break; @@ -210,7 +213,7 @@ public override int StartServer(SettingsLoader.ServerChild ServerData) throw new NotImplementedException(); } - public override bool StopServer(SettingsLoader.ServerChild ServerData) + public override bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent) { throw new NotImplementedException(); } diff --git a/ARKUpdater/Interfaces/SteamInterface.cs b/ARKUpdater/Interfaces/SteamInterface.cs index 4ee62b9..6272313 100644 --- a/ARKUpdater/Interfaces/SteamInterface.cs +++ b/ARKUpdater/Interfaces/SteamInterface.cs @@ -76,12 +76,15 @@ public override bool VerifySteamPath(string ExecutablePath) public override int GetGameInformation(uint appid) { - using( var Steam3 = SteamKit.SpawnThread(_Parent) ) + var WaitHandle = new AutoResetEvent(false); + using( var Steam3 = SteamKit.SpawnThread(_Parent, WaitHandle) ) { - while( !Steam3.tClass.Ready && !Steam3.tClass.Failed ) Thread.Sleep(100); - var WaitHandle = new AutoResetEvent(false); + // Wait for Steam3 to be ready + WaitHandle.WaitOne(); + WaitHandle.Reset(); - if( Steam3.tClass.Ready ) + // Prepare request to Steam3 + if( Steam3.tClass.Ready && !Steam3.tClass.Failed ) { var returndata = -1; Steam3.tClass.RequestAppInfo(appid, (x) => { diff --git a/ARKUpdater/Program.cs b/ARKUpdater/Program.cs index 9ecfd4e..06e0141 100644 --- a/ARKUpdater/Program.cs +++ b/ARKUpdater/Program.cs @@ -246,11 +246,14 @@ public void Run() Log.ConsolePrint(LogLevel.Debug, "Checking with Steam for updates to ARK (Current Build: {0})", BuildNumber); BuildNumber = SteamInt.GetGameInformation(376030); - LastUpdatePollTime = Helpers.CurrentUnixStamp; - if( BuildNumber > PreviousBuild ) Log.ConsolePrint(LogLevel.Info, "A new build of `ARK: Survival Evolved` is available. Build number: {0}", BuildNumber); + if( BuildNumber != -1 ) + { + LastUpdatePollTime = Helpers.CurrentUnixStamp; + if( BuildNumber > PreviousBuild ) Log.ConsolePrint(LogLevel.Info, "A new build of `ARK: Survival Evolved` is available. Build number: {0}", BuildNumber); + } } - bool MinutePassed = (LastMinutePollTime+60 <= Helpers.CurrentUnixStamp) ? true : false; + bool MinutePassed = ( LastMinutePollTime + 60 <= Helpers.CurrentUnixStamp ) ? true : false; foreach( var Server in Servers ) { if( Server.MinutesRemaining == -1 ) @@ -337,11 +340,12 @@ public void Run() _Sleeper.WaitOne( TimeSpan.FromSeconds(2) ); // Shutdown Server + var ResetEvent = new AutoResetEvent(false); + ServerInt.StopServer(Server.ServerData, ResetEvent); Log.ConsolePrint(LogLevel.Info, "Server '{0}' will now be shutdown for an update", Server.ServerData.GameServerName); - ServerInt.StopServer(Server.ServerData); - + + ResetEvent.WaitOne(); Log.ConsolePrint(LogLevel.Debug, "Server '{0}' now waiting for process exit", Server.ServerData.GameServerName); - while( Server.ProcessID != 0 ) Thread.Sleep(100); // This is set to 0 by our Exit event on the process in ServerInterface.cs // Update Server SteamInt.UpdateGame(Server.ServerData.SteamUpdateScript, ARKConfiguration.ShowSteamUpdateInConsole);