🔨 modification server
continuous-integration/drone/push Build is failing Details

pull/7/head^2
Bruno DA COSTA CUNHA 2 years ago
parent 997a626a7c
commit 7c6d340c1a

@ -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<IPEndPoint, UdpClient> clients = new Dictionary<IPEndPoint, UdpClient>();
Dictionary<string, Room> rooms = new Dictionary<string, Room>();
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<Player> data = JsonSerializer.Deserialize<ObjectTransfert<Player>>(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);
}
}
}

@ -17,65 +17,9 @@ class Program
static void Main(string[] args)
{
PongServer server = new PongServer();
Console.WriteLine("Welcome to LeapHit Multiplayer - Server");
StartServer();
server.StartServer();
}
static void StartServer()
{
Room room = new Room();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 3131);
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<Player> data = JsonSerializer.Deserialize<ObjectTransfert<Player>>(fileJson);
room.MaxPlayers.Add(data.Data);
if (data.Informations.Action == Shared.DTO.Action.Connect)
{
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);
}
}
}

@ -9,7 +9,18 @@ namespace Server
{
public class Room
{
public ICollection<Player> MaxPlayers { get; set; }
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; }
}
}

Loading…
Cancel
Save