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) { 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 ) { Thread join = new Thread(() => Join(data, remoteEndPoint, serverSocket, choisenRoom.Value)); join.Start(); } else { Thread host = new Thread(() => Host(data, remoteEndPoint, serverSocket, true)); host.Start(); } } } } 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++; Tuple dataToSend = new Tuple(room.Port, true); Console.WriteLine(JsonSerializer.Serialize(dataToSend)); // Send port message to client byte[] connectionData = Encoding.ASCII.GetBytes(JsonSerializer.Serialize(dataToSend)); serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); rooms[data.Data.playerId] = room; room.PropertyChanged += room.OnReadyChanged; Console.WriteLine("FIN HOST..............."); } private void Join(ObjectTransfert data, IPEndPoint remoteEndPoint, UdpClient serverSocket, Room room) { room.playerJoin = new KeyValuePair(data.Data, room.playerHost.Value); Console.WriteLine("New connection Client from " + remoteEndPoint.ToString()); Tuple dataToSend = new Tuple(room.Port, false); Console.WriteLine(JsonSerializer.Serialize(dataToSend)); // Send port message to client byte[] connectionData = Encoding.ASCII.GetBytes(JsonSerializer.Serialize(dataToSend)); serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); room.PropertyChanged += room.OnReadyChanged; room.NbPlayer++; Console.WriteLine("FIN JOIN..............."); } }