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

Commit ea40a34

Browse files
disconnect opcode thread safety
1 parent 38527f1 commit ea40a34

5 files changed

Lines changed: 10 additions & 6 deletions

File tree

Scripts/Netcode/Client/ENetClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class ENetClient : IDisposable
1616
public int PingMs { get; set;}
1717
public bool WasPingReceived { get; set; }
1818
public ConcurrentQueue<ENetCmd> ENetCmds { get; set; }
19-
public DisconnectOpcode DisconnectOpcode { get; set; }
2019
public bool IsConnected { get => Interlocked.Read(ref Connected) == 1; }
2120
public bool IsRunning { get => Interlocked.Read(ref Running) == 1; }
2221
public bool IsENetThreadRunning { get => Interlocked.Read(ref ENetThreadRunning) == 1; }
@@ -33,7 +32,6 @@ public ENetClient()
3332
{
3433
Outgoing = new();
3534
ENetCmds = new();
36-
DisconnectOpcode = DisconnectOpcode.Disconnected;
3735
}
3836

3937
public async Task Send(ClientPacketOpcode opcode, APacket data = null, PacketFlags flags = PacketFlags.Reliable)

Scripts/Netcode/Client/GameClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ private void HandlePeerLeave(DisconnectOpcode opcode)
3636
ConnectingToLobby = false;
3737
Disconnected = true;
3838
Connected = 0;
39-
DisconnectOpcode = (DisconnectOpcode)opcode;
40-
NetworkManager.GodotCmds.Enqueue(new GodotCmd(GodotOpcode.ChangeScene, "GameServers"));
39+
NetworkManager.GodotCmds.Enqueue(new GodotCmd(GodotOpcode.Disconnect, opcode));
4140
CancelTokenSource.Cancel();
4241
}
4342
}

Scripts/Netcode/Common/_Opcodes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public enum GodotOpcode
8787
LogMessage,
8888
LogError,
8989
ChangeScene,
90-
PopupMessage
90+
PopupMessage,
91+
Disconnect
9192
}
9293

9394
public class ENetCmd

Scripts/Scenes/Game Servers/SceneGameServers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override async void _Ready()
3333
GameClient.Disconnected = false;
3434
var message = "Disconnected";
3535

36-
switch (NetworkManager.GameClient.DisconnectOpcode) // THREAD SAFETY VIOLATION
36+
switch (NetworkManager.DisconnectOpcode)
3737
{
3838
case DisconnectOpcode.Timeout:
3939
message = "Timed out from server";

Scripts/Scenes/Main/NetworkManager.cs

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

21+
public static DisconnectOpcode DisconnectOpcode { get; set; }
2122
public static uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
2223
public static bool IsHost { get; set; }
2324

@@ -97,6 +98,11 @@ public override async void _Process(float delta)
9798
case GodotOpcode.ChangeScene:
9899
await SceneManager.ChangeScene($"{cmd.Data}");
99100
break;
101+
102+
case GodotOpcode.Disconnect:
103+
DisconnectOpcode = (DisconnectOpcode)cmd.Data;
104+
await SceneManager.ChangeScene("GameServers");
105+
break;
100106
}
101107
}
102108
}

0 commit comments

Comments
 (0)