diff --git a/code/server/ApiLeapHit/Program.cs b/code/server/ApiLeapHit/Program.cs index 8df64de..958f98a 100644 --- a/code/server/ApiLeapHit/Program.cs +++ b/code/server/ApiLeapHit/Program.cs @@ -8,7 +8,7 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); //builder.Services.AddSingleton(); // Add logging diff --git a/code/server/ClientConsole/Program.cs b/code/server/ClientConsole/Program.cs index dc744ca..48401b8 100644 --- a/code/server/ClientConsole/Program.cs +++ b/code/server/ClientConsole/Program.cs @@ -13,7 +13,7 @@ class Program static void StartClient() { - IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3131); + IPEndPoint serverEndPoint = new IPEndPoint(Dns.GetHostAddresses("hulivet.fr").FirstOrDefault(), 3131); UdpClient client = new UdpClient(); // Send connection message to server @@ -31,7 +31,7 @@ class Program string message = ""; while (message != "exit") { - serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(receivedPort)); + serverEndPoint = new IPEndPoint(Dns.GetHostAddresses("hulivet.fr").FirstOrDefault(), int.Parse(receivedPort)); Console.Write("Enter message to send (or 'exit' to quit): "); message = Console.ReadLine(); diff --git a/code/server/Dockerfile b/code/server/Dockerfile index ab1f7af..1d936f4 100644 --- a/code/server/Dockerfile +++ b/code/server/Dockerfile @@ -1,5 +1,3 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. - FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app EXPOSE 80 @@ -14,6 +12,7 @@ RUN dotnet restore "ApiLeapHit/ApiLeapHit.csproj" COPY . . WORKDIR "/src/ApiLeapHit" RUN dotnet build "ApiLeapHit.csproj" -c Release -o /app/build +RUN rm -rf /root/.nuget/packages/* FROM build AS publish RUN dotnet publish "ApiLeapHit.csproj" -c Release -o /app/publish diff --git a/code/server/Server.sln b/code/server/Server.sln index d97c22f..17bf734 100644 --- a/code/server/Server.sln +++ b/code/server/Server.sln @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{12A97D16-3 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientConsole", "ClientConsole\ClientConsole.csproj", "{33F7C629-F92B-459C-BCC5-6A4601D6D9EA}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{CA1465F2-A006-4AF6-8231-59DB00963BD0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -47,6 +49,10 @@ Global {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Debug|Any CPU.Build.0 = Debug|Any CPU {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Release|Any CPU.ActiveCfg = Release|Any CPU {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Release|Any CPU.Build.0 = Release|Any CPU + {CA1465F2-A006-4AF6-8231-59DB00963BD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CA1465F2-A006-4AF6-8231-59DB00963BD0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CA1465F2-A006-4AF6-8231-59DB00963BD0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CA1465F2-A006-4AF6-8231-59DB00963BD0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/code/server/Server/PongServer.cs b/code/server/Server/PongServer.cs new file mode 100644 index 0000000..740d9ec --- /dev/null +++ b/code/server/Server/PongServer.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using DataBase.Entity; +using Shared.DTO; +using System.Text.Json; +using Server; + +public class PongServer +{ + //sert juste à stocker les connexions pour l'instant + static Dictionary clients = new Dictionary(); + Dictionary rooms = new Dictionary(); + + int nextPort; + int listenPort; + + public PongServer(int port = 3131) + { + nextPort = port + 1; + listenPort = port; + } + + public void StartServer() + { + + IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, listenPort); + UdpClient serverSocket = new UdpClient(serverEndPoint); + Console.WriteLine("Server started, waiting for clients to connect..."); + + while (true) + { + IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + byte[] receivedData = serverSocket.Receive(ref remoteEndPoint); + string fileJson = Encoding.UTF8.GetString(receivedData); + ObjectTransfert data = JsonSerializer.Deserialize>(fileJson); + + if (data.Informations.Action == Shared.DTO.Action.Create) + { + Room room = new Room(data.Data.playerID); + room.playerHost = data.Data; + room.nbPlayer++; + Console.WriteLine("New connection from " + remoteEndPoint.ToString()); + + // Assign a unique port to the client + IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort); + room.Port = nextPort; + nextPort++; + UdpClient clientSocket = new UdpClient(clientEndPoint); + clients[remoteEndPoint] = clientSocket; + + // Send connection message to client + string connectionMessage = room.ID; + byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage); + serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); + + // Start thread to receive data from client + Thread receiveThread = new Thread(() => ReceiveMessages(clientSocket)); + receiveThread.Start(); + + } + + if (data.Informations.Action == Shared.DTO.Action.Join) + { + Console.WriteLine("New connection from " + remoteEndPoint.ToString()); + + // Assign a unique port to the client + IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort++); + UdpClient clientSocket = new UdpClient(clientEndPoint); + clients[remoteEndPoint] = clientSocket; + + // Send connection message to client + string connectionMessage = clientEndPoint.Port.ToString(); + byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage); + serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); + + // Start thread to receive data from client + Thread receiveThread = new Thread(() => ReceiveMessages(clientSocket)); + receiveThread.Start(); + + } + + if (room.MaxPlayers.Count == 2) + { + Console.WriteLine("Starting game..."); + // Call a function to start the game + } + } + } + + static void ReceiveMessages(UdpClient clientSocket) + { + IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + + while (true) + { + byte[] receivedData = clientSocket.Receive(ref remoteEndPoint); + string receivedMessage = Encoding.ASCII.GetString(receivedData); + Console.WriteLine("Received from " + remoteEndPoint.ToString() + ": " + receivedMessage); + } + } +} \ No newline at end of file diff --git a/code/server/Server/Room.cs b/code/server/Server/Room.cs new file mode 100644 index 0000000..20f6832 --- /dev/null +++ b/code/server/Server/Room.cs @@ -0,0 +1,26 @@ +using DataBase.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Server +{ + public class Room + { + + public Room(int id) + { + ID = id; + } + + public int ID { get; set; } + + public Player playerHost; + public Player playerJoin; + + public int nbPlayer = 0; + public int Port { get; set; } + } +} diff --git a/code/server/Server/Server.csproj b/code/server/Server/Server.csproj index 4744ab2..cd65276 100644 --- a/code/server/Server/Server.csproj +++ b/code/server/Server/Server.csproj @@ -1,15 +1,15 @@ - - Exe - net6.0 - enable - enable - + + Exe + net6.0 + enable + enable + - - - - + + + + \ No newline at end of file