Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConsoleInput for Linux users with try/catch. #1803

Merged
merged 3 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions Projects/Server/Console/ConsoleInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Server.Logging;

namespace Server;

Expand Down Expand Up @@ -147,37 +148,46 @@ private static void DisplayHelp(string arguments)
}
}

private static readonly ILogger logger = LogFactory.GetLogger(typeof(ConsoleInputHandler));

private static async void ProcessConsoleInput()
{
var token = Core.ClosingTokenSource.Token;

while (!token.IsCancellationRequested)
{
var input = Console.ReadLine()?.Trim();

if (Volatile.Read(ref _expectUserInput))
try
{
_input = input;
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}
var input = Console.ReadLine()?.Trim();

if (string.IsNullOrEmpty(input))
{
continue;
}
if (Volatile.Read(ref _expectUserInput))
{
_input = input;
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}

if (string.IsNullOrEmpty(input))
{
continue;
}

var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();
var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();

Action<string> action;
lock (_inputCommands)
Action<string> action;
lock (_inputCommands)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
}

action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
}
catch (Exception e)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
logger.Warning(e, "Failed to process console input");
}

action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
}
}

Expand Down
Loading