Test de communication de paquets entre un client et un serveur
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Client.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Client
  8. {
  9. public static int dataBufferSize = 4096;
  10. public int id;
  11. public Player player;
  12. public TCP tcp;
  13. public UDP udp;
  14. public Client(int _clientId)
  15. {
  16. id = _clientId;
  17. tcp = new TCP(id);
  18. udp = new UDP(id);
  19. }
  20. public class TCP
  21. {
  22. public TcpClient socket;
  23. private readonly int id;
  24. private NetworkStream stream;
  25. private Packet receivedData;
  26. private byte[] receiveBuffer;
  27. public TCP(int _id)
  28. {
  29. id = _id;
  30. }
  31. public void Connect(TcpClient _socket)
  32. {
  33. socket = _socket;
  34. socket.ReceiveBufferSize = dataBufferSize;
  35. socket.SendBufferSize = dataBufferSize;
  36. stream = socket.GetStream();
  37. receivedData = new Packet();
  38. receiveBuffer = new byte[dataBufferSize];
  39. stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
  40. ServerSend.Welcome(id, "Welcome to the server");
  41. }
  42. public void SendData(Packet _packet)
  43. {
  44. try
  45. {
  46. if (socket != null)
  47. {
  48. stream.BeginWrite(_packet.ToArray(), 0, _packet.Length(), null, null);
  49. }
  50. }
  51. catch (Exception _ex)
  52. {
  53. Debug.Log($"Error sending data to the player {id} via TCP: {_ex}");
  54. }
  55. }
  56. private void ReceiveCallback(IAsyncResult _result)
  57. {
  58. try
  59. {
  60. int _byteLength = stream.EndRead(_result);
  61. if (_byteLength <= 0)
  62. {
  63. Server.clients[id].Disconnect();
  64. return;
  65. }
  66. byte[] _data = new byte[_byteLength];
  67. Array.Copy(receiveBuffer, _data, _byteLength);
  68. receivedData.Reset(HandleData(_data));
  69. stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
  70. }
  71. catch (Exception _ex)
  72. {
  73. Debug.Log($"Error receiving TCP data : {_ex}");
  74. // TODO diconnect
  75. }
  76. }
  77. private bool HandleData(byte[] _data)
  78. {
  79. int _packetLength = 0;
  80. receivedData.SetBytes(_data);
  81. if (receivedData.UnreadLength() >= 4)
  82. {
  83. _packetLength = receivedData.ReadInt();
  84. if (_packetLength <= 0)
  85. {
  86. return true;
  87. }
  88. }
  89. while (_packetLength > 0 && _packetLength <= receivedData.UnreadLength())
  90. {
  91. byte[] _packetBytes = receivedData.ReadBytes(_packetLength);
  92. ThreadManager.ExecuteOnMainThread(() =>
  93. {
  94. using (Packet _packet = new Packet(_packetBytes))
  95. {
  96. int _packetId = _packet.ReadInt();
  97. Server.packetHandlers[_packetId](id, _packet);
  98. }
  99. });
  100. _packetLength = 0;
  101. if (receivedData.UnreadLength() >= 4)
  102. {
  103. _packetLength = receivedData.ReadInt();
  104. if (_packetLength <= 0)
  105. {
  106. return true;
  107. }
  108. }
  109. }
  110. if (_packetLength <= 1)
  111. {
  112. return true;
  113. }
  114. return false;
  115. }
  116. public void Disconnect()
  117. {
  118. socket.Close();
  119. stream = null;
  120. receivedData = null;
  121. receiveBuffer = null;
  122. socket = null;
  123. }
  124. }
  125. public class UDP
  126. {
  127. public IPEndPoint endPoint;
  128. private int id;
  129. public UDP(int _id)
  130. {
  131. id = _id;
  132. }
  133. public void Connect(IPEndPoint _endPoint)
  134. {
  135. endPoint = _endPoint;
  136. }
  137. public void SendData(Packet _packet)
  138. {
  139. Server.SendUDPData(endPoint, _packet);
  140. }
  141. public void HandleData(Packet _packetData)
  142. {
  143. int _packetLength = _packetData.ReadInt();
  144. byte[] _packetBytes = _packetData.ReadBytes(_packetLength);
  145. ThreadManager.ExecuteOnMainThread(() =>
  146. {
  147. using (Packet _packet = new Packet(_packetBytes))
  148. {
  149. int _packetId = _packet.ReadInt();
  150. Server.packetHandlers[_packetId](id, _packet);
  151. }
  152. });
  153. }
  154. public void Disconnect()
  155. {
  156. endPoint = null;
  157. }
  158. }
  159. public void SendIntoGame(string _playerName)
  160. {
  161. player = NetworkManager.instance.InstantiatePlayer();
  162. player.Initialize(id, _playerName);
  163. foreach (Client _client in Server.clients.Values)
  164. {
  165. if (_client.player != null)
  166. {
  167. if (_client.id != id)
  168. {
  169. ServerSend.SpawnPlayer(id, _client.player);
  170. }
  171. }
  172. }
  173. foreach (Client _client in Server.clients.Values)
  174. {
  175. if (_client.player != null)
  176. {
  177. ServerSend.SpawnPlayer(_client.id, player);
  178. }
  179. }
  180. }
  181. public void Disconnect()
  182. {
  183. Debug.Log($"{tcp.socket.Client.RemoteEndPoint} has disconnected.");
  184. ThreadManager.ExecuteOnMainThread(() =>
  185. {
  186. UnityEngine.Object.Destroy(player.gameObject);
  187. player = null;
  188. });
  189. tcp.Disconnect();
  190. udp.Disconnect();
  191. ServerSend.PlayerDisconnected(id);
  192. }
  193. }