|
|
|
@ -8,6 +8,7 @@ using DataBase.Entity;
|
|
|
|
|
using Shared.DTO;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Server;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
|
|
public class PongServer
|
|
|
|
|
{
|
|
|
|
@ -38,57 +39,108 @@ public class PongServer
|
|
|
|
|
string fileJson = Encoding.UTF8.GetString(receivedData);
|
|
|
|
|
ObjectTransfert<Player> data = JsonSerializer.Deserialize<ObjectTransfert<Player>>(fileJson);
|
|
|
|
|
|
|
|
|
|
if (data.Informations.Action == Shared.DTO.Action.Create)
|
|
|
|
|
if (data.Informations.Action == Shared.DTO.Action.Host)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
byte[] connectionData = Encoding.ASCII.GetBytes(room.Id);
|
|
|
|
|
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
|
|
|
|
|
|
|
|
|
|
// Start thread to receive data from client
|
|
|
|
|
Thread receiveThread = new Thread(() => Room.ReceiveMessages(clientSocket, data.Data));
|
|
|
|
|
receiveThread.Start();
|
|
|
|
|
Host(data, remoteEndPoint, serverSocket, false);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.Informations.Action == Shared.DTO.Action.Join)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
|
|
|
|
|
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);
|
|
|
|
|
if (choisenRoom.Value != default )
|
|
|
|
|
{
|
|
|
|
|
Join(data, remoteEndPoint, serverSocket, choisenRoom.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Host(data, remoteEndPoint, serverSocket, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void Host(ObjectTransfert<Player> data, IPEndPoint remoteEndPoint, UdpClient serverSocket, bool availaible)
|
|
|
|
|
{
|
|
|
|
|
Room room = new Room(data.Data.playerId, availaible);
|
|
|
|
|
room.playerHost = data.Data;
|
|
|
|
|
room.nbPlayer++;
|
|
|
|
|
|
|
|
|
|
room.PropertyChanged += OnReadyChanged;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
byte[] connectionData = Encoding.ASCII.GetBytes(room.Id);
|
|
|
|
|
serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint);
|
|
|
|
|
|
|
|
|
|
// Start thread to receive data from client
|
|
|
|
|
Thread receiveThread = new Thread(() => room.ReceiveMessages(clientSocket, data.Data));
|
|
|
|
|
receiveThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Join(ObjectTransfert<Player> data, IPEndPoint remoteEndPoint, UdpClient serverSocket, Room room)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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(() => room.ReceiveMessages(clientSocket, data.Data));
|
|
|
|
|
receiveThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnReadyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Room nbPlayer = sender as Room;
|
|
|
|
|
bool maxPlayer = nbPlayer.maxPlayer;
|
|
|
|
|
|
|
|
|
|
if (room.MaxPlayers.Count == 2)
|
|
|
|
|
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
|
|
|
|
|
UdpClient serverSocket = new UdpClient(serverEndPoint);
|
|
|
|
|
|
|
|
|
|
if (maxPlayer)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Starting game...");
|
|
|
|
|
// Call a function to start the game
|
|
|
|
|
//Faut finir ça mnt
|
|
|
|
|
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
byte[] receivedData = serverSocket.Receive(ref remoteEndPoint);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|