Test de communication de paquets entre un client et un serveur
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Server.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using UnityEngine;
  7. public class Server
  8. {
  9. public static int MaxPlayers { get; private set; }
  10. public static int Port { get; private set; }
  11. public static Dictionary<int, Client> clients = new Dictionary<int, Client>();
  12. public delegate void PacketHandler(int _fromClient, Packet _packet);
  13. public static Dictionary<int, PacketHandler> packetHandlers;
  14. public static TcpListener tcpListener;
  15. public static UdpClient udpListener;
  16. public static void Start(int _maxPlayers, int _port)
  17. {
  18. MaxPlayers = _maxPlayers;
  19. Port = _port;
  20. Debug.Log("Starting Server...");
  21. InitializeServerData();
  22. tcpListener = new TcpListener(IPAddress.Any, Port);
  23. tcpListener.Start();
  24. tcpListener.BeginAcceptTcpClient(TCPConnectCallback, null);
  25. udpListener = new UdpClient(Port);
  26. udpListener.BeginReceive(UDPReceiveCallback, null);
  27. Debug.Log($"Server started on port : {Port}.");
  28. }
  29. private static void TCPConnectCallback(IAsyncResult _result)
  30. {
  31. TcpClient _client = tcpListener.EndAcceptTcpClient(_result);
  32. tcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectCallback), null);
  33. Debug.Log($"Incoming connection from {_client.Client.RemoteEndPoint}...");
  34. for (int i = 1; i <= MaxPlayers; i++)
  35. {
  36. if (clients[i].tcp.socket == null)
  37. {
  38. clients[i].tcp.Connect(_client);
  39. return;
  40. }
  41. }
  42. Debug.Log($"{_client.Client.RemoteEndPoint} failed to connect. server full !");
  43. }
  44. private static void UDPReceiveCallback(IAsyncResult _result)
  45. {
  46. try
  47. {
  48. IPEndPoint _clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
  49. byte[] _data = udpListener.EndReceive(_result, ref _clientEndPoint);
  50. udpListener.BeginReceive(UDPReceiveCallback, null);
  51. if (_data.Length < 4)
  52. {
  53. return;
  54. }
  55. using (Packet _packet = new Packet(_data))
  56. {
  57. int _clientId = _packet.ReadInt();
  58. if (_clientId == 0)
  59. {
  60. return;
  61. }
  62. if (clients[_clientId].udp.endPoint == null)
  63. {
  64. clients[_clientId].udp.Connect(_clientEndPoint);
  65. return;
  66. }
  67. if (clients[_clientId].udp.endPoint.ToString() == _clientEndPoint.ToString())
  68. {
  69. clients[_clientId].udp.HandleData(_packet);
  70. }
  71. }
  72. }
  73. catch (Exception _ex)
  74. {
  75. Debug.Log($"Error receiving UDP data: {_ex}");
  76. }
  77. }
  78. public static void SendUDPData(IPEndPoint _clientEndPoint, Packet _packet)
  79. {
  80. try
  81. {
  82. if (_clientEndPoint != null)
  83. {
  84. udpListener.BeginSend(_packet.ToArray(), _packet.Length(), _clientEndPoint, null, null);
  85. }
  86. }
  87. catch (Exception _ex)
  88. {
  89. Debug.Log($"Error sending data to {_clientEndPoint} via UDP: {_ex}");
  90. }
  91. }
  92. private static void InitializeServerData()
  93. {
  94. for (int i = 1; i <= MaxPlayers; i++)
  95. {
  96. clients.Add(i, new Client(i));
  97. }
  98. packetHandlers = new Dictionary<int, PacketHandler>()
  99. {
  100. { (int)ClientPackets.welcomeReceived, ServerHandle.WelcomeReceived },
  101. { (int)ClientPackets.playerMovement, ServerHandle.PlayerMovement },
  102. { (int)ClientPackets.playerShoot, ServerHandle.PlayerShoot }
  103. };
  104. Debug.Log("Initialized packets.");
  105. }
  106. public static void Stop()
  107. {
  108. tcpListener.Stop();
  109. udpListener.Close();
  110. }
  111. }