ServerNetMsgController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. for (int i = 0; i < sendMsgBuffer.Count; i++)
  72. {
  73. if(sendMsgBuffer[i].Count == 0) continue;
  74. DataStreamWriter writer = new DataStreamWriter();
  75. server.m_Driver.BeginSend(server.m_Connections[i], out writer);
  76. writer.WriteUInt((uint)networkTick.tick); // Current Tick Header
  77. writer.WriteByte((byte)sendMsgBuffer[i].Count); // Message count Header
  78. for (int a = 0; a < sendMsgBuffer[i].Count; a++)
  79. {
  80. NetMsg netMsg = sendMsgBuffer[i][a];
  81. FindWithCode(netMsg.code).SendMessage(server, netMsg, ref writer, i);
  82. }
  83. server.m_Driver.EndSend(writer);
  84. sendMsgBuffer[i].Clear();
  85. }
  86. }
  87. private void ReadBufferedData()
  88. {
  89. for (int i = 0; i < receivedMsgBuffer.Count; i++)
  90. {
  91. for (int a = 0; a < receivedMsgBuffer[i].Count; a++)
  92. {
  93. NetMsg msg = receivedMsgBuffer[i][a];
  94. msg.ReadMessage(server, i);
  95. }
  96. receivedMsgBuffer[i].Clear();
  97. }
  98. }
  99. }
  100. }