From 4420b4e06196695513f775c9de865e81e1aeae68 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 12 Apr 2026 21:56:05 +0200 Subject: [PATCH] get udp example working --- backend/Backend.csproj | 3 --- backend/Dockerfile | 14 ++++++++++++++ backend/Program.cs | 44 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 backend/Dockerfile diff --git a/backend/Backend.csproj b/backend/Backend.csproj index 748c613..cc4ddb6 100644 --- a/backend/Backend.csproj +++ b/backend/Backend.csproj @@ -9,8 +9,5 @@ Backend - - - diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..985edbb --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,14 @@ +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build +WORKDIR /build +COPY Backend.csproj . +RUN dotnet restore +COPY . . +RUN dotnet build -c Release + +FROM mcr.microsoft.com/dotnet/runtime:10.0 +WORKDIR /app +COPY --from=build /build/bin/Release/net10.0 . +ENV ENET_HOST=0.0.0.0 +ENV ENET_PORT=7777 +EXPOSE 7777/udp +ENTRYPOINT ["./Backend"] diff --git a/backend/Program.cs b/backend/Program.cs index d9f4eb9..805a683 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -1,12 +1,52 @@ using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace Love2DBackend { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - Console.WriteLine("Hello from LÖVE2D Backend!"); + Console.WriteLine("LÖVE2D Backend - UDP Server"); + Console.WriteLine("Starting server on port 7777..."); + + using (var udpServer = new UdpClient(7777)) + { + Console.WriteLine("Server listening on 0.0.0.0:7777"); + + var cts = new CancellationTokenSource(); + + // Handle Ctrl+C gracefully + Console.CancelKeyPress += (s, e) => + { + e.Cancel = true; + cts.Cancel(); + }; + + try + { + while (!cts.Token.IsCancellationRequested) + { + var result = await udpServer.ReceiveAsync(cts.Token); + var message = Encoding.UTF8.GetString(result.Buffer); + var clientEndpoint = result.RemoteEndPoint; + + Console.WriteLine($"[{clientEndpoint}] {message}"); + + // Echo back + var response = Encoding.UTF8.GetBytes($"Echo: {message}"); + await udpServer.SendAsync(response, response.Length, clientEndpoint); + } + } + catch (OperationCanceledException) + { + Console.WriteLine("Server stopped."); + } + } } } }