Skip to content

Commit

Permalink
Add missing 'break' to BotFramework and ConfigureAwait(false) to Scri…
Browse files Browse the repository at this point in the history
…ptedBot async calls.
  • Loading branch information
ethanmoffat committed Jan 29, 2025
1 parent 43f80fa commit d74a8a9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
10 changes: 6 additions & 4 deletions EOBot/BotFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public async Task InitializeAsync(IBotFactory botFactory, int delayBetweenInitsM
{
var bot = botFactory.CreateBot(i);
bot.WorkCompleted += () => _doneSignal.Release();
await bot.InitializeAsync(_host, _port);
await bot.InitializeAsync(_host, _port).ConfigureAwait(false);
_botsList.Add(bot);

ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Bot {i} initialized.");
await Task.Delay(delayBetweenInitsMS); // minimum for this is 1sec server-side
await Task.Delay(delayBetweenInitsMS).ConfigureAwait(false); // minimum for this is 1sec server-side

break;
}
catch (Exception ex)
{
Expand All @@ -83,7 +85,7 @@ public async Task InitializeAsync(IBotFactory botFactory, int delayBetweenInitsM
{
var retryDelayTime = TimeSpan.FromMilliseconds(delayBetweenInitsMS + (1000 * attempt * attempt));
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Warning, $"Bot {i} failed to initialize. Retrying in {retryDelayTime.TotalMilliseconds}ms...", ConsoleColor.DarkYellow);
await Task.Delay(retryDelayTime);
await Task.Delay(retryDelayTime).ConfigureAwait(false);
}
}
}
Expand Down Expand Up @@ -123,7 +125,7 @@ public async Task RunAsync()
var continuation = Task.WhenAll(botTasks);
try
{
await continuation;
await continuation.ConfigureAwait(false);
}
catch { }

Expand Down
18 changes: 9 additions & 9 deletions EOBot/Interpreter/BuiltInIdentifierConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ private async Task ConnectAsync(string host, int port)
configRepo.VersionBuild = ((IntVariable)_state.SymbolTable[PredefinedIdentifiers.VERSION].Identifiable).Value;

var connectionActions = c.Resolve<INetworkConnectionActions>();
var connectResult = await connectionActions.ConnectToServer();
var connectResult = await connectionActions.ConnectToServer().ConfigureAwait(false);
if (connectResult != ConnectResult.Success)
throw new ArgumentException($"Bot {_botIndex}: Unable to connect to server! Host={host} Port={port}");

var backgroundReceiveActions = c.Resolve<IBackgroundReceiveActions>();
backgroundReceiveActions.RunBackgroundReceiveLoop();

var handshakeResult = await connectionActions.BeginHandshake(_random.Next(Constants.MaxChallenge));
var handshakeResult = await connectionActions.BeginHandshake(_random.Next(Constants.MaxChallenge)).ConfigureAwait(false);

if (handshakeResult.ReplyCode != InitReply.Ok)
throw new InvalidOperationException($"Bot {_botIndex}: Invalid response from server or connection failed! Must receive an OK reply.");
Expand All @@ -149,38 +149,38 @@ private void Disconnect()

private async Task<int> CreateAccountAsync(string user, string pass)
{
return (int)await _botHelper.CreateAccountAsync(user, pass);
return (int)await _botHelper.CreateAccountAsync(user, pass).ConfigureAwait(false);
}

private async Task<int> LoginAsync(string user, string pass)
{
return (int)await _botHelper.LoginToAccountAsync(user, pass);
return (int)await _botHelper.LoginToAccountAsync(user, pass).ConfigureAwait(false);
}

private async Task<int> CreateAndLoginAsync(string user, string pass)
{
var accountReply = (AccountReply)await CreateAccountAsync(user, pass);
var accountReply = (AccountReply)await CreateAccountAsync(user, pass).ConfigureAwait(false);
if (accountReply == AccountReply.Created || accountReply == AccountReply.Exists)
{
return await LoginAsync(user, pass);
return await LoginAsync(user, pass).ConfigureAwait(false);
}

return (int)LoginReply.WrongUser;
}

private async Task<int> ChangePasswordAsync(string user, string oldPass, string newPass)
{
return (int)await _botHelper.ChangePasswordAsync(user, oldPass, newPass);
return (int)await _botHelper.ChangePasswordAsync(user, oldPass, newPass).ConfigureAwait(false);
}

private async Task<int> CreateCharacterAsync(string charName)
{
return (int)await _botHelper.CreateCharacterAsync(charName);
return (int)await _botHelper.CreateCharacterAsync(charName).ConfigureAwait(false);
}

private async Task<int> DeleteCharacterAsync(string charName, bool force)
{
return (int)await _botHelper.DeleteCharacterAsync(charName, force);
return (int)await _botHelper.DeleteCharacterAsync(charName, force).ConfigureAwait(false);
}

private Task LoginToCharacterAsync(string charName)
Expand Down
4 changes: 2 additions & 2 deletions EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ static async Task<int> Main(string[] args)
{
using (f = new BotFramework(parsedArgs))
{
await f.InitializeAsync(botFactory, parsedArgs.InitDelay);
await f.RunAsync();
await f.InitializeAsync(botFactory, parsedArgs.InitDelay).ConfigureAwait(false);
await f.RunAsync().ConfigureAwait(false);
}

Console.WriteLine();
Expand Down
4 changes: 2 additions & 2 deletions EOBot/ScriptedBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override async Task InitializeAsync(string host, int port)
throw new InvalidOperationException("Something went wrong getting the connect function out of the symbol table");

// call connect function that uses user-defined $version variable instead of base logic that has it hard-coded
await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port));
await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port)).ConfigureAwait(false);

WorkCompleted += () =>
{
Expand All @@ -51,7 +51,7 @@ protected override async Task DoWorkAsync(CancellationToken ct)
if (_programState == null)
throw new InvalidOperationException("Scripted bot must be initialized before it is run");

await _interpreter.Run(_programState);
await _interpreter.Run(_programState).ConfigureAwait(false);
}
}
}

0 comments on commit d74a8a9

Please sign in to comment.