Implement kick on restart
This commit is contained in:
@@ -22,6 +22,7 @@ namespace Torch
|
|||||||
string WaitForPID { get; set; }
|
string WaitForPID { get; set; }
|
||||||
string ChatName { get; set; }
|
string ChatName { get; set; }
|
||||||
string ChatColor { get; set; }
|
string ChatColor { get; set; }
|
||||||
|
bool DisconnectOnRestart { get; set; }
|
||||||
|
|
||||||
bool Save(string path = null);
|
bool Save(string path = null);
|
||||||
}
|
}
|
||||||
|
57
Torch.Mod/Messages/JoinServerMessage.cs
Normal file
57
Torch.Mod/Messages/JoinServerMessage.cs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using ProtoBuf;
|
||||||
|
using Sandbox.ModAPI;
|
||||||
|
|
||||||
|
namespace Torch.Mod.Messages
|
||||||
|
{
|
||||||
|
[ProtoContract]
|
||||||
|
public class JoinServerMessage : MessageBase
|
||||||
|
{
|
||||||
|
[ProtoMember(201)]
|
||||||
|
public int Delay;
|
||||||
|
[ProtoMember(202)]
|
||||||
|
public string Address;
|
||||||
|
|
||||||
|
private JoinServerMessage()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinServerMessage(string address)
|
||||||
|
{
|
||||||
|
Address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinServerMessage(string address, int delay)
|
||||||
|
{
|
||||||
|
Address = address;
|
||||||
|
Delay = delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ProcessClient()
|
||||||
|
{
|
||||||
|
if (TorchModCore.Debug)
|
||||||
|
{
|
||||||
|
MyAPIGateway.Utilities.ShowMessage("Torch", $"Joining server {Address} with delay {Delay}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Delay <= 0)
|
||||||
|
{
|
||||||
|
MyAPIGateway.Multiplayer.JoinServer(Address);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MyAPIGateway.Parallel.StartBackground(() =>
|
||||||
|
{
|
||||||
|
MyAPIGateway.Parallel.Sleep(Delay);
|
||||||
|
MyAPIGateway.Multiplayer.JoinServer(Address);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ProcessServer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -11,6 +11,7 @@ namespace Torch.Mod.Messages
|
|||||||
[ProtoInclude(1, typeof(DialogMessage))]
|
[ProtoInclude(1, typeof(DialogMessage))]
|
||||||
[ProtoInclude(2, typeof(NotificationMessage))]
|
[ProtoInclude(2, typeof(NotificationMessage))]
|
||||||
[ProtoInclude(3, typeof(VoxelResetMessage))]
|
[ProtoInclude(3, typeof(VoxelResetMessage))]
|
||||||
|
[ProtoInclude(4, typeof(JoinServerMessage))]
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
[ProtoContract]
|
[ProtoContract]
|
||||||
@@ -28,7 +29,7 @@ namespace Torch.Mod.Messages
|
|||||||
internal ulong[] Ignore;
|
internal ulong[] Ignore;
|
||||||
internal byte[] CompressedData;
|
internal byte[] CompressedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum MessageTarget
|
public enum MessageTarget
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@@ -10,6 +10,7 @@ using Torch.Mod.Messages;
|
|||||||
using VRage;
|
using VRage;
|
||||||
using VRage.Collections;
|
using VRage.Collections;
|
||||||
using VRage.Game.ModAPI;
|
using VRage.Game.ModAPI;
|
||||||
|
using VRage.Network;
|
||||||
using VRage.Utils;
|
using VRage.Utils;
|
||||||
using Task = ParallelTasks.Task;
|
using Task = ParallelTasks.Task;
|
||||||
|
|
||||||
@@ -50,6 +51,10 @@ namespace Torch.Mod
|
|||||||
{
|
{
|
||||||
var m = _messagePool.Get();
|
var m = _messagePool.Get();
|
||||||
m.CompressedData = bytes;
|
m.CompressedData = bytes;
|
||||||
|
#if TORCH
|
||||||
|
m.SenderId = MyEventContext.Current.Sender.Value;
|
||||||
|
#endif
|
||||||
|
|
||||||
_processing.Add(m);
|
_processing.Add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,10 +64,19 @@ namespace Torch.Mod
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var m = _processing.Take();
|
MessageBase m;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m = _processing.Take();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}");
|
MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}");
|
||||||
|
|
||||||
if (m is IncomingMessage)
|
if (m is IncomingMessage) //process incoming messages
|
||||||
{
|
{
|
||||||
MessageBase i;
|
MessageBase i;
|
||||||
try
|
try
|
||||||
@@ -78,50 +92,55 @@ namespace Torch.Mod
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (TorchModCore.Debug)
|
||||||
|
MyAPIGateway.Utilities.ShowMessage("Torch", $"Received message of type {i.GetType().Name}");
|
||||||
|
|
||||||
if (MyAPIGateway.Multiplayer.IsServer)
|
if (MyAPIGateway.Multiplayer.IsServer)
|
||||||
i.ProcessServer();
|
i.ProcessServer();
|
||||||
else
|
else
|
||||||
i.ProcessClient();
|
i.ProcessClient();
|
||||||
}
|
}
|
||||||
else
|
else //process outgoing messages
|
||||||
{
|
{
|
||||||
|
if (TorchModCore.Debug)
|
||||||
|
MyAPIGateway.Utilities.ShowMessage("Torch", $"Sending message of type {m.GetType().Name}");
|
||||||
|
|
||||||
var b = MyAPIGateway.Utilities.SerializeToBinary(m);
|
var b = MyAPIGateway.Utilities.SerializeToBinary(m);
|
||||||
m.CompressedData = MyCompression.Compress(b);
|
m.CompressedData = MyCompression.Compress(b);
|
||||||
|
|
||||||
MyAPIGateway.Utilities.InvokeOnGameThread(() =>
|
switch (m.TargetType)
|
||||||
{
|
{
|
||||||
|
case MessageTarget.Single:
|
||||||
|
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target);
|
||||||
|
break;
|
||||||
|
case MessageTarget.Server:
|
||||||
|
MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData);
|
||||||
|
break;
|
||||||
|
case MessageTarget.AllClients:
|
||||||
|
MyAPIGateway.Players.GetPlayers(_playerCache);
|
||||||
|
foreach (var p in _playerCache)
|
||||||
|
{
|
||||||
|
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId)
|
||||||
|
continue;
|
||||||
|
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
|
||||||
|
}
|
||||||
|
|
||||||
switch (m.TargetType)
|
break;
|
||||||
{
|
case MessageTarget.AllExcept:
|
||||||
case MessageTarget.Single:
|
MyAPIGateway.Players.GetPlayers(_playerCache);
|
||||||
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target);
|
foreach (var p in _playerCache)
|
||||||
break;
|
{
|
||||||
case MessageTarget.Server:
|
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId))
|
||||||
MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData);
|
continue;
|
||||||
break;
|
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
|
||||||
case MessageTarget.AllClients:
|
}
|
||||||
MyAPIGateway.Players.GetPlayers(_playerCache);
|
|
||||||
foreach (var p in _playerCache)
|
break;
|
||||||
{
|
default:
|
||||||
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId)
|
throw new Exception();
|
||||||
continue;
|
}
|
||||||
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
|
|
||||||
}
|
_playerCache.Clear();
|
||||||
break;
|
|
||||||
case MessageTarget.AllExcept:
|
|
||||||
MyAPIGateway.Players.GetPlayers(_playerCache);
|
|
||||||
foreach (var p in _playerCache)
|
|
||||||
{
|
|
||||||
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId))
|
|
||||||
continue;
|
|
||||||
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
_playerCache.Clear();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -130,7 +149,7 @@ namespace Torch.Mod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECEIVED!");
|
MyLog.Default.WriteLineAndConsole("TORCH MOD: INFO: Communication thread shut down successfully! THIS IS NOT AN ERROR");
|
||||||
//exit signal received. Clean everything and GTFO
|
//exit signal received. Clean everything and GTFO
|
||||||
_processing?.Dispose();
|
_processing?.Dispose();
|
||||||
_processing = null;
|
_processing = null;
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Messages\IncomingMessage.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Messages\IncomingMessage.cs" />
|
||||||
|
<Compile Include="$(MSBuildThisFileDirectory)Messages\JoinServerMessage.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Messages\NotificationMessage.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Messages\NotificationMessage.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Messages\DialogMessage.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Messages\DialogMessage.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Messages\MessageBase.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Messages\MessageBase.cs" />
|
||||||
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Sandbox.ModAPI;
|
||||||
using VRage.Game.Components;
|
using VRage.Game.Components;
|
||||||
|
|
||||||
namespace Torch.Mod
|
namespace Torch.Mod
|
||||||
@@ -12,6 +13,7 @@ namespace Torch.Mod
|
|||||||
{
|
{
|
||||||
public const ulong MOD_ID = 1406994352;
|
public const ulong MOD_ID = 1406994352;
|
||||||
private static bool _init;
|
private static bool _init;
|
||||||
|
public static bool Debug;
|
||||||
|
|
||||||
public override void UpdateAfterSimulation()
|
public override void UpdateAfterSimulation()
|
||||||
{
|
{
|
||||||
@@ -20,6 +22,17 @@ namespace Torch.Mod
|
|||||||
|
|
||||||
_init = true;
|
_init = true;
|
||||||
ModCommunication.Register();
|
ModCommunication.Register();
|
||||||
|
MyAPIGateway.Utilities.MessageEntered += Utilities_MessageEntered;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Utilities_MessageEntered(string messageText, ref bool sendToOthers)
|
||||||
|
{
|
||||||
|
if (messageText == "@!debug")
|
||||||
|
{
|
||||||
|
Debug = !Debug;
|
||||||
|
MyAPIGateway.Utilities.ShowMessage("Torch", $"Debug: {Debug}");
|
||||||
|
sendToOthers = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UnloadData()
|
protected override void UnloadData()
|
||||||
|
@@ -94,6 +94,9 @@ namespace Torch.Server
|
|||||||
[Arg("localplugins", "Loads all pluhins from disk, ignores the plugins defined in config.")]
|
[Arg("localplugins", "Loads all pluhins from disk, ignores the plugins defined in config.")]
|
||||||
public bool LocalPlugins { get; set; }
|
public bool LocalPlugins { get; set; }
|
||||||
|
|
||||||
|
[Arg("disconnect", "When server restarts, all clients are rejected to main menu to prevent auto rejoin")]
|
||||||
|
public bool DisconnectOnRestart { get; set; }
|
||||||
|
|
||||||
public string ChatName { get; set; } = "Server";
|
public string ChatName { get; set; } = "Server";
|
||||||
|
|
||||||
public string ChatColor { get; set; } = "Red";
|
public string ChatColor { get; set; } = "Red";
|
||||||
|
@@ -19,6 +19,7 @@ using Torch.API.Managers;
|
|||||||
using Torch.API.Session;
|
using Torch.API.Session;
|
||||||
using Torch.Commands;
|
using Torch.Commands;
|
||||||
using Torch.Mod;
|
using Torch.Mod;
|
||||||
|
using Torch.Mod.Messages;
|
||||||
using Torch.Server.Commands;
|
using Torch.Server.Commands;
|
||||||
using Torch.Server.Managers;
|
using Torch.Server.Managers;
|
||||||
using Torch.Utils;
|
using Torch.Utils;
|
||||||
@@ -152,6 +153,11 @@ namespace Torch.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Restart()
|
public override void Restart()
|
||||||
{
|
{
|
||||||
|
if (Config.DisconnectOnRestart)
|
||||||
|
{
|
||||||
|
ModCommunication.SendMessageToClients(new JoinServerMessage("0.0.0.0:25555"));
|
||||||
|
}
|
||||||
|
|
||||||
if (IsRunning)
|
if (IsRunning)
|
||||||
Save().ContinueWith(DoRestart, this, TaskContinuationOptions.RunContinuationsAsynchronously);
|
Save().ContinueWith(DoRestart, this, TaskContinuationOptions.RunContinuationsAsynchronously);
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user