feat: add connection status

This commit is contained in:
Anna 2021-01-24 21:24:29 -05:00
parent cc052f6fcf
commit d9b63b3f58
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
3 changed files with 25 additions and 14 deletions

View File

@ -71,7 +71,6 @@ namespace XIVChatPlugin {
this.Relay = new Relay(this); this.Relay = new Relay(this);
this.Relay.Start(); this.Relay.Start();
PluginLog.Log("started");
} }
internal void StopRelay() { internal void StopRelay() {

View File

@ -199,6 +199,10 @@ namespace XIVChatPlugin {
ImGui.Spacing(); ImGui.Spacing();
ImGui.TextUnformatted($"Connection status: {this.Plugin.Relay?.Status ?? ConnectionStatus.Disconnected}");
ImGui.Spacing();
var relayAuth = this.Plugin.Config.RelayAuth ?? ""; var relayAuth = this.Plugin.Config.RelayAuth ?? "";
WithWhiteText(() => ImGui.TextUnformatted("Relay authentication code")); WithWhiteText(() => ImGui.TextUnformatted("Relay authentication code"));
ImGui.PushItemWidth(-1f); ImGui.PushItemWidth(-1f);

View File

@ -9,6 +9,13 @@ using WebSocketSharp;
using XIVChatCommon.Message.Relay; using XIVChatCommon.Message.Relay;
namespace XIVChatPlugin { namespace XIVChatPlugin {
public enum ConnectionStatus {
Disconnected,
Connecting,
Negotiating,
Connected,
}
public class Relay : IDisposable { public class Relay : IDisposable {
#if DEBUG #if DEBUG
private const string RelayUrl = "ws://localhost:14555/"; private const string RelayUrl = "ws://localhost:14555/";
@ -22,6 +29,8 @@ namespace XIVChatPlugin {
private bool Running { get; set; } private bool Running { get; set; }
public ConnectionStatus Status { get; private set; }
private Channel<IToRelay> ToRelay { get; } = Channel.CreateUnbounded<IToRelay>(); private Channel<IToRelay> ToRelay { get; } = Channel.CreateUnbounded<IToRelay>();
internal Relay(Plugin plugin) { internal Relay(Plugin plugin) {
@ -32,7 +41,7 @@ namespace XIVChatPlugin {
EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12, EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12,
}, },
}; };
PluginLog.Log($"using {RelayUrl}");
this.Connection.OnOpen += this.OnOpen; this.Connection.OnOpen += this.OnOpen;
this.Connection.OnMessage += this.OnMessage; this.Connection.OnMessage += this.OnMessage;
this.Connection.OnClose += this.OnClose; this.Connection.OnClose += this.OnClose;
@ -51,8 +60,8 @@ namespace XIVChatPlugin {
this.Running = true; this.Running = true;
this.Status = ConnectionStatus.Connecting;
this.Connection.ConnectAsync(); this.Connection.ConnectAsync();
PluginLog.Log("ran connect");
} }
internal void ResendPublicKey() { internal void ResendPublicKey() {
@ -67,34 +76,28 @@ namespace XIVChatPlugin {
} }
private void OnOpen(object sender, EventArgs e) { private void OnOpen(object sender, EventArgs e) {
PluginLog.Log("onopen"); this.Status = ConnectionStatus.Negotiating;
var auth = this.Plugin.Config.RelayAuth; var auth = this.Plugin.Config.RelayAuth;
if (auth == null) { if (auth == null) {
PluginLog.Log("auth null");
return; return;
} }
var keys = this.Plugin.Config.KeyPair; var keys = this.Plugin.Config.KeyPair;
if (keys == null) { if (keys == null) {
PluginLog.Log("keys null");
return; return;
} }
PluginLog.Log("making registration message");
var message = new RelayRegister { var message = new RelayRegister {
AuthToken = auth, AuthToken = auth,
PublicKey = keys.PublicKey, PublicKey = keys.PublicKey,
}; };
PluginLog.Log("serialising");
var bytes = MessagePackSerializer.Serialize((IToRelay) message); var bytes = MessagePackSerializer.Serialize((IToRelay) message);
PluginLog.Log("sending");
this.Connection.Send(bytes); this.Connection.Send(bytes);
PluginLog.Log("sent registration");
Task.Run(async () => { Task.Run(async () => {
while (this.Running) { while (this.Running) {
PluginLog.Debug("Sending ping to relay");
this.Connection.Ping(); this.Connection.Ping();
await Task.Delay(TimeSpan.FromSeconds(30)); await Task.Delay(TimeSpan.FromSeconds(30));
} }
@ -111,11 +114,13 @@ namespace XIVChatPlugin {
} }
private void OnMessage(object sender, MessageEventArgs e) { private void OnMessage(object sender, MessageEventArgs e) {
PluginLog.Log(e.RawData.ToHexString());
var message = MessagePackSerializer.Deserialize<IFromRelay>(e.RawData); var message = MessagePackSerializer.Deserialize<IFromRelay>(e.RawData);
switch (message) { switch (message) {
case RelaySuccess success: case RelaySuccess success:
if (!success.Success) { if (success.Success) {
this.Status = ConnectionStatus.Connected;
} else {
this.Status = ConnectionStatus.Disconnected;
this.Plugin.StopRelay(); this.Plugin.StopRelay();
} }
@ -156,11 +161,14 @@ namespace XIVChatPlugin {
private void OnClose(object sender, CloseEventArgs e) { private void OnClose(object sender, CloseEventArgs e) {
this.Running = false; this.Running = false;
this.Status = ConnectionStatus.Disconnected;
} }
private void OnError(object sender, ErrorEventArgs e) { private void OnError(object sender, ErrorEventArgs e) {
PluginLog.LogError(e.Exception, $"Error in relay connection: {e.Message}"); PluginLog.LogError(e.Exception, $"Error in relay connection: {e.Message}");
// TODO reconnect this.Running = false;
this.Status = ConnectionStatus.Disconnected;
this.Start();
} }
} }
} }