Fix NRE with ConnectionStateChanged

This commit is contained in:
teinarss
2021-06-17 18:45:07 +02:00
committed by abcdefg30
parent 3e6e5a83f3
commit 5e1468facb
5 changed files with 15 additions and 16 deletions

View File

@@ -62,17 +62,17 @@ namespace OpenRA
public static OrderManager JoinServer(ConnectionTarget endpoint, string password, bool recordReplay = true) public static OrderManager JoinServer(ConnectionTarget endpoint, string password, bool recordReplay = true)
{ {
var connection = new NetworkConnection(endpoint); var newConnection = new NetworkConnection(endpoint);
if (recordReplay) if (recordReplay)
connection.StartRecording(() => { return TimestampedFilename(); }); newConnection.StartRecording(() => { return TimestampedFilename(); });
var om = new OrderManager(connection); var om = new OrderManager(newConnection);
JoinInner(om); JoinInner(om);
CurrentServerSettings.Password = password; CurrentServerSettings.Password = password;
CurrentServerSettings.Target = endpoint; CurrentServerSettings.Target = endpoint;
lastConnectionState = ConnectionState.PreConnecting; lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged(OrderManager, password, connection); ConnectionStateChanged(OrderManager, password, newConnection);
return om; return om;
} }
@@ -653,10 +653,10 @@ namespace OpenRA
{ {
PerformDelayedActions(); PerformDelayedActions();
if (OrderManager.Connection.ConnectionState != lastConnectionState) if (OrderManager.Connection is NetworkConnection nc && nc.ConnectionState != lastConnectionState)
{ {
lastConnectionState = OrderManager.Connection.ConnectionState; lastConnectionState = nc.ConnectionState;
ConnectionStateChanged(OrderManager, null, null); ConnectionStateChanged(OrderManager, null, nc);
} }
InnerLogicTick(OrderManager); InnerLogicTick(OrderManager);

View File

@@ -31,7 +31,6 @@ namespace OpenRA.Network
public interface IConnection : IDisposable public interface IConnection : IDisposable
{ {
int LocalClientId { get; } int LocalClientId { get; }
ConnectionState ConnectionState { get; }
void Send(int frame, List<byte[]> orders); void Send(int frame, List<byte[]> orders);
void SendImmediate(IEnumerable<byte[]> orders); void SendImmediate(IEnumerable<byte[]> orders);
void SendSync(int frame, byte[] syncData); void SendSync(int frame, byte[] syncData);
@@ -51,8 +50,6 @@ namespace OpenRA.Network
public virtual int LocalClientId => 1; public virtual int LocalClientId => 1;
public virtual ConnectionState ConnectionState => ConnectionState.PreConnecting;
public virtual void Send(int frame, List<byte[]> orders) public virtual void Send(int frame, List<byte[]> orders)
{ {
var ms = new MemoryStream(); var ms = new MemoryStream();
@@ -256,7 +253,7 @@ namespace OpenRA.Network
} }
public override int LocalClientId => clientId; public override int LocalClientId => clientId;
public override ConnectionState ConnectionState => connectionState; public ConnectionState ConnectionState => connectionState;
public override void SendSync(int frame, byte[] syncData) public override void SendSync(int frame, byte[] syncData)
{ {

View File

@@ -34,7 +34,6 @@ namespace OpenRA.Network
Dictionary<int, int> lastClientsFrame = new Dictionary<int, int>(); Dictionary<int, int> lastClientsFrame = new Dictionary<int, int>();
public int LocalClientId => -1; public int LocalClientId => -1;
public ConnectionState ConnectionState => ConnectionState.Connected;
public IPEndPoint EndPoint => throw new NotSupportedException("A replay connection doesn't have an endpoint"); public IPEndPoint EndPoint => throw new NotSupportedException("A replay connection doesn't have an endpoint");

View File

@@ -22,12 +22,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void ConnectionStateChanged(OrderManager om, string password, NetworkConnection connection) void ConnectionStateChanged(OrderManager om, string password, NetworkConnection connection)
{ {
if (om.Connection.ConnectionState == ConnectionState.Connected) if (connection.ConnectionState == ConnectionState.Connected)
{ {
CloseWindow(); CloseWindow();
onConnect(); onConnect();
} }
else if (om.Connection.ConnectionState == ConnectionState.NotConnected) else if (connection.ConnectionState == ConnectionState.NotConnected)
{ {
CloseWindow(); CloseWindow();

View File

@@ -22,14 +22,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var disconnected = false; var disconnected = false;
widget.Get<LogicTickerWidget>("DISCONNECT_WATCHER").OnTick = () => widget.Get<LogicTickerWidget>("DISCONNECT_WATCHER").OnTick = () =>
{ {
if (disconnected || orderManager.Connection.ConnectionState != ConnectionState.NotConnected) if (!(orderManager.Connection is NetworkConnection connection))
return;
if (disconnected || connection.ConnectionState != ConnectionState.NotConnected)
return; return;
Game.RunAfterTick(() => Ui.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs Game.RunAfterTick(() => Ui.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs
{ {
{ "orderManager", orderManager }, { "orderManager", orderManager },
{ "password", CurrentServerSettings.Password }, { "password", CurrentServerSettings.Password },
{ "connection", orderManager.Connection as NetworkConnection }, { "connection", connection },
{ "onAbort", null }, { "onAbort", null },
{ "onRetry", null } { "onRetry", null }
})); }));