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; using System.ComponentModel; 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.Host) { Host(data, remoteEndPoint, serverSocket, false); } if (data.Informations.Action == Shared.DTO.Action.Join) { var choisenRoom = rooms.FirstOrDefault(room => room.Key == data.Informations.IdRoom); if(choisenRoom.Value != default && choisenRoom.Value.Availaible) { Join(data, remoteEndPoint, serverSocket, choisenRoom.Value); } } if (data.Informations.Action == Shared.DTO.Action.Connect) // Join = rejoindre un room , et Host ça va juste créer un room { var choisenRoom = rooms.FirstOrDefault(room => room.Value.Availaible); Console.WriteLine("Connection " + choisenRoom.Key); if (choisenRoom.Value != default ) { Join(data, remoteEndPoint, serverSocket, choisenRoom.Value); } else { Host(data, remoteEndPoint, serverSocket, true); } } } } private void Host(ObjectTransfert data, IPEndPoint remoteEndPoint, UdpClient serverSocket, bool availaible) { Room room = new Room(data.Data.playerId, availaible); // Assign a unique port to the client IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort); UdpClient clientSocket = new UdpClient(clientEndPoint); room.playerHost = new KeyValuePair(data.Data,clientSocket); room.NbPlayer++; Console.WriteLine("New connection Host From " + remoteEndPoint.ToString()); room.Port = nextPort; nextPort++; // Send port message to client byte[] connectionData = Encoding.ASCII.GetBytes(nextPort.ToString()); serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); rooms[data.Data.playerId] = room; room.PropertyChanged += room.OnReadyChanged; } private void Join(ObjectTransfert data, IPEndPoint remoteEndPoint, UdpClient serverSocket, Room room) { // Assign a unique port to the client IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, room.Port); UdpClient clientSocket = new UdpClient(clientEndPoint); room.playerJoin = new KeyValuePair(data.Data, clientSocket); Console.WriteLine("New connection Client from " + remoteEndPoint.ToString()); // Send port message to client byte[] connectionData = Encoding.ASCII.GetBytes(nextPort.ToString()); serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); room.PropertyChanged += room.OnReadyChanged; room.NbPlayer++; } }