NetMsgController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. protected void GetNetMessageTypes()
  19. {
  20. netMessageTypes = ReflectiveEnumerator.GetEnumerableOfType<NetMsg>()
  21. .Where(x => x.code != 0)
  22. .Select(x => { return x; })
  23. .ToList();
  24. }
  25. public virtual 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 virtual 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 virtual 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 virtual void SendData(uint code, NetMsg netMsg, ServerBehaviour server, int clientId)
  46. {
  47. DataStreamWriter writer = new DataStreamWriter();
  48. server.m_Driver.BeginSend(server.m_Connections[clientId], out writer);
  49. FindWithCode(code).SendMessage(server, netMsg, ref writer, clientId);
  50. server.m_Driver.EndSend(writer);
  51. }
  52. public virtual void SendData(uint code, NetMsg netMsg, ClientBehaviour client)
  53. {
  54. DataStreamWriter writer = new DataStreamWriter();
  55. client.m_Driver.BeginSend(client.m_Connection, out writer);
  56. FindWithCode(code).SendMessage(client, netMsg, ref writer);
  57. client.m_Driver.EndSend(writer);
  58. }
  59. // Remove rest of the code below
  60. /*
  61. public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, int clientId = -1)
  62. => Deserialize(code, ref stream, server, null, clientId);
  63. public static void Deserialize(uint code, ref DataStreamReader stream, ClientBehaviour client, int clientId = -1)
  64. => Deserialize(code, ref stream, null, client, clientId);
  65. public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, ClientBehaviour client, int clientId)
  66. {
  67. NetOpCode opCode = (NetOpCode)code;
  68. switch (opCode)
  69. {
  70. case NetOpCode.Disconnect:
  71. NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
  72. netDisconnectMsg.Deserialize(ref stream);
  73. if(client != null)
  74. {
  75. string text = $"Server disconnected client";
  76. if(client.debug) Debug.Log(text);
  77. NetMsgEvents.Trigger($"{client.eventStreamName}_ServerDisconnect", text, -1, code, netDisconnectMsg);
  78. }
  79. if(server != null)
  80. {
  81. string text = $"Client is disconnecting";
  82. if(server.debug) Debug.Log(text);
  83. NetMsgEvents.Trigger($"{server.eventStreamName}_ClientDisconnect", text, clientId, code, netDisconnectMsg);
  84. }
  85. break;
  86. case NetOpCode.Handshake:
  87. NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
  88. netHandshakeMsg.Deserialize(ref stream);
  89. if(client != null)
  90. {
  91. string text = $"Server accepted handshake (playerName = {netHandshakeMsg.playerName})";
  92. if(client.debug) Debug.Log(text);
  93. NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, netHandshakeMsg);
  94. }
  95. if(server != null)
  96. {
  97. string text = $"Received handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
  98. if(server.debug) Debug.Log(text);
  99. NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, netHandshakeMsg);
  100. }
  101. break;
  102. case NetOpCode.ClientConnected:
  103. NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg();
  104. netClientConnectedMsg.Deserialize(ref stream);
  105. if(client != null)
  106. {
  107. string text = $"New peer connected to server (playerName = {netClientConnectedMsg.playerName})";
  108. if(client.debug) Debug.Log(text);
  109. NetMsgEvents.Trigger($"{client.eventStreamName}_PeerConnected", text, -1, code, netClientConnectedMsg);
  110. }
  111. break;
  112. case NetOpCode.ClientDisconnected:
  113. NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
  114. netClientDisconnectedMsg.Deserialize(ref stream);
  115. if(client != null)
  116. {
  117. string text = $"Peer disconnected to server (playerName = {netClientDisconnectedMsg.playerName})";
  118. if(client.debug) Debug.Log(text);
  119. NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, netClientDisconnectedMsg);
  120. }
  121. break;
  122. default:
  123. NetMsg netMsg = new NetMsg();
  124. netMsg.Deserialize(ref stream);
  125. string error = $"Received data with invalid NetOpCode {code}";
  126. if(client != null)
  127. {
  128. if(client.debug) Debug.Log(error);
  129. NetMsgEvents.Trigger($"{client.eventStreamName}_InvalidOpCode", error, -1, code, netMsg);
  130. }
  131. if(server != null)
  132. {
  133. if(server.debug) Debug.Log(error);
  134. NetMsgEvents.Trigger($"{server.eventStreamName}_InvalidOpCode", error, clientId, code, netMsg);
  135. }
  136. break;
  137. }
  138. }
  139. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId = -1)
  140. => Serialize(code, netMsg, ref writer, server, null, clientId);
  141. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client, int clientId = -1 )
  142. => Serialize(code, netMsg, ref writer, null, client, clientId);
  143. public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, ClientBehaviour client, int clientId)
  144. {
  145. NetOpCode opCode = (NetOpCode)code;
  146. switch (opCode)
  147. {
  148. case NetOpCode.Disconnect:
  149. NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg;
  150. if(client != null)
  151. {
  152. string text = $"Sending disconnect message to server";
  153. if(client.debug) Debug.Log(text);
  154. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingDisconnectMessageToServer", text, clientId, code, netDisconnectMsg);
  155. }
  156. if(server != null)
  157. {
  158. string text = $"Sending disconnect message to client (ID {clientId})";
  159. if(server.debug) Debug.Log(text);
  160. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingDisconnectMessageToClient", text, clientId, code, netDisconnectMsg);
  161. }
  162. netDisconnectMsg.Serialize(ref writer);
  163. break;
  164. case NetOpCode.Handshake:
  165. NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
  166. if(client != null)
  167. {
  168. string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";
  169. if(client.debug) Debug.Log(text);
  170. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, clientId, code, netHandshakeMsg);
  171. }
  172. if(server != null)
  173. {
  174. string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
  175. if(server.debug) Debug.Log(text);
  176. NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
  177. }
  178. netHandshakeMsg.Serialize(ref writer);
  179. break;
  180. case NetOpCode.ClientConnected:
  181. NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg;
  182. if(server != null)
  183. {
  184. string text = $"Alerting clients of new connection (playerName = {netClientConnectedMsg.playerName})";
  185. if(server.debug) Debug.Log(text);
  186. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientConnectedMsg", text, clientId, code, netClientConnectedMsg);
  187. }
  188. netClientConnectedMsg.Serialize(ref writer);
  189. break;
  190. case NetOpCode.ClientDisconnected:
  191. NetClientDisconnectedMsg netClientDisconnectedMsg = (NetClientDisconnectedMsg)netMsg;
  192. if(server != null)
  193. {
  194. string text = $"Alerting clients of disconnection (playerName = {netClientDisconnectedMsg.playerName})";
  195. if(server.debug) Debug.Log(text);
  196. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientDisconnectedMsg", text, clientId, code, netClientDisconnectedMsg);
  197. }
  198. netClientDisconnectedMsg.Serialize(ref writer);
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. */
  205. }
  206. }