Skip to content

Commit

Permalink
Merge pull request #8 from lichie567/develop
Browse files Browse the repository at this point in the history
Merge Develop
  • Loading branch information
lichie567 authored Dec 15, 2021
2 parents 07a3c7f + 8de021f commit bd9693d
Show file tree
Hide file tree
Showing 25 changed files with 903 additions and 434 deletions.
72 changes: 48 additions & 24 deletions LMeter/ACT/ACTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ public class ACTClient : ILMeterDisposable

private ACTEvent? LastEvent { get; set; }

public ConnectionStatus Status { get; private set; }
private ConnectionStatus _status { get; set; }

public ACTClient()
{
this.Socket = new ClientWebSocket();
this.CancellationTokenSource = new CancellationTokenSource();
this.Status = ConnectionStatus.NotConnected;
this._status = ConnectionStatus.NotConnected;
}

public static ConnectionStatus Status => Singletons.Get<ACTClient>()._status;

public static ACTEvent? GetLastEvent()
{
ACTClient client = Singletons.Get<ACTClient>();
if (client.Status != ConnectionStatus.Connected ||
if (client._status != ConnectionStatus.Connected ||
client.LastEvent is null)
{
return null;
Expand All @@ -64,22 +66,32 @@ public static void EndEncounter()
chat.PrintChat(message);
}

public static void ClearAct()
public static void Clear(bool clearAct)
{
ChatGui chat = Singletons.Get<ChatGui>();
XivChatEntry message = new XivChatEntry()
Singletons.Get<ACTClient>().LastEvent = null;
if (clearAct)
{
Message = "clear",
Type = XivChatType.Echo
};
ChatGui chat = Singletons.Get<ChatGui>();
XivChatEntry message = new XivChatEntry()
{
Message = "clear",
Type = XivChatType.Echo
};

chat.PrintChat(message);
Singletons.Get<ACTClient>().LastEvent = null;
chat.PrintChat(message);
}
}

public static void RetryConnection(string address)
{
ACTClient client = Singletons.Get<ACTClient>();
client.Reset();
client.Start(address);
}

public void Start(string host)
{
if (this.Status != ConnectionStatus.NotConnected)
if (this._status != ConnectionStatus.NotConnected)
{
PluginLog.Error("Cannot start, ACTClient needs to be reset!");
return;
Expand All @@ -91,16 +103,16 @@ public void Start(string host)
}
catch (Exception ex)
{
this.Status = ConnectionStatus.ConnectionFailed;
PluginLog.Error($"{ex.ToString()}");
this._status = ConnectionStatus.ConnectionFailed;
this.LogConnectionFailure(ex.ToString());
}
}

private async Task Connect(string host)
{
try
{
this.Status = ConnectionStatus.Connecting;
this._status = ConnectionStatus.Connecting;
await this.Socket.ConnectAsync(new Uri(host), this.CancellationTokenSource.Token);

string subscribe = "{\"call\":\"subscribe\",\"events\":[\"CombatData\"]}";
Expand All @@ -112,20 +124,20 @@ await this.Socket.SendAsync(
}
catch (Exception ex)
{
this.Status = ConnectionStatus.ConnectionFailed;
PluginLog.Error($"Failed to connect to ACT!\n{ex.ToString()}");
this._status = ConnectionStatus.ConnectionFailed;
this.LogConnectionFailure(ex.ToString());
return;
}

ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[4096]);
if (buffer.Array is null)
{
this.Status = ConnectionStatus.ConnectionFailed;
PluginLog.Error($"Failed to connect to ACT!\nFailed to allocate receive buffer!");
this._status = ConnectionStatus.ConnectionFailed;
this.LogConnectionFailure("Failed to allocate receive buffer!");
return;
}

this.Status = ConnectionStatus.Connected;
this._status = ConnectionStatus.Connected;
PluginLog.Information("Successfully Established ACT Connection");
try
{
Expand Down Expand Up @@ -165,23 +177,28 @@ await this.Socket.SendAsync(
}
catch (Exception ex)
{
PluginLog.Error(ex.ToString());
this.LogConnectionFailure(ex.ToString());
}
}
}
}
}
while (this.Status == ConnectionStatus.Connected);
while (this._status == ConnectionStatus.Connected);
}
catch
{
// Swallow exception in case something weird happens during shutdown
}
finally
{
this.Shutdown();
}
}

public void Shutdown()
{
this.Status = ConnectionStatus.ShuttingDown;
this._status = ConnectionStatus.ShuttingDown;
this.LastEvent = null;
if (this.Socket.State == WebSocketState.Open ||
this.Socket.State == WebSocketState.Connecting)
{
Expand All @@ -207,14 +224,21 @@ public void Shutdown()
}

this.Socket.Dispose();
this._status = ConnectionStatus.NotConnected;
}

public void Reset()
{
this.Shutdown();
this.Socket = new ClientWebSocket();
this.CancellationTokenSource = new CancellationTokenSource();
this.Status = ConnectionStatus.NotConnected;
this._status = ConnectionStatus.NotConnected;
}

private void LogConnectionFailure(string error)
{
PluginLog.Debug($"Failed to connect to ACT!");
PluginLog.Verbose(error);
}

public void Dispose()
Expand Down
Loading

0 comments on commit bd9693d

Please sign in to comment.