NetMsgController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Unity.Networking.Transport;
  6. using KairoEngine.Core;
  7. using Sirenix.OdinInspector;
  8. namespace KairoEngine.Multiplayer
  9. {
  10. [HideMonoScript]
  11. public class NetMsgController : MonoBehaviour
  12. {
  13. private static List<NetMsg> netMessageTypes = new List<NetMsg>();
  14. private void Start()
  15. {
  16. GetNetMessageTypes();
  17. }
  18. private void GetNetMessageTypes()
  19. {
  20. netMessageTypes = ReflectiveEnumerator.GetEnumerableOfType<NetMsg>()
  21. .Where(x => x.code != 0)
  22. .Select(x => { return x; })
  23. .ToList();
  24. }
  25. public NetMsg FindWithCode(uint code)
  26. {
  27. for (int i = 0; i < netMessageTypes.Count; i++)
  28. {
  29. if(netMessageTypes[i].code == code) return netMessageTypes[i];
  30. }
  31. return null;
  32. }
  33. public void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId)
  34. {
  35. uint code = stream.ReadByte();
  36. NetMsg msg = FindWithCode(code).ReceiveMessage(server, ref stream, clientId);
  37. msg.ReadMessage(server, clientId);
  38. }
  39. public void ReceiveData(ref DataStreamReader stream, ClientBehaviour client)
  40. {
  41. uint code = stream.ReadByte();
  42. NetMsg msg = FindWithCode(code).ReceiveMessage(client, ref stream);
  43. msg.ReadMessage(client);
  44. }
  45. public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId)
  46. {
  47. FindWithCode(code).SendMessage(server, netMsg, ref writer, clientId);
  48. }
  49. public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client)
  50. {
  51. FindWithCode(code).SendMessage(client, netMsg, ref writer);
  52. }
  53. // Remove rest of the code below
  54. /*
  55. public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, int clientId = -1)
  56. => Deserialize(code, ref stream, server, null, clientId);
  57. public static void Deserialize(uint code, ref DataStreamReader stream, ClientBehaviour client, int clientId = -1)
  58. => Deserialize(code, ref stream, null, client, clientId);
  59. public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, ClientBehaviour client, int clientId)
  60. {
  61. NetOpCode opCode = (NetOpCode)code;
  62. switch (opCode)
  63. {
  64. case NetOpCode.Disconnect:
  65. NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
  66. netDisconnectMsg.Deserialize(ref stream);
  67. if(client != null)
  68. {
  69. string text = $"Server disconnected client";
  70. if(client.debug) Debug.Log(text);
  71. NetMsgEvents.Trigger($"{client.eventStreamName}_ServerDisconnect", text, -1, code, netDisconnectMsg);
  72. }
  73. if(server != null)
  74. {
  75. string text = $"Client is disconnecting";
  76. if(server.debug) Debug.Log(text);
  77. NetMsgEvents.Trigger($"{server.eventStreamName}_ClientDisconnect", text, clientId, code, netDisconnectMsg);
  78. }
  79. break;
  80. case NetOpCode.Handshake:
  81. NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
  82. netHandshakeMsg.Deserialize(ref stream);
  83. if(client != null)
  84. {
  85. string text = $"Server accepted handshake (playerName = {netHandshakeMsg.playerName})";
  86. if(client.debug) Debug.Log(text);
  87. NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, netHandshakeMsg);
  88. }
  89. if(server != null)
  90. {
  91. string text = $"Received handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
  92. if(server.debug) Debug.Log(text);
  93. NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, netHandshakeMsg);
  94. }
  95. break;
  96. case NetOpCode.ClientConnected:
  97. NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg();
  98. netClientConnectedMsg.Deserialize(ref stream);
  99. if(client != null)
  100. {
  101. string text = $"New peer connected to server (playerName = {netClientConnectedMsg.playerName})";
  102. if(client.debug) Debug.Log(text);
  103. NetMsgEvents.Trigger($"{client.eventStreamName}_PeerConnected", text, -1, code, netClientConnectedMsg);
  104. }
  105. break;
  106. case NetOpCode.ClientDisconnected:
  107. NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
  108. netClientDisconnectedMsg.Deserialize(ref stream);
  109. if(client != null)
  110. {
  111. string text = $"Peer disconnected to server (playerName = {netClientDisconnectedMsg.playerName})";
  112. if(client.debug) Debug.Log(text);
  113. NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, netClientDisconnectedMsg);
  114. }
  115. break;
  116. default:
  117. NetMsg netMsg = new NetMsg();
  118. netMsg.Deserialize(ref stream);
  119. string error = $"Received data with invalid NetOpCode {code}";
  120. if(client != null)
  121. {
  122. if(client.debug) Debug.Log(error);
  123. NetMsgEvents.Trigger($"{client.eventStreamName}_InvalidOpCode", error, -1, code, netMsg);
  124. }
  125. if(server != null)
  126. {
  127. if(server.debug) Debug.Log(error);
  128. NetMsgEvents.Trigger($"{server.eventStreamName}_InvalidOpCode", error, clientId, code, netMsg);
  129. }
  130. break;
  131. }
  132. }
  133. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId = -1)
  134. => Serialize(code, netMsg, ref writer, server, null, clientId);
  135. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client, int clientId = -1 )
  136. => Serialize(code, netMsg, ref writer, null, client, clientId);
  137. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, ClientBehaviour client, int clientId)
  138. {
  139. NetOpCode opCode = (NetOpCode)code;
  140. switch (opCode)
  141. {
  142. case NetOpCode.Disconnect:
  143. NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg;
  144. if(client != null)
  145. {
  146. string text = $"Sending disconnect message to server";
  147. if(client.debug) Debug.Log(text);
  148. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingDisconnectMessageToServer", text, clientId, code, netDisconnectMsg);
  149. }
  150. if(server != null)
  151. {
  152. string text = $"Sending disconnect message to client (ID {clientId})";
  153. if(server.debug) Debug.Log(text);
  154. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingDisconnectMessageToClient", text, clientId, code, netDisconnectMsg);
  155. }
  156. netDisconnectMsg.Serialize(ref writer);
  157. break;
  158. case NetOpCode.Handshake:
  159. NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
  160. if(client != null)
  161. {
  162. string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";
  163. if(client.debug) Debug.Log(text);
  164. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, clientId, code, netHandshakeMsg);
  165. }
  166. if(server != null)
  167. {
  168. string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
  169. if(server.debug) Debug.Log(text);
  170. NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
  171. }
  172. netHandshakeMsg.Serialize(ref writer);
  173. break;
  174. case NetOpCode.ClientConnected:
  175. NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg;
  176. if(server != null)
  177. {
  178. string text = $"Alerting clients of new connection (playerName = {netClientConnectedMsg.playerName})";
  179. if(server.debug) Debug.Log(text);
  180. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientConnectedMsg", text, clientId, code, netClientConnectedMsg);
  181. }
  182. netClientConnectedMsg.Serialize(ref writer);
  183. break;
  184. case NetOpCode.ClientDisconnected:
  185. NetClientDisconnectedMsg netClientDisconnectedMsg = (NetClientDisconnectedMsg)netMsg;
  186. if(server != null)
  187. {
  188. string text = $"Alerting clients of disconnection (playerName = {netClientDisconnectedMsg.playerName})";
  189. if(server.debug) Debug.Log(text);
  190. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientDisconnectedMsg", text, clientId, code, netClientDisconnectedMsg);
  191. }
  192. netClientDisconnectedMsg.Serialize(ref writer);
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. */
  199. }
  200. }