Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Mar 14, 2024
1 parent 32c1e59 commit b2fa8b3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion samples/SpaceWar.Lobby/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void LoadContent()
Services.AddService(new GameAssets(Content, GraphicsDevice));
Services.AddService(settings);
Services.AddService(SpriteBatch);
Services.AddService(new LobbyClient(settings));
Services.AddService(new LobbyHttpClient(settings));

SceneManager = new(this, startScene: new ChooseLobbyScene());
Services.AddService(SceneManager);
Expand Down
18 changes: 9 additions & 9 deletions samples/SpaceWar.Lobby/Scenes/LobbyScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace SpaceWar.Scenes;
public sealed class LobbyScene(PlayerMode mode) : Scene
{
LobbyState currentState = LobbyState.Loading;
LobbyClient client;
LobbyHttpClient client;
string errorMessage;
User user;
Lobby lobbyInfo;
Task networkCall;
bool ready;
bool connected;
UdpPuncher udpPuncher;
LobbyUdpClient lobbyUdpClient;

readonly TimeSpan refreshInterval = TimeSpan.FromSeconds(2);
readonly TimeSpan pingInterval = TimeSpan.FromMilliseconds(300);
Expand All @@ -25,9 +25,9 @@ public sealed class LobbyScene(PlayerMode mode) : Scene

public override void Initialize()
{
client = Services.GetService<LobbyClient>();
client = Services.GetService<LobbyHttpClient>();
networkCall = RequestLobby();
udpPuncher = new(Config.Port, Config.LobbyUrl, Config.LobbyPort);
lobbyUdpClient = new(Config.Port, Config.LobbyUrl, Config.LobbyPort);
keyboard.Update();

StartPingTimer();
Expand Down Expand Up @@ -234,7 +234,7 @@ async Task RefreshLobby()
{
lobbyInfo = await client.GetLobby(user);

await udpPuncher.HandShake(user.Token);
await lobbyUdpClient.HandShake(user);

if (connected) return;
connected = lobbyInfo.Players.SingleOrDefault(x => x.PeerId == user.PeerId) is
Expand All @@ -246,7 +246,7 @@ void CheckPlayersReady()
if (lobbyInfo?.Ready == false) return;

cts.Cancel();
udpPuncher.Stop();
lobbyUdpClient.Stop();

switch (mode)
{
Expand Down Expand Up @@ -330,8 +330,8 @@ public void StartPingTimer() => Task.Run(async () =>
while (await timer.WaitForNextTickAsync(cts.Token))
{
if (lobbyInfo is null || lobbyInfo.Ready) continue;
await udpPuncher.Ping(user, lobbyInfo.Players, cts.Token);
await udpPuncher.Ping(user, lobbyInfo.Spectators, cts.Token);
await lobbyUdpClient.Ping(user, lobbyInfo.Players, cts.Token);
await lobbyUdpClient.Ping(user, lobbyInfo.Spectators, cts.Token);
}
}
catch (OperationCanceledException)
Expand Down Expand Up @@ -371,7 +371,7 @@ protected override void Dispose(bool disposing)
try
{
cts.Dispose();
udpPuncher.Dispose();
lobbyUdpClient.Dispose();
if (user is not null && lobbyInfo is { Ready: false })
client.LeaveLobby(user).GetAwaiter().GetResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SpaceWar.Services;

public sealed class LobbyClient(AppSettings appSettings)
public sealed class LobbyHttpClient(AppSettings appSettings)
{
static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
{
Expand All @@ -33,10 +33,7 @@ public async Task<User> EnterLobby(string lobbyName, string username, PlayerMode
mode,
}, JsonOptions);

if (response.StatusCode is HttpStatusCode.Conflict)
throw new InvalidOperationException("Duplicated username");

if (response.StatusCode is HttpStatusCode.Conflict)
if (response.StatusCode is HttpStatusCode.UnprocessableEntity)
throw new InvalidOperationException("Already started");

response.EnsureSuccessStatusCode();
Expand Down
13 changes: 5 additions & 8 deletions samples/SpaceWar.Lobby/Services/UdpPuncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SpaceWar.Services;

public sealed class UdpPuncher : IDisposable
public sealed class LobbyUdpClient : IDisposable
{
readonly IPEndPoint remoteEndPoint;
readonly UdpSocket socket;
Expand All @@ -15,21 +15,18 @@ public sealed class UdpPuncher : IDisposable

bool disposed;

public UdpPuncher(int localPort, Uri serverUrl, int serverPort)
public LobbyUdpClient(int localPort, Uri serverUrl, int serverPort)
{
var serverAddress =
Dns.GetHostAddresses(serverUrl.DnsSafeHost, AddressFamily.InterNetwork).FirstOrDefault()
?? throw new InvalidOperationException($"Unable to get ip address from {serverUrl}");

var serverAddress = UdpSocket.GetDnsIpAddress(serverUrl.DnsSafeHost);
remoteEndPoint = new(serverAddress, serverPort);
socket = new(localPort);

Task.Run(() => Receive(cts.Token));
}

public async Task HandShake(Guid token, CancellationToken ct = default)
public async Task HandShake(User user, CancellationToken ct = default)
{
if (!token.TryFormat(buffer, out var bytesWritten) || bytesWritten is 0) return;
if (!user.Token.TryFormat(buffer, out var bytesWritten) || bytesWritten is 0) return;
await socket.SendToAsync(buffer.AsMemory()[..bytesWritten], remoteEndPoint, ct)
.ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Backdash/Network/Client/UdpSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public UdpSocket(int port, bool useIPv6 = false) : this(useIPv6 ? IPAddress.IPv6
public UdpSocket(string bindHost, int port, AddressFamily addressFamily = AddressFamily.InterNetwork)
: this(GetDnsIpAddress(bindHost, addressFamily), port) { }

static IPAddress GetDnsIpAddress(string host, AddressFamily addressFamily)
public static IPAddress GetDnsIpAddress(string host, AddressFamily addressFamily = AddressFamily.InterNetwork)
{
var address = Dns.GetHostAddresses(host, addressFamily).FirstOrDefault()
?? throw new BackdashException($"Unable to retrieve IP Address from host {host}");
Expand Down

0 comments on commit b2fa8b3

Please sign in to comment.