Skip to content

Commit f1616f6

Browse files
committed
Implement kick on restart
1 parent a42f5ab commit f1616f6

8 files changed

Lines changed: 138 additions & 37 deletions

File tree

‎Torch.API/ITorchConfig.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface ITorchConfig
2222
string WaitForPID { get; set; }
2323
string ChatName { get; set; }
2424
string ChatColor { get; set; }
25+
bool DisconnectOnRestart { get; set; }
2526

2627
bool Save(string path = null);
2728
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using ProtoBuf;
5+
using Sandbox.ModAPI;
6+
7+
namespace Torch.Mod.Messages
8+
{
9+
[ProtoContract]
10+
public class JoinServerMessage : MessageBase
11+
{
12+
[ProtoMember(201)]
13+
public int Delay;
14+
[ProtoMember(202)]
15+
public string Address;
16+
17+
private JoinServerMessage()
18+
{
19+
20+
}
21+
22+
public JoinServerMessage(string address)
23+
{
24+
Address = address;
25+
}
26+
27+
public JoinServerMessage(string address, int delay)
28+
{
29+
Address = address;
30+
Delay = delay;
31+
}
32+
33+
public override void ProcessClient()
34+
{
35+
if (TorchModCore.Debug)
36+
{
37+
MyAPIGateway.Utilities.ShowMessage("Torch", $"Joining server {Address} with delay {Delay}");
38+
}
39+
40+
if (Delay <= 0)
41+
{
42+
MyAPIGateway.Multiplayer.JoinServer(Address);
43+
return;
44+
}
45+
46+
MyAPIGateway.Parallel.StartBackground(() =>
47+
{
48+
MyAPIGateway.Parallel.Sleep(Delay);
49+
MyAPIGateway.Multiplayer.JoinServer(Address);
50+
});
51+
}
52+
53+
public override void ProcessServer()
54+
{
55+
}
56+
}
57+
}

‎Torch.Mod/Messages/MessageBase.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Torch.Mod.Messages
1111
[ProtoInclude(1, typeof(DialogMessage))]
1212
[ProtoInclude(2, typeof(NotificationMessage))]
1313
[ProtoInclude(3, typeof(VoxelResetMessage))]
14+
[ProtoInclude(4, typeof(JoinServerMessage))]
1415
#endregion
1516

1617
[ProtoContract]
@@ -28,7 +29,7 @@ public abstract class MessageBase
2829
internal ulong[] Ignore;
2930
internal byte[] CompressedData;
3031
}
31-
32+
3233
public enum MessageTarget
3334
{
3435
/// <summary>

‎Torch.Mod/ModCommunication.cs‎

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using VRage;
1111
using VRage.Collections;
1212
using VRage.Game.ModAPI;
13+
using VRage.Network;
1314
using VRage.Utils;
1415
using Task = ParallelTasks.Task;
1516

@@ -50,6 +51,10 @@ private static void MessageHandler(byte[] bytes)
5051
{
5152
var m = _messagePool.Get();
5253
m.CompressedData = bytes;
54+
#if TORCH
55+
m.SenderId = MyEventContext.Current.Sender.Value;
56+
#endif
57+
5358
_processing.Add(m);
5459
}
5560

@@ -59,10 +64,19 @@ public static void DoProcessing()
5964
{
6065
try
6166
{
62-
var m = _processing.Take();
67+
MessageBase m;
68+
try
69+
{
70+
m = _processing.Take();
71+
}
72+
catch
73+
{
74+
continue;
75+
}
76+
6377
MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}");
6478

65-
if (m is IncomingMessage)
79+
if (m is IncomingMessage) //process incoming messages
6680
{
6781
MessageBase i;
6882
try
@@ -78,50 +92,55 @@ public static void DoProcessing()
7892
continue;
7993
}
8094

95+
if (TorchModCore.Debug)
96+
MyAPIGateway.Utilities.ShowMessage("Torch", $"Received message of type {i.GetType().Name}");
97+
8198
if (MyAPIGateway.Multiplayer.IsServer)
8299
i.ProcessServer();
83100
else
84101
i.ProcessClient();
85102
}
86-
else
103+
else //process outgoing messages
87104
{
105+
if (TorchModCore.Debug)
106+
MyAPIGateway.Utilities.ShowMessage("Torch", $"Sending message of type {m.GetType().Name}");
107+
88108
var b = MyAPIGateway.Utilities.SerializeToBinary(m);
89109
m.CompressedData = MyCompression.Compress(b);
90110

91-
MyAPIGateway.Utilities.InvokeOnGameThread(() =>
111+
switch (m.TargetType)
92112
{
113+
case MessageTarget.Single:
114+
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target);
115+
break;
116+
case MessageTarget.Server:
117+
MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData);
118+
break;
119+
case MessageTarget.AllClients:
120+
MyAPIGateway.Players.GetPlayers(_playerCache);
121+
foreach (var p in _playerCache)
122+
{
123+
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId)
124+
continue;
125+
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
126+
}
127+
128+
break;
129+
case MessageTarget.AllExcept:
130+
MyAPIGateway.Players.GetPlayers(_playerCache);
131+
foreach (var p in _playerCache)
132+
{
133+
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId))
134+
continue;
135+
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
136+
}
137+
138+
break;
139+
default:
140+
throw new Exception();
141+
}
93142

94-
switch (m.TargetType)
95-
{
96-
case MessageTarget.Single:
97-
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target);
98-
break;
99-
case MessageTarget.Server:
100-
MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData);
101-
break;
102-
case MessageTarget.AllClients:
103-
MyAPIGateway.Players.GetPlayers(_playerCache);
104-
foreach (var p in _playerCache)
105-
{
106-
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId)
107-
continue;
108-
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
109-
}
110-
break;
111-
case MessageTarget.AllExcept:
112-
MyAPIGateway.Players.GetPlayers(_playerCache);
113-
foreach (var p in _playerCache)
114-
{
115-
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId))
116-
continue;
117-
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
118-
}
119-
break;
120-
default:
121-
throw new Exception();
122-
}
123-
_playerCache.Clear();
124-
});
143+
_playerCache.Clear();
125144
}
126145
}
127146
catch (Exception ex)
@@ -130,7 +149,7 @@ public static void DoProcessing()
130149
}
131150
}
132151

133-
MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECEIVED!");
152+
MyLog.Default.WriteLineAndConsole("TORCH MOD: INFO: Communication thread shut down successfully! THIS IS NOT AN ERROR");
134153
//exit signal received. Clean everything and GTFO
135154
_processing?.Dispose();
136155
_processing = null;

‎Torch.Mod/Torch.Mod.projitems‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)Messages\IncomingMessage.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)Messages\JoinServerMessage.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)Messages\NotificationMessage.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)Messages\DialogMessage.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)Messages\MessageBase.cs" />

‎Torch.Mod/TorchModCore.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Sandbox.ModAPI;
67
using VRage.Game.Components;
78

89
namespace Torch.Mod
@@ -12,6 +13,7 @@ public class TorchModCore : MySessionComponentBase
1213
{
1314
public const ulong MOD_ID = 1406994352;
1415
private static bool _init;
16+
public static bool Debug;
1517

1618
public override void UpdateAfterSimulation()
1719
{
@@ -20,6 +22,17 @@ public override void UpdateAfterSimulation()
2022

2123
_init = true;
2224
ModCommunication.Register();
25+
MyAPIGateway.Utilities.MessageEntered += Utilities_MessageEntered;
26+
}
27+
28+
private void Utilities_MessageEntered(string messageText, ref bool sendToOthers)
29+
{
30+
if (messageText == "@!debug")
31+
{
32+
Debug = !Debug;
33+
MyAPIGateway.Utilities.ShowMessage("Torch", $"Debug: {Debug}");
34+
sendToOthers = false;
35+
}
2336
}
2437

2538
protected override void UnloadData()

‎Torch.Server/TorchConfig.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public string InstancePath
9494
[Arg("localplugins", "Loads all pluhins from disk, ignores the plugins defined in config.")]
9595
public bool LocalPlugins { get; set; }
9696

97+
[Arg("disconnect", "When server restarts, all clients are rejected to main menu to prevent auto rejoin")]
98+
public bool DisconnectOnRestart { get; set; }
99+
97100
public string ChatName { get; set; } = "Server";
98101

99102
public string ChatColor { get; set; } = "Red";

‎Torch.Server/TorchServer.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Torch.API.Session;
2020
using Torch.Commands;
2121
using Torch.Mod;
22+
using Torch.Mod.Messages;
2223
using Torch.Server.Commands;
2324
using Torch.Server.Managers;
2425
using Torch.Utils;
@@ -152,6 +153,11 @@ public override void Stop()
152153
/// </summary>
153154
public override void Restart()
154155
{
156+
if (Config.DisconnectOnRestart)
157+
{
158+
ModCommunication.SendMessageToClients(new JoinServerMessage("0.0.0.0:25555"));
159+
}
160+
155161
if (IsRunning)
156162
Save().ContinueWith(DoRestart, this, TaskContinuationOptions.RunContinuationsAsynchronously);
157163
else

0 commit comments

Comments
 (0)