Skip to content

Commit

Permalink
fix: less aggressive reconnect, hello on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed Jul 8, 2024
1 parent e926424 commit f06a010
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion AutoSweep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<AssemblyName>autoSweep</AssemblyName>
<AssemblyVersion>1.4.4.0</AssemblyVersion>
<AssemblyVersion>1.4.5.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
31 changes: 21 additions & 10 deletions Paissa/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading.Tasks;
using AutoSweep.Structures;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Plugin.Services;
using DebounceThrottle;
using Newtonsoft.Json;
using WebSocketSharp;
Expand All @@ -16,8 +15,10 @@ namespace AutoSweep.Paissa {
public class PaissaClient : IDisposable {
private readonly HttpClient http;
private WebSocket ws;
private int wsAttempts = 0;
private bool disposed = false;
private string sessionToken;
internal bool needsHello = true;

// dalamud
private Plugin plugin;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void Dispose() {
/// <summary>
/// Make a POST request to register the current character's content ID.
/// </summary>
public async Task Hello() {
public void Hello() {
IPlayerCharacter player = Plugin.ClientState.LocalPlayer;
if (player == null) return;
var homeworld = player.HomeWorld.GameData;
Expand All @@ -68,12 +69,15 @@ public async Task Hello() {
};
string content = JsonConvert.SerializeObject(charInfo);
Plugin.PluginLog.Debug(content);
var response = await Post("/hello", content, false);
if (response.IsSuccessStatusCode) {
string respText = await response.Content.ReadAsStringAsync();
sessionToken = JsonConvert.DeserializeObject<HelloResponse>(respText).session_token;
Plugin.PluginLog.Info("Completed PaissaDB HELLO");
}

Task.Run(async () => {
var response = await Post("/hello", content, false);
if (response.IsSuccessStatusCode) {
string respText = await response.Content.ReadAsStringAsync();
sessionToken = JsonConvert.DeserializeObject<HelloResponse>(respText).session_token;
Plugin.PluginLog.Info("Completed PaissaDB HELLO");
}
});
}

/// <summary>
Expand Down Expand Up @@ -153,8 +157,8 @@ private async Task<HttpResponseMessage>
if (auth) {
if (sessionToken == null) {
Plugin.PluginLog.Warning("Trying to send authed request but no session token!");
await Hello();
continue;
needsHello = true;
return null;
}

request = new HttpRequestMessage(HttpMethod.Post, $"{apiBase}{route}") {
Expand Down Expand Up @@ -232,6 +236,7 @@ private void ReconnectWS() {

private void OnWSOpen(object sender, EventArgs e) {
Plugin.PluginLog.Information("WebSocket connected");
wsAttempts = 0;
}

private void OnWSMessage(object sender, MessageEventArgs e) {
Expand Down Expand Up @@ -271,7 +276,13 @@ private void OnWSError(object sender, ErrorEventArgs e) {

private void WSReconnectSoon() {
if (ws.IsAlive) return;
if (++wsAttempts > 5) {
Plugin.PluginLog.Warning($"Could not connect to websocket after {wsAttempts} attempts; giving up");
return;
}

int t = new Random().Next(5_000, 15_000);
t *= wsAttempts;
Plugin.PluginLog.Warning(
$"WebSocket closed unexpectedly: will reconnect to socket in {t / 1000f:F3} seconds");
Task.Run(async () => await Task.Delay(t)).ContinueWith(_ => {
Expand Down
10 changes: 4 additions & 6 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Threading.Tasks;
using AutoSweep.Paissa;
using AutoSweep.Structures;
using Dalamud.Game.Command;
Expand Down Expand Up @@ -37,7 +36,6 @@ public sealed class Plugin : IDalamudPlugin {
private readonly WardObserver wardObserver;
private readonly LotteryObserver lotteryObserver;
private readonly PluginUI ui;
private bool clientNeedsHello = true;

public Plugin() {
// setup
Expand Down Expand Up @@ -118,13 +116,13 @@ private void OnChatLinkClick(uint cmdId, SeString seString) {
}

private void OnLogin() {
clientNeedsHello = true;
PaissaClient.needsHello = true;
}

private void OnUpdateEvent(IFramework f) {
if (clientNeedsHello && ClientState?.LocalPlayer != null && PaissaClient != null) {
clientNeedsHello = false;
Task.Run(async () => await PaissaClient.Hello());
if (ClientState?.LocalPlayer != null && PaissaClient != null && PaissaClient.needsHello) {
PaissaClient.needsHello = false;
PaissaClient.Hello();
}
}

Expand Down

0 comments on commit f06a010

Please sign in to comment.