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

Commit 38527f1

Browse files
more thread safety
1 parent 5427f2d commit 38527f1

5 files changed

Lines changed: 13 additions & 16 deletions

File tree

Scripts/Netcode/Client/ENetClient.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public class ENetClient : IDisposable
1212
{
1313
public static readonly Dictionary<ServerPacketOpcode, APacketServer> HandlePacket = ReflectionUtils.LoadInstances<ServerPacketOpcode, APacketServer>("SPacket");
1414

15-
// values grabbed from the server
16-
// =========================================================
17-
public uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
18-
public bool IsHost { get; set; }
19-
// =========================================================
20-
2115
public DateTime PingSent { get; set; }
2216
public int PingMs { get; set;}
2317
public bool WasPingReceived { get; set; }

Scripts/Netcode/Common/SPacketLobby.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public override async Task Handle(ENetClient client)
167167
// LobbyCreate
168168
private async Task HandleCreate()
169169
{
170-
Client.PeerId = Id;
171-
Client.IsHost = true;
170+
NetworkManager.PeerId = Id;
171+
NetworkManager.IsHost = true;
172172
Client.Log($"{GameManager.Options.OnlineUsername} created lobby with their id being {Id}");
173173
Client.Players[Id] = GameManager.Options.OnlineUsername;
174174

@@ -201,7 +201,7 @@ private void HandleCountdownChange()
201201
// LobbyGameStart
202202
private async Task HandleGameStart()
203203
{
204-
if (Client.IsHost)
204+
if (NetworkManager.IsHost)
205205
NetworkManager.GameServer.ENetCmds.Enqueue(new ENetCmd(ENetOpcode.StartGame));
206206

207207
await SceneManager.ChangeScene("Game");
@@ -216,7 +216,7 @@ private async Task HandleGameStart()
216216
public byte LobbyHostId { get; set; }
217217
private async Task HandleInfo()
218218
{
219-
Client.PeerId = Id;
219+
NetworkManager.PeerId = Id;
220220
Client.Log($"{GameManager.Options.OnlineUsername} joined lobby with id {Id}");
221221
Client.Players[Id] = GameManager.Options.OnlineUsername;
222222
Players.ForEach(pair => NetworkManager.GameClient.Players[pair.Key] = pair.Value.Username);

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public void RemovePlayer(byte id)
104104

105105
private void InitMultiplayerStuff()
106106
{
107-
Players[NetworkManager.GameClient.PeerId] = Player;
107+
Players[NetworkManager.PeerId] = Player;
108108
Player.SetUsername(GameManager.Options.OnlineUsername);
109109

110-
bool IsNotClient(uint id) => id != NetworkManager.GameClient.PeerId; // THREAD SAFETY VIOLATION
110+
bool IsNotClient(uint id) => id != NetworkManager.PeerId;
111111

112112
NetworkManager.GameClient.Players
113113
.Where(x => IsNotClient(x.Key))

Scripts/Scenes/Lobby/SceneLobby.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void _Ready()
4545
UIPlayers = new();
4646
TimerCountdownGameStart = new STimer(1000, TimerCountdownCallback, false);
4747

48-
if (!NetworkManager.GameClient.IsHost) // THREAD SAFETY VIOLATION
48+
if (!NetworkManager.IsHost)
4949
BtnStart.Disabled = true;
5050

5151
NetworkManager.GameClient.Players.ForEach(x => AddPlayer(x.Key, x.Value));
@@ -74,7 +74,7 @@ private async void TimerCountdownCallback()
7474
{
7575
TimerCountdownGameStart.Stop();
7676

77-
if (NetworkManager.GameClient.IsHost) // THREAD SAFETY VIOLATION
77+
if (NetworkManager.IsHost)
7878
{
7979
WebClient.TimerPingMasterServer.Stop();
8080

@@ -151,7 +151,7 @@ public void Log(uint peerId, string text)
151151

152152
private async void _on_Ready_pressed()
153153
{
154-
var player = UIPlayers[NetworkManager.GameClient.PeerId]; // THREAD SAFETY VIOLATION
154+
var player = UIPlayers[NetworkManager.PeerId];
155155
player.SetReady(!player.Ready);
156156

157157
BtnReady.Text = player.Ready ? "Ready" : "Not Ready";
@@ -165,7 +165,7 @@ private async void _on_Ready_pressed()
165165

166166
private async void _on_Start_pressed()
167167
{
168-
if (!NetworkManager.GameClient.IsHost) // THREAD SAFETY VIOLATION
168+
if (!NetworkManager.IsHost)
169169
return;
170170

171171
// check if all players are ready

Scripts/Scenes/Main/NetworkManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class NetworkManager : Node
1818
public static NetworkManager Instance { get; set; }
1919
private static CancellationTokenSource ClientConnectingTokenSource { get; set; }
2020

21+
public static uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
22+
public static bool IsHost { get; set; }
23+
2124
public override void _Ready()
2225
{
2326
Instance = this;

0 commit comments

Comments
 (0)