From 5e1468facb339ca82588ebfa4cddeea2188ee5e3 Mon Sep 17 00:00:00 2001 From: teinarss Date: Thu, 17 Jun 2021 18:45:07 +0200 Subject: [PATCH] Fix NRE with ConnectionStateChanged --- OpenRA.Game/Game.cs | 14 +++++++------- OpenRA.Game/Network/Connection.cs | 5 +---- OpenRA.Game/Network/ReplayConnection.cs | 1 - .../Widgets/Logic/ConnectionLogic.cs | 4 ++-- .../Widgets/Logic/DisconnectWatcherLogic.cs | 7 +++++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index d0880e54b0..62e5b80ff5 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -62,17 +62,17 @@ namespace OpenRA public static OrderManager JoinServer(ConnectionTarget endpoint, string password, bool recordReplay = true) { - var connection = new NetworkConnection(endpoint); + var newConnection = new NetworkConnection(endpoint); if (recordReplay) - connection.StartRecording(() => { return TimestampedFilename(); }); + newConnection.StartRecording(() => { return TimestampedFilename(); }); - var om = new OrderManager(connection); + var om = new OrderManager(newConnection); JoinInner(om); CurrentServerSettings.Password = password; CurrentServerSettings.Target = endpoint; lastConnectionState = ConnectionState.PreConnecting; - ConnectionStateChanged(OrderManager, password, connection); + ConnectionStateChanged(OrderManager, password, newConnection); return om; } @@ -653,10 +653,10 @@ namespace OpenRA { PerformDelayedActions(); - if (OrderManager.Connection.ConnectionState != lastConnectionState) + if (OrderManager.Connection is NetworkConnection nc && nc.ConnectionState != lastConnectionState) { - lastConnectionState = OrderManager.Connection.ConnectionState; - ConnectionStateChanged(OrderManager, null, null); + lastConnectionState = nc.ConnectionState; + ConnectionStateChanged(OrderManager, null, nc); } InnerLogicTick(OrderManager); diff --git a/OpenRA.Game/Network/Connection.cs b/OpenRA.Game/Network/Connection.cs index 5e55691ae7..6708a81a6f 100644 --- a/OpenRA.Game/Network/Connection.cs +++ b/OpenRA.Game/Network/Connection.cs @@ -31,7 +31,6 @@ namespace OpenRA.Network public interface IConnection : IDisposable { int LocalClientId { get; } - ConnectionState ConnectionState { get; } void Send(int frame, List orders); void SendImmediate(IEnumerable orders); void SendSync(int frame, byte[] syncData); @@ -51,8 +50,6 @@ namespace OpenRA.Network public virtual int LocalClientId => 1; - public virtual ConnectionState ConnectionState => ConnectionState.PreConnecting; - public virtual void Send(int frame, List orders) { var ms = new MemoryStream(); @@ -256,7 +253,7 @@ namespace OpenRA.Network } public override int LocalClientId => clientId; - public override ConnectionState ConnectionState => connectionState; + public ConnectionState ConnectionState => connectionState; public override void SendSync(int frame, byte[] syncData) { diff --git a/OpenRA.Game/Network/ReplayConnection.cs b/OpenRA.Game/Network/ReplayConnection.cs index b5f6ea4ca4..f5ba7af103 100644 --- a/OpenRA.Game/Network/ReplayConnection.cs +++ b/OpenRA.Game/Network/ReplayConnection.cs @@ -34,7 +34,6 @@ namespace OpenRA.Network Dictionary lastClientsFrame = new Dictionary(); public int LocalClientId => -1; - public ConnectionState ConnectionState => ConnectionState.Connected; public IPEndPoint EndPoint => throw new NotSupportedException("A replay connection doesn't have an endpoint"); diff --git a/OpenRA.Mods.Common/Widgets/Logic/ConnectionLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ConnectionLogic.cs index 148f09fba4..7cb142ec07 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ConnectionLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ConnectionLogic.cs @@ -22,12 +22,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic void ConnectionStateChanged(OrderManager om, string password, NetworkConnection connection) { - if (om.Connection.ConnectionState == ConnectionState.Connected) + if (connection.ConnectionState == ConnectionState.Connected) { CloseWindow(); onConnect(); } - else if (om.Connection.ConnectionState == ConnectionState.NotConnected) + else if (connection.ConnectionState == ConnectionState.NotConnected) { CloseWindow(); diff --git a/OpenRA.Mods.Common/Widgets/Logic/DisconnectWatcherLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/DisconnectWatcherLogic.cs index 66a41f6887..b84865e5a1 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/DisconnectWatcherLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/DisconnectWatcherLogic.cs @@ -22,14 +22,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic var disconnected = false; widget.Get("DISCONNECT_WATCHER").OnTick = () => { - if (disconnected || orderManager.Connection.ConnectionState != ConnectionState.NotConnected) + if (!(orderManager.Connection is NetworkConnection connection)) + return; + + if (disconnected || connection.ConnectionState != ConnectionState.NotConnected) return; Game.RunAfterTick(() => Ui.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs { { "orderManager", orderManager }, { "password", CurrentServerSettings.Password }, - { "connection", orderManager.Connection as NetworkConnection }, + { "connection", connection }, { "onAbort", null }, { "onRetry", null } }));