Another restart fix and invoke tweaks

This commit is contained in:
John Gross
2017-11-23 10:46:28 -08:00
parent 25e6f27854
commit fe5dfa0ea7
5 changed files with 23 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Torch.API.Managers;
@@ -65,18 +66,18 @@ namespace Torch.API
/// <summary>
/// Invoke an action on the game thread.
/// </summary>
void Invoke(Action action);
void Invoke(Action action, [CallerMemberName] string caller = "");
/// <summary>
/// Invoke an action on the game thread and block until it has completed.
/// If this is called on the game thread the action will execute immediately.
/// </summary>
void InvokeBlocking(Action action);
void InvokeBlocking(Action action, [CallerMemberName] string caller = "");
/// <summary>
/// Invoke an action on the game thread asynchronously.
/// </summary>
Task InvokeAsync(Action action);
Task InvokeAsync(Action action, [CallerMemberName] string caller = "");
/// <summary>
/// Start the Torch instance.

View File

@@ -183,14 +183,14 @@ quit";
LogException(ex);
Console.WriteLine("Exiting in 5 seconds.");
Thread.Sleep(5000);
LogManager.Flush();
if (_config.RestartOnCrash)
{
var exe = typeof(Program).Assembly.Location;
_config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
Process.Start(exe, _config.ToString());
}
//1627 = Function failed during execution.
Environment.Exit(1627);
Process.GetCurrentProcess().Kill();
}
}
}

View File

@@ -14,6 +14,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xml.Serialization.GeneratedAssembly;
using NLog;
using Sandbox.Engine.Analytics;
using Sandbox.Game.Multiplayer;
using Sandbox.ModAPI;
@@ -297,9 +298,11 @@ namespace Torch.Server
MySandboxGame.Static.Exit();
Log.Info("Server stopped.");
LogManager.Flush();
_stopHandle.Set();
State = ServerState.Stopped;
IsRunning = false;
Process.GetCurrentProcess().Kill();
}
/// <summary>
@@ -309,9 +312,12 @@ namespace Torch.Server
{
var exe = Assembly.GetExecutingAssembly().Location;
((TorchConfig)Config).WaitForPID = Process.GetCurrentProcess().Id.ToString();
Config.Autostart = true;
Process.Start(exe, Config.ToString());
Save(0).Wait();
Stop();
Environment.Exit(0);
LogManager.Flush();
Process.GetCurrentProcess().Kill();
}
/// <inheritdoc/>

View File

@@ -151,7 +151,6 @@ namespace Torch.Commands
{
Context.Torch.Invoke(() =>
{
Context.Torch.Save(0).Wait();
Context.Torch.Restart();
});
yield break;

View File

@@ -9,6 +9,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using ProtoBuf.Meta;
using Sandbox;
using Sandbox.Engine.Multiplayer;
using Sandbox.Game;
@@ -191,16 +192,18 @@ namespace Torch
/// Invokes an action on the game thread.
/// </summary>
/// <param name="action"></param>
public void Invoke(Action action)
[MethodImpl(MethodImplOptions.NoInlining)]
public void Invoke(Action action, [CallerMemberName] string caller = "")
{
MySandboxGame.Static.Invoke(action, "Torch");
MySandboxGame.Static.Invoke(action, caller);
}
/// <summary>
/// Invokes an action on the game thread asynchronously.
/// </summary>
/// <param name="action"></param>
public Task InvokeAsync(Action action)
[MethodImpl(MethodImplOptions.NoInlining)]
public Task InvokeAsync(Action action, [CallerMemberName] string caller = "")
{
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
{
@@ -209,14 +212,15 @@ namespace Torch
return Task.CompletedTask;
}
return Task.Run(() => InvokeBlocking(action));
return Task.Run(() => InvokeBlocking(action, caller));
}
/// <summary>
/// Invokes an action on the game thread and blocks until it is completed.
/// </summary>
/// <param name="action"></param>
public void InvokeBlocking(Action action)
[MethodImpl(MethodImplOptions.NoInlining)]
public void InvokeBlocking(Action action, [CallerMemberName] string caller = "")
{
if (action == null)
return;
@@ -240,7 +244,7 @@ namespace Torch
{
e.Set();
}
}, "Torch");
}, caller);
if (!e.WaitOne(60000))
throw new TimeoutException("The game action timed out.");