Test de communication de paquets entre un client et un serveur
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ServerSend.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ServerSend
  5. {
  6. private static void SendTCPData(int _toClient, Packet _packet)
  7. {
  8. _packet.WriteLength();
  9. Server.clients[_toClient].tcp.SendData(_packet);
  10. }
  11. private static void SendUDPData(int _toClient, Packet _packet)
  12. {
  13. _packet.WriteLength();
  14. Server.clients[_toClient].udp.SendData(_packet);
  15. }
  16. private static void SendTCPDataToAll(Packet _packet)
  17. {
  18. _packet.WriteLength();
  19. for (int i = 1; i <= Server.MaxPlayers; i++)
  20. {
  21. Server.clients[i].tcp.SendData(_packet);
  22. }
  23. }
  24. private static void SendTCPDataToAll(int _exceptClient, Packet _packet)
  25. {
  26. _packet.WriteLength();
  27. for (int i = 1; i <= Server.MaxPlayers; i++)
  28. {
  29. if (i != _exceptClient)
  30. {
  31. Server.clients[i].tcp.SendData(_packet);
  32. }
  33. }
  34. }
  35. private static void SendUDPDataToAll(Packet _packet)
  36. {
  37. _packet.WriteLength();
  38. for (int i = 1; i <= Server.MaxPlayers; i++)
  39. {
  40. Server.clients[i].udp.SendData(_packet);
  41. }
  42. }
  43. private static void SendUDPDataToAll(int _exceptClient, Packet _packet)
  44. {
  45. _packet.WriteLength();
  46. for (int i = 1; i <= Server.MaxPlayers; i++)
  47. {
  48. if (i != _exceptClient)
  49. {
  50. Server.clients[i].udp.SendData(_packet);
  51. }
  52. }
  53. }
  54. #region packets
  55. public static void Welcome(int _toClient, string _msg)
  56. {
  57. using (Packet _packet = new Packet((int)ServerPackets.welcome))
  58. {
  59. _packet.Write(_msg);
  60. _packet.Write(_toClient);
  61. SendTCPData(_toClient, _packet);
  62. }
  63. }
  64. public static void SpawnPlayer(int _toClient, Player _player)
  65. {
  66. using (Packet _packet = new Packet((int)ServerPackets.spawnPlayer))
  67. {
  68. _packet.Write(_player.id);
  69. _packet.Write(_player.username);
  70. _packet.Write(_player.transform.position);
  71. _packet.Write(_player.transform.rotation);
  72. SendTCPData(_toClient, _packet);
  73. }
  74. }
  75. public static void PlayerPosition(Player _player)
  76. {
  77. using (Packet _packet = new Packet((int)ServerPackets.playerPosition))
  78. {
  79. _packet.Write(_player.id);
  80. _packet.Write(_player.transform.position);
  81. SendUDPDataToAll(_packet);
  82. }
  83. }
  84. public static void PlayerRotation(Player _player)
  85. {
  86. using (Packet _packet = new Packet((int)ServerPackets.playerRotation))
  87. {
  88. _packet.Write(_player.id);
  89. _packet.Write(_player.transform.rotation);
  90. SendUDPDataToAll(_player.id, _packet);
  91. }
  92. }
  93. public static void PlayerDisconnected(int _playerId)
  94. {
  95. using (Packet _packet = new Packet((int)ServerPackets.playerDisconnected))
  96. {
  97. _packet.Write(_playerId);
  98. SendTCPDataToAll(_packet);
  99. }
  100. }
  101. public static void PlayerHealth(Player _player)
  102. {
  103. using (Packet _packet = new Packet((int)ServerPackets.playerHealth))
  104. {
  105. _packet.Write(_player.id);
  106. _packet.Write(_player.health);
  107. SendTCPDataToAll(_packet);
  108. }
  109. }
  110. public static void PlayerRespawned(Player _player)
  111. {
  112. using (Packet _packet = new Packet((int)ServerPackets.playerRespawned))
  113. {
  114. _packet.Write(_player.id);
  115. SendTCPDataToAll(_packet);
  116. }
  117. }
  118. #endregion
  119. }