Skip to content

Commit 0cbae43

Browse files
committed
editor netcode fixes and trying to diagnose jackpot
1 parent 5ac68b3 commit 0cbae43

File tree

7 files changed

+209
-62
lines changed

7 files changed

+209
-62
lines changed

Intersect.Editor/Forms/frmLogin.cs

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ private void InitLocalization()
7777
lblStatus.Text = Strings.Login.connecting;
7878
}
7979

80+
public long LastNetworkStatusChangeTime { get; private set; }
81+
82+
private NetworkStatus _networkStatus;
83+
84+
public void SetNetworkStatus(NetworkStatus networkStatus)
85+
{
86+
_networkStatus = networkStatus;
87+
LastNetworkStatusChangeTime = Timing.Global.MillisecondsUtc;
88+
}
89+
8090
private void tmrSocket_Tick(object sender, EventArgs e)
8191
{
8292
if (!mOptionsLoaded)
@@ -85,68 +95,67 @@ private void tmrSocket_Tick(object sender, EventArgs e)
8595
}
8696

8797
Networking.Network.Update();
88-
var statusString = Strings.Login.connecting;
89-
btnLogin.Enabled = Networking.Network.Connected;
90-
if (Networking.Network.Connected)
98+
btnLogin.Enabled = _networkStatus == NetworkStatus.Online || Networking.Network.Connected;
99+
100+
string statusString = _networkStatus switch
91101
{
92-
statusString = Strings.Login.connected;
93-
}
94-
else if (Networking.Network.Connecting)
102+
NetworkStatus.Unknown => Strings.Login.Denied,
103+
NetworkStatus.Connecting => Strings.Login.connecting,
104+
NetworkStatus.Online => Strings.Login.connected,
105+
NetworkStatus.Offline => Strings.Login.failedtoconnect.ToString(((Globals.ReconnectTime - Timing.Global.MillisecondsUtc) / 1000).ToString("0")),
106+
NetworkStatus.Failed => Strings.Login.Denied,
107+
NetworkStatus.VersionMismatch => Strings.Login.Denied,
108+
NetworkStatus.ServerFull => Strings.Login.Denied,
109+
NetworkStatus.HandshakeFailure => Strings.Login.Denied,
110+
NetworkStatus.Quitting => Strings.Login.Denied,
111+
_ => throw new UnreachableException(),
112+
};
113+
114+
var hasRaptr = Process.GetProcesses()
115+
.ToArray()
116+
.Any(process => process.ProcessName.Contains("raptr"));
117+
if (hasRaptr)
95118
{
96-
}
97-
else if (Networking.Network.ConnectionDenied)
98-
{
99-
statusString = Strings.Login.Denied;
100-
}
101-
else
102-
{
103-
var seconds = (Globals.ReconnectTime - Timing.Global.MillisecondsUtc) / 1000;
104-
statusString = Strings.Login.failedtoconnect.ToString(seconds.ToString("0"));
119+
statusString = Strings.Login.raptr;
120+
btnLogin.Enabled = false;
105121
}
106122

107-
Process.GetProcesses()
108-
.ToList()
109-
.ForEach(
110-
process =>
123+
Globals.LoginForm.lblStatus.Text = statusString;
124+
125+
if (_loginPending && Networking.Network.Connected)
126+
{
127+
if (txtUsername.Text.Trim().Length > 0 && txtPassword.Text.Trim().Length > 0)
128+
{
129+
using (var sha = new SHA256Managed())
111130
{
112-
if (!(process?.ProcessName.Contains("raptr") ?? false))
131+
if (mSavedPassword != "")
113132
{
114-
return;
133+
PacketSender.SendLogin(txtUsername.Text.Trim(), mSavedPassword);
134+
}
135+
else
136+
{
137+
PacketSender.SendLogin(
138+
txtUsername.Text.Trim(),
139+
BitConverter.ToString(sha.ComputeHash(Encoding.UTF8.GetBytes(txtPassword.Text.Trim())))
140+
.Replace("-", "")
141+
);
115142
}
116-
117-
statusString = Strings.Login.raptr;
118-
btnLogin.Enabled = false;
119143
}
120-
);
121-
122-
Globals.LoginForm.lblStatus.Text = statusString;
144+
}
145+
}
123146
}
124147

148+
private bool _loginPending = false;
149+
125150
private void btnLogin_Click(object sender, EventArgs e)
126151
{
127-
if (!Networking.Network.Connected || !btnLogin.Enabled)
152+
if (!Networking.Network.Connected)
128153
{
129-
return;
154+
Networking.Network.Connect();
130155
}
131156

132-
if (txtUsername.Text.Trim().Length > 0 && txtPassword.Text.Trim().Length > 0)
133-
{
134-
using (var sha = new SHA256Managed())
135-
{
136-
if (mSavedPassword != "")
137-
{
138-
PacketSender.SendLogin(txtUsername.Text.Trim(), mSavedPassword);
139-
}
140-
else
141-
{
142-
PacketSender.SendLogin(
143-
txtUsername.Text.Trim(),
144-
BitConverter.ToString(sha.ComputeHash(Encoding.UTF8.GetBytes(txtPassword.Text.Trim())))
145-
.Replace("-", "")
146-
);
147-
}
148-
}
149-
}
157+
_loginPending = true;
158+
btnLogin.Enabled = false;
150159
}
151160

152161
protected override void OnKeyPress(KeyPressEventArgs e)

Intersect.Editor/Intersect.Editor.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@
387387
<Compile Update="Forms\Editors\Events\Event Commands\EventCommand_WaitForRouteCompletion.cs">
388388
<SubType>UserControl</SubType>
389389
</Compile>
390-
<Compile
391-
Update="Forms\Editors\Events\Event Commands\EventCommand_WaitForRouteCompletion.Designer.cs">
390+
<Compile Update="Forms\Editors\Events\Event Commands\EventCommand_WaitForRouteCompletion.Designer.cs">
392391
<DependentUpon>EventCommand_WaitForRouteCompletion.cs</DependentUpon>
393392
</Compile>
394393
<Compile Update="Forms\Editors\Events\Event Commands\EventCommand_Warp.cs">
@@ -406,8 +405,7 @@
406405
<Compile Update="Forms\Editors\Events\Event Commands\Event_MoveRouteAnimationSelector.cs">
407406
<SubType>UserControl</SubType>
408407
</Compile>
409-
<Compile
410-
Update="Forms\Editors\Events\Event Commands\Event_MoveRouteAnimationSelector.Designer.cs">
408+
<Compile Update="Forms\Editors\Events\Event Commands\Event_MoveRouteAnimationSelector.Designer.cs">
411409
<DependentUpon>Event_MoveRouteAnimationSelector.cs</DependentUpon>
412410
</Compile>
413411
<Compile Update="Forms\Editors\Events\Event Commands\Event_MoveRouteDesigner.cs">

Intersect.Editor/Networking/Network.cs

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
using Intersect.Plugins.Helpers;
1515
using Intersect.Plugins.Interfaces;
1616
using Intersect.Rsa;
17+
using System.Diagnostics.CodeAnalysis;
18+
using System.Net;
19+
using Intersect.Utilities;
20+
using Intersect.Network.Packets.Unconnected.Client;
1721

1822
namespace Intersect.Editor.Networking
1923
{
@@ -69,12 +73,22 @@ internal static partial class Network
6973

7074
public static bool ConnectionDenied;
7175

72-
public static ClientNetwork EditorLidgrenNetwork;
76+
public static ClientNetwork EditorLidgrenNetwork { get; set; }
7377

7478
internal static PacketHandler PacketHandler { get; private set; }
7579

7680
public static bool Connected => EditorLidgrenNetwork?.IsConnected ?? false;
7781

82+
public static void Connect()
83+
{
84+
if (EditorLidgrenNetwork?.Connect() ?? true)
85+
{
86+
return;
87+
}
88+
89+
Log.Warn("Failed to connect to server.");
90+
}
91+
7892
public static void InitNetwork()
7993
{
8094
if (EditorLidgrenNetwork == null)
@@ -87,6 +101,7 @@ public static void InitNetwork()
87101
}
88102

89103
var packetHandlerRegistry = new PacketHandlerRegistry(packetTypeRegistry, logger);
104+
packetHandlerRegistry.TryRegisterAvailableTypeHandlers(typeof(Network).Assembly, requireAttribute: true);
90105
var packetHelper = new PacketHelper(packetTypeRegistry, packetHandlerRegistry);
91106
PackedIntersectPacket.AddKnownTypes(packetHelper.AvailablePacketTypes);
92107
var virtualEditorContext = new VirtualEditorContext(packetHelper, logger);
@@ -117,15 +132,99 @@ public static void InitNetwork()
117132
if (!Connected)
118133
{
119134
Connecting = true;
120-
if (!EditorLidgrenNetwork.Connect())
121-
{
122-
Log.Error("An error occurred while attempting to connect.");
123-
}
124135
}
125136
}
126137

138+
/// <summary>
139+
/// Interval between status pings in ms (e.g. full, bad version, etc.)
140+
/// </summary>
141+
#if DEBUG
142+
private const long ServerStatusPingInterval = 1_000;
143+
#else
144+
private const long ServerStatusPingInterval = 15_000;
145+
#endif
146+
147+
private static long _nextServerStatusPing;
148+
149+
private static string? _lastHost;
150+
private static int? _lastPort;
151+
private static IPEndPoint? _lastEndpoint;
152+
153+
private static bool TryResolveEndPoint([NotNullWhen(true)] out IPEndPoint? endPoint)
154+
{
155+
var currentHost = ClientConfiguration.Instance.Host;
156+
if (!string.Equals(_lastHost, currentHost))
157+
{
158+
_lastHost = currentHost;
159+
_lastEndpoint = default;
160+
}
161+
162+
var currentPort = ClientConfiguration.Instance.Port;
163+
if (_lastPort != currentPort)
164+
{
165+
_lastPort = currentPort;
166+
_lastEndpoint = default;
167+
}
168+
169+
if (string.IsNullOrWhiteSpace(_lastHost))
170+
{
171+
endPoint = default;
172+
return false;
173+
}
174+
175+
if (_lastEndpoint != default)
176+
{
177+
endPoint = _lastEndpoint;
178+
return true;
179+
}
180+
181+
var address = Dns.GetHostAddresses(_lastHost).FirstOrDefault();
182+
var port = _lastPort;
183+
if (address == default || !port.HasValue)
184+
{
185+
endPoint = default;
186+
return false;
187+
}
188+
189+
endPoint = new IPEndPoint(address, port.Value);
190+
_lastEndpoint = endPoint;
191+
return true;
192+
}
193+
127194
public static void Update()
128195
{
196+
if (Globals.LoginForm?.Visible ?? false)
197+
{
198+
var now = Timing.Global.MillisecondsUtc;
199+
// ReSharper disable once InvertIf
200+
if (_nextServerStatusPing <= now)
201+
{
202+
if (TryResolveEndPoint(out var serverEndpoint))
203+
{
204+
var network = EditorLidgrenNetwork;
205+
if (network == default)
206+
{
207+
Log.Info("No network created to poll for server status.");
208+
}
209+
else
210+
{
211+
network.SendUnconnected(serverEndpoint, new ServerStatusRequestPacket());
212+
}
213+
}
214+
else
215+
{
216+
Log.Info($"Unable to resolve '{_lastHost}:{_lastPort}'");
217+
}
218+
219+
if (Globals.LoginForm.LastNetworkStatusChangeTime + ServerStatusPingInterval + ServerStatusPingInterval / 2 < now)
220+
{
221+
Globals.LoginForm.SetNetworkStatus(NetworkStatus.Offline);
222+
}
223+
224+
_nextServerStatusPing = now + ServerStatusPingInterval;
225+
}
226+
}
227+
129228
if (!Connected && !Connecting && !ConnectionDenied)
130229
{
131230
InitNetwork();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Intersect.Editor.General;
2+
using Intersect.Logging;
3+
using Intersect.Network;
4+
using Intersect.Network.Packets.Unconnected.Server;
5+
6+
namespace Intersect.Editor.Networking.UnconnectedPacketHandlers;
7+
8+
[PacketHandler(typeof(ServerStatusResponsePacket))]
9+
public class ServerStatusResponsePacketHandler : AbstractPacketHandler<ServerStatusResponsePacket>
10+
{
11+
public override bool Handle(IPacketSender packetSender, ServerStatusResponsePacket packet)
12+
{
13+
try
14+
{
15+
Globals.LoginForm.SetNetworkStatus(packet.Status);
16+
return true;
17+
}
18+
catch (Exception exception)
19+
{
20+
Log.Debug(exception);
21+
return false;
22+
}
23+
}
24+
}

Intersect.Server/Database/IntersectDbContext.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
5757
: QueryTrackingBehavior.TrackAll);
5858

5959
var loggerFactory = ContextOptions.LoggerFactory;
60-
#if DEBUG
60+
#if DEBUG
6161
loggerFactory ??= new IntersectLoggerFactory();
62-
#endif
62+
#endif
63+
64+
var enableSensitiveDataLogging = ContextOptions.EnableSensitiveDataLogging;
65+
#if DEBUG
66+
enableSensitiveDataLogging = true;
67+
#endif
68+
69+
var enableDetailedErrors = ContextOptions.EnableDetailedErrors;
70+
#if DEBUG
71+
enableDetailedErrors = true;
72+
#endif
6373

6474
_ = optionsBuilder
65-
.EnableDetailedErrors(ContextOptions.EnableDetailedErrors)
66-
.EnableSensitiveDataLogging(ContextOptions.EnableSensitiveDataLogging)
75+
.EnableDetailedErrors(enableDetailedErrors)
76+
.EnableSensitiveDataLogging(enableSensitiveDataLogging)
6777
.ReplaceService<IModelCacheKeyFactory, IntersectModelCacheKeyFactory>()
6878
.UseLoggerFactory(loggerFactory)
6979
.UseQueryTrackingBehavior(queryTrackingBehavior);

Intersect.Server/Database/PlayerData/User.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public void Save(PlayerContext? playerContext, bool force = false, bool create =
367367
foreach (var entry in ex.Entries)
368368
{
369369
var type = entry.GetType().FullName;
370-
concurrencyErrors.AppendLine($"Entry Type [{type}]");
370+
concurrencyErrors.AppendLine($"Entry Type [{type} / {entry.State}]");
371371
concurrencyErrors.AppendLine("--------------------");
372372

373373
var proposedValues = entry.CurrentValues;
@@ -380,6 +380,13 @@ public void Save(PlayerContext? playerContext, bool force = false, bool create =
380380
);
381381
}
382382

383+
foreach (var property in entry.Properties)
384+
{
385+
concurrencyErrors.AppendLine(
386+
$"{property.Metadata.Name} {nameof(property.IsModified)}={property.IsModified} {nameof(property.IsTemporary)}={property.IsTemporary}"
387+
);
388+
}
389+
383390
concurrencyErrors.AppendLine("");
384391
concurrencyErrors.AppendLine("");
385392
}

Intersect.Server/Networking/Client.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void SetUser(User user)
135135

136136
if (user != null && user != User)
137137
{
138-
User.Login(user, mConnection.Ip);
138+
User.Login(user, mConnection?.Ip);
139139
}
140140

141141
User = user;

0 commit comments

Comments
 (0)