Changed various locking mechanisms.

This commit is contained in:
Tom
2025-03-29 20:28:36 +00:00
parent eddd9e6403
commit fb04f4003f
11 changed files with 272 additions and 155 deletions

View File

@ -54,8 +54,8 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers
_user.DefaultTTSVoice = message.DefaultTTSVoice;
_user.VoicesAvailable = new ConcurrentDictionary<string, string>(message.TTSVoicesAvailable);
_user.VoicesEnabled = new HashSet<string>(message.EnabledTTSVoices);
_user.TwitchConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "twitch");
_user.NightbotConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "nightbot");
_user.TwitchConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "twitch") ?? message.Connections.FirstOrDefault(c => c.Type == "twitch");
_user.NightbotConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "nightbot") ?? message.Connections.FirstOrDefault(c => c.Type == "nightbot");
if (_user.TwitchConnection != null)
{
_logger.Information("Twitch connection: " + _user.TwitchConnection.Name + " / " + _user.TwitchConnection.AccessToken);

View File

@ -28,7 +28,7 @@ namespace TwitchChatTTS.Hermes.Socket
public string? UserId { get; set; }
private readonly System.Timers.Timer _heartbeatTimer;
private readonly IBackoff _backoff;
private readonly object _lock;
private readonly ReaderWriterLockSlim _rwls;
public bool Connected { get; set; }
public bool LoggedIn { get; set; }
@ -62,7 +62,7 @@ namespace TwitchChatTTS.Hermes.Socket
LastHeartbeatReceived = LastHeartbeatSent = DateTime.UtcNow;
URL = $"wss://{BASE_URL}";
_lock = new object();
_rwls = new ReaderWriterLockSlim();
var ttsCreateUserVoice = _bus.GetTopic("tts.user.voice.create");
ttsCreateUserVoice.Subscribe(async data => await Send(3, new RequestMessage()
@ -82,25 +82,35 @@ namespace TwitchChatTTS.Hermes.Socket
public override async Task Connect()
{
lock (_lock)
_rwls.EnterWriteLock();
try
{
if (Connected)
return;
}
_logger.Debug($"Attempting to connect to {URL}");
await ConnectAsync(URL);
_logger.Debug($"Attempting to connect to {URL}");
await ConnectAsync(URL);
}
finally
{
_rwls.ExitWriteLock();
}
}
private async Task Disconnect()
{
lock (_lock)
_rwls.EnterWriteLock();
try
{
if (!Connected)
return;
}
await DisconnectAsync(new SocketDisconnectionEventArgs("Normal disconnection", "Disconnection was executed"));
await DisconnectAsync(new SocketDisconnectionEventArgs("Normal disconnection", "Disconnection was executed"));
}
finally
{
_rwls.ExitWriteLock();
}
}
public async Task CreateTTSVoice(string voiceName)
@ -251,12 +261,10 @@ namespace TwitchChatTTS.Hermes.Socket
OnConnected += async (sender, e) =>
{
lock (_lock)
{
if (Connected)
return;
Connected = true;
}
if (Connected)
return;
Connected = true;
_logger.Information("Tom to Speech websocket client connected.");
_heartbeatTimer.Enabled = true;
@ -273,16 +281,13 @@ namespace TwitchChatTTS.Hermes.Socket
OnDisconnected += async (sender, e) =>
{
lock (_lock)
{
if (!Connected)
return;
if (!Connected)
return;
Connected = false;
LoggedIn = false;
Ready = false;
_user.Slave = true;
}
Connected = false;
LoggedIn = false;
Ready = false;
_user.Slave = true;
_logger.Warning("Tom to Speech websocket client disconnected.");
@ -424,13 +429,21 @@ namespace TwitchChatTTS.Hermes.Socket
public new async Task Send<T>(int opcode, T message)
{
if (!Connected)
_rwls.EnterReadLock();
try
{
_logger.Warning("Tom to Speech websocket client is not connected. Not sending a message.");
return;
}
if (!Connected)
{
_logger.Warning("Tom to Speech websocket client is not connected. Not sending a message.");
return;
}
await base.Send(opcode, message);
await base.Send(opcode, message);
}
finally
{
_rwls.ExitReadLock();
}
}
}
}

View File

@ -23,7 +23,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
return;
}
if (long.TryParse(requestData["chatter"].ToString(), out var chatterId))
if (!long.TryParse(requestData["chatter"].ToString(), out var chatterId))
{
_logger.Warning($"Chatter Id is invalid [chatter id: {chatterId}]");
return;