Exercise: Fastest Server Matchmaking

Find the fastest responding game server using asynchronous task combinators.

Problem statement

In multiplayer gaming, a client must often ping multiple regional matchmaking servers simultaneously to determine which one offers the lowest latency. You need to implement a utility that simulates pinging three servers and automatically selects the one that responds first, allowing the player to connect without waiting for the slower servers.

Task requirements

  • Start the simulated ping process for three regions: “US-East”, “Europe”, and “Asia”.

  • Determine which server responds first.

  • Extract and return the name of the winning server from the utility class.

Constraints

  • You must use Task.WhenAny inside the Matchmaker class to race the three tasks against each other.

  • You must await the winning task to extract its string result.

  • Do not block the thread using .Result or .Wait().

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Pass the three tasks into Task.WhenAny separated by commas inside FindFastestServerAsync.

  • Task.WhenAny returns a Task<Task<string>>. The first await gives you the winning Task<string>.

  • You need a second await to get the actual string value from the winning task, which you then return.

Exercise: Fastest Server Matchmaking

Find the fastest responding game server using asynchronous task combinators.

Problem statement

In multiplayer gaming, a client must often ping multiple regional matchmaking servers simultaneously to determine which one offers the lowest latency. You need to implement a utility that simulates pinging three servers and automatically selects the one that responds first, allowing the player to connect without waiting for the slower servers.

Task requirements

  • Start the simulated ping process for three regions: “US-East”, “Europe”, and “Asia”.

  • Determine which server responds first.

  • Extract and return the name of the winning server from the utility class.

Constraints

  • You must use Task.WhenAny inside the Matchmaker class to race the three tasks against each other.

  • You must await the winning task to extract its string result.

  • Do not block the thread using .Result or .Wait().

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Pass the three tasks into Task.WhenAny separated by commas inside FindFastestServerAsync.

  • Task.WhenAny returns a Task<Task<string>>. The first await gives you the winning Task<string>.

  • You need a second await to get the actual string value from the winning task, which you then return.

C# 14.0
using System.Threading.Tasks;
namespace Multiplayer;
public class Matchmaker
{
public async Task<string> FindFastestServerAsync()
{
Task<string> usTask = PingServerAsync("US-East", 700);
Task<string> euTask = PingServerAsync("Europe", 500);
Task<string> asiaTask = PingServerAsync("Asia", 550);
// TODO: Use Task.WhenAny to wait for the fastest server to respond
// TODO: Await the winner to get the server name
// TODO: Return the winning server name
return string.Empty;
}
private async Task<string> PingServerAsync(string region, int delayInMilliseconds)
{
await Task.Delay(delayInMilliseconds);
return region;
}
}