Skip to content

Commit

Permalink
TwitchChatVotingProxy: Ensure pipe is killed on any exception
Browse files Browse the repository at this point in the history
Might fix occasional lockup when reloading mod with voting enabled

Also remove enormous try finally block, the global mutex is released on program stop anyways
  • Loading branch information
pongo1231 committed Sep 20, 2023
1 parent 774c583 commit 7417e96
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 74 deletions.
7 changes: 5 additions & 2 deletions TwitchChatVotingProxy/ChaosPipe/ChaosPipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void PipeTick(object sender, ElapsedEventArgs e)
SendHeartBeat();
ReadPipe();
}
catch (IOException exception)
catch (Exception exception)
{
m_Logger.Information("Pipe disconnected: " + exception.Message);
DisconnectFromPipe();
Expand All @@ -182,7 +182,10 @@ private void PipeTick(object sender, ElapsedEventArgs e)
private void ReadPipe()
{
// If no reading task is active, create one
if (m_ReadPipeTask == null) m_ReadPipeTask = m_PipeReader.ReadLineAsync();
if (m_ReadPipeTask == null)
{
m_ReadPipeTask = m_PipeReader.ReadLineAsync();
}
// If the reading task is created and complete, get its results
else if (m_ReadPipeTask.IsCompleted)
{
Expand Down
137 changes: 65 additions & 72 deletions TwitchChatVotingProxy/TwitchChatVotingProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,95 +40,88 @@ private static async Task Main(string[] args)
var mutex = new Mutex(false, "ChaosModVVotingMutex");
mutex.WaitOne();

try
var votingMode = (EVotingMode)config.ReadValueInt("VotingChanceSystem", 0, "TwitchVotingChanceSystem");
var overlayMode = (EOverlayMode)config.ReadValueInt("VotingOverlayMode", 0, "TwitchVotingOverlayMode");
var retainInitialVotes = config.ReadValueBool("VotingChanceSystemRetainChance", false, "TwitchVotingChanceSystemRetainChance");

// Check if OBS overlay should be shown
OverlayServer.OverlayServer overlayServer = null;
if (overlayMode == EOverlayMode.OVERLAY_OBS)
{
var votingMode = (EVotingMode)config.ReadValueInt("VotingChanceSystem", 0, "TwitchVotingChanceSystem");
var overlayMode = (EOverlayMode)config.ReadValueInt("VotingOverlayMode", 0, "TwitchVotingOverlayMode");
var retainInitialVotes = config.ReadValueBool("VotingChanceSystemRetainChance", false, "TwitchVotingChanceSystemRetainChance");
// Create component
var overlayServerPort = config.ReadValueInt("OverlayServerPort", 9091);
var overlayServerConfig = new OverlayServerConfig(votingMode, retainInitialVotes, overlayServerPort);
overlayServer = new OverlayServer.OverlayServer(overlayServerConfig);
}

// Check if OBS overlay should be shown
OverlayServer.OverlayServer overlayServer = null;
if (overlayMode == EOverlayMode.OVERLAY_OBS)
{
// Create component
var overlayServerPort = config.ReadValueInt("OverlayServerPort", 9091);
var overlayServerConfig = new OverlayServerConfig(votingMode, retainInitialVotes, overlayServerPort);
overlayServer = new OverlayServer.OverlayServer(overlayServerConfig);
}
// Create components
var chaosPipe = new ChaosPipeClient();

// Create components
var chaosPipe = new ChaosPipeClient();
var votingReceivers = new List<(string Name, IVotingReceiver VotingReceiver)>();
if (config.ReadValueBool("EnableVotingTwitch", false))
{
votingReceivers.Add(("Twitch", new TwitchVotingReceiver(config, chaosPipe)));
}
if (config.ReadValueBool("EnableVotingDiscord", false))
{
votingReceivers.Add(("Discord", new DiscordVotingReceiver(config, chaosPipe)));
}

var votingReceivers = new List<(string Name, IVotingReceiver VotingReceiver)>();
if (config.ReadValueBool("EnableVotingTwitch", false))
{
votingReceivers.Add(("Twitch", new TwitchVotingReceiver(config, chaosPipe)));
}
if (config.ReadValueBool("EnableVotingDiscord", false))
{
votingReceivers.Add(("Discord", new DiscordVotingReceiver(config, chaosPipe)));
}
foreach (var votingReceiver in votingReceivers)
{
m_Logger.Information($"Initializing {votingReceiver.Name} voting");

foreach (var votingReceiver in votingReceivers)
try
{
m_Logger.Information($"Initializing {votingReceiver.Name} voting");

try
{
if (!await votingReceiver.VotingReceiver.Init())
{
m_Logger.Fatal($"Failed to initialize {votingReceiver.Name} voting");

return;
}
}
catch (Exception exception)
if (!await votingReceiver.VotingReceiver.Init())
{
m_Logger.Fatal($"Failed to initialize {votingReceiver.Name} voting\nException occured: ${exception}");
chaosPipe.SendErrorMessage($"Error occured while initializing {votingReceiver.Name} voting." +
$" Check chaosproxy.log for details.");
m_Logger.Fatal($"Failed to initialize {votingReceiver.Name} voting");

return;
}
}

// Start the chaos mod controller
m_Logger.Information("Initializing controller");

var permittedUsernames = config.ReadValue("PermittedUsernames", "", "TwitchPermittedUsernames").ToLower()
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToArray();
var chaosModControllerConfig = new ChaosModControllerConfig()
{
VotingMode = votingMode,
OverlayMode = overlayMode,
RetainInitialVotes = retainInitialVotes,
PermittedUsernames = permittedUsernames,
VoteablePrefix = config.ReadValue("VoteablePrefix", "")
};
new ChaosModController(chaosPipe, overlayServer, votingReceivers.Select(item => item.VotingReceiver).ToArray(),
chaosModControllerConfig);

m_Logger.Information("Sending hello to mod");

chaosPipe.SendMessageToPipe("hello");
while (!chaosPipe.GotHelloBack && chaosPipe.IsConnected())
catch (Exception exception)
{
await Task.Delay(0);
}
m_Logger.Fatal($"Failed to initialize {votingReceiver.Name} voting\nException occured: ${exception}");
chaosPipe.SendErrorMessage($"Error occured while initializing {votingReceiver.Name} voting." +
$" Check chaosproxy.log for details.");

if (chaosPipe.GotHelloBack)
{
m_Logger.Information("Received hello_back from mod!");
return;
}
}

while (chaosPipe.IsConnected())
{
await Task.Delay(100);
}
// Start the chaos mod controller
m_Logger.Information("Initializing controller");

var permittedUsernames = config.ReadValue("PermittedUsernames", "", "TwitchPermittedUsernames").ToLower()
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToArray();
var chaosModControllerConfig = new ChaosModControllerConfig()
{
VotingMode = votingMode,
OverlayMode = overlayMode,
RetainInitialVotes = retainInitialVotes,
PermittedUsernames = permittedUsernames,
VoteablePrefix = config.ReadValue("VoteablePrefix", "")
};
new ChaosModController(chaosPipe, overlayServer, votingReceivers.Select(item => item.VotingReceiver).ToArray(),
chaosModControllerConfig);

m_Logger.Information("Sending hello to mod");

chaosPipe.SendMessageToPipe("hello");
while (!chaosPipe.GotHelloBack && chaosPipe.IsConnected())
{
await Task.Delay(0);
}
finally

if (chaosPipe.GotHelloBack)
{
m_Logger.Information("Received hello_back from mod!");
}

while (chaosPipe.IsConnected())
{
mutex.ReleaseMutex();
await Task.Delay(100);
}

m_Logger.Information("Pipe disconnected, ending program.");
Expand Down

0 comments on commit 7417e96

Please sign in to comment.