Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit 9e80942

Browse files
Make more use of IDisposable
1 parent d2ac2e3 commit 9e80942

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

Scripts/Netcode/Client/ENetClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace GodotModules.Netcode.Client
1313
{
14-
public abstract class ENetClient
14+
public abstract class ENetClient : IDisposable
1515
{
1616
// values grabbed from the server
1717
// =========================================================
1818
public static uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
1919
public static bool IsHost { get; set; }
2020
// =========================================================
21-
public static CancellationTokenSource CancelTokenSource { get; private set; }
21+
private static CancellationTokenSource CancelTokenSource { get; set; }
2222
public static ConsoleColor LogsColor = ConsoleColor.Yellow;
2323
public static ConcurrentQueue<ENetCmd> ENetCmds { get; set; }
2424
private static int OutgoingId { get; set; }
@@ -245,5 +245,10 @@ protected virtual void Disconnect(Event netEvent) { }
245245
/// </summary>
246246
/// <param name="netEvent"></param>
247247
protected virtual void Timeout(Event netEvent) { }
248+
249+
public void Dispose()
250+
{
251+
CancelTokenSource.Dispose();
252+
}
248253
}
249254
}

Scripts/Netcode/Server/ENetServer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
namespace GodotModules.Netcode.Server
1414
{
15-
public abstract class ENetServer
15+
public abstract class ENetServer : IDisposable
1616
{
17-
public static CancellationTokenSource CancelTokenSource { get; private set; }
17+
private static CancellationTokenSource CancelTokenSource { get; set; }
1818
public static ConsoleColor LogsColor = ConsoleColor.Cyan;
1919
public static bool SomeoneConnected { get; set; }
2020
public static bool Running { get; set; }
@@ -319,5 +319,10 @@ private static void Kick(uint id, DisconnectOpcode opcode)
319319
Peers[id].DisconnectNow((uint)opcode);
320320
Peers.Remove(id);
321321
}
322+
323+
public void Dispose()
324+
{
325+
CancelTokenSource.Dispose();
326+
}
322327
}
323328
}

Scripts/Scenes/Main/NetworkManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,19 @@ private static async Task ExitCleanup()
118118

119119
ClientConnectingTokenSource?.Dispose();
120120

121-
ENetClient.CancelTokenSource?.Dispose();
122-
ENetServer.CancelTokenSource?.Dispose();
123-
124-
//ClientPlayer.EmitPosition?.Dispose();
125-
126121
Instance.GetTree().Quit();
127122
}
128123

129124
public static async void StartClient(string ip, ushort port)
130125
{
126+
GameClient?.Dispose();
131127
GameClient = new GameClient();
132128
await GameClient.Connect(ip, port);
133129
}
134130

135131
public static async void StartServer(ushort port, int maxClients)
136132
{
133+
GameServer?.Dispose();
137134
GameServer = new GameServer();
138135
await GameServer.Start(port, maxClients);
139136
}

0 commit comments

Comments
 (0)