ServerNetMsgController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Networking.Transport;
  5. using KairoEngine.Core;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.Multiplayer
  8. {
  9. [HideMonoScript]
  10. public class ServerNetMsgController : NetMsgController
  11. {
  12. public ServerBehaviour server;
  13. public NetworkTick networkTick;
  14. private List<List<NetMsg>> sendMsgBuffer = new List<List<NetMsg>>();
  15. private List<List<NetMsg>> receivedMsgBuffer = new List<List<NetMsg>>();
  16. private void Start()
  17. {
  18. GetNetMessageTypes();
  19. networkTick.eventStreamName = server.eventStreamName;
  20. for (int i = 0; i < server.players; i++)
  21. {
  22. sendMsgBuffer.Add(new List<NetMsg>());
  23. receivedMsgBuffer.Add(new List<NetMsg>());
  24. }
  25. }
  26. private void OnEnable()
  27. {
  28. GenericEvents.StartListening($"{server.eventStreamName}_OnTick", OnTick);
  29. GenericEvents.StartListening($"{server.eventStreamName}_Listening", ServerStarted);
  30. }
  31. private void OnDisable()
  32. {
  33. GenericEvents.StopListening($"{server.eventStreamName}_OnTick", OnTick);
  34. GenericEvents.StopListening($"{server.eventStreamName}_Listening", ServerStarted);
  35. }
  36. private void ServerStarted(string text)
  37. {
  38. networkTick.StartTickSystem();
  39. }
  40. public override void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId)
  41. {
  42. uint tick = stream.ReadUInt();
  43. uint msgCount = stream.ReadByte();
  44. //Debug.Log($"Receiving {msgCount} messages from client {clientId}");
  45. for (int i = 0; i < msgCount; i++)
  46. {
  47. uint code = stream.ReadByte();
  48. NetMsg typeMsg = FindWithCode(code);
  49. if(typeMsg != null)
  50. {
  51. NetMsg msg = typeMsg.ReceiveMessage(server, ref stream, clientId);
  52. if(msg != null)
  53. {
  54. receivedMsgBuffer[clientId].Add(msg);
  55. }
  56. }
  57. else Debug.LogError($"Could not find NetMsg with code {code}");
  58. }
  59. }
  60. public override void SendData(uint code, NetMsg netMsg, ServerBehaviour server, int clientId)
  61. {
  62. sendMsgBuffer[clientId].Add(netMsg);
  63. }
  64. private void OnTick(int tick)
  65. {
  66. SendBufferedData();
  67. ReadBufferedData();
  68. }
  69. private void SendBufferedData()
  70. {
  71. if(!server.m_Driver.IsCreated) return;
  72. for (int i = 0; i < sendMsgBuffer.Count; i++)
  73. {
  74. if(sendMsgBuffer[i].Count == 0) continue;
  75. if(!server.m_Connections[i].IsCreated) continue;
  76. DataStreamWriter writer = new DataStreamWriter();
  77. server.m_Driver.BeginSend(server.m_Connections[i], out writer);
  78. writer.WriteUInt((uint)networkTick.tick); // Current Tick Header
  79. writer.WriteByte((byte)sendMsgBuffer[i].Count); // Message count Header
  80. for (int a = 0; a < sendMsgBuffer[i].Count; a++)
  81. {
  82. NetMsg netMsg = sendMsgBuffer[i][a];
  83. FindWithCode(netMsg.code).SendMessage(server, netMsg, ref writer, i);
  84. }
  85. server.m_Driver.EndSend(writer);
  86. sendMsgBuffer[i].Clear();
  87. }
  88. }
  89. private void ReadBufferedData()
  90. {
  91. for (int i = 0; i < receivedMsgBuffer.Count; i++)
  92. {
  93. for (int a = 0; a < receivedMsgBuffer[i].Count; a++)
  94. {
  95. NetMsg msg = receivedMsgBuffer[i][a];
  96. msg.ReadMessage(server, i);
  97. }
  98. receivedMsgBuffer[i].Clear();
  99. }
  100. }
  101. }
  102. }