ClientNetMsgController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ClientNetMsgController : NetMsgController
  11. {
  12. public ClientBehaviour client;
  13. public NetworkTick networkTick;
  14. private List<NetMsg> sendMsgBuffer = new List<NetMsg>();
  15. private List<NetMsg> receivedMsgBuffer = new List<NetMsg>();
  16. private void Start()
  17. {
  18. GetNetMessageTypes();
  19. networkTick.eventStreamName = client.eventStreamName;
  20. }
  21. private void OnEnable()
  22. {
  23. GenericEvents.StartListening($"{client.eventStreamName}_OnTick", OnTick);
  24. GenericEvents.StartListening($"{client.eventStreamName}_Connected", StartTickSystem);
  25. }
  26. private void OnDisable()
  27. {
  28. GenericEvents.StopListening($"{client.eventStreamName}_OnTick", OnTick);
  29. GenericEvents.StopListening($"{client.eventStreamName}_Connected", StartTickSystem);
  30. }
  31. private void StartTickSystem(string text) => networkTick.StartTickSystem();
  32. public override void ReceiveData(ref DataStreamReader stream, ClientBehaviour client)
  33. {
  34. uint tick = stream.ReadUInt();
  35. uint msgCount = stream.ReadByte();
  36. for (int i = 0; i < msgCount; i++)
  37. {
  38. uint code = stream.ReadByte();
  39. NetMsg msg = FindWithCode(code).ReceiveMessage(client, ref stream);
  40. receivedMsgBuffer.Add(msg);
  41. }
  42. }
  43. public override void SendData(uint code, NetMsg netMsg, ClientBehaviour client)
  44. {
  45. sendMsgBuffer.Add(netMsg);
  46. }
  47. private void OnTick(int tick)
  48. {
  49. SendBufferedData();
  50. ReadBufferedData();
  51. }
  52. private void SendBufferedData()
  53. {
  54. if(sendMsgBuffer.Count == 0) return;
  55. Debug.Log("Sending buffered data from client to server");
  56. DataStreamWriter writer = new DataStreamWriter();
  57. client.m_Driver.BeginSend(client.m_Connection, out writer);
  58. writer.WriteUInt((uint)networkTick.tick); // Current Tick Header
  59. writer.WriteByte((byte)sendMsgBuffer.Count); // Message count Header
  60. for (int a = 0; a < sendMsgBuffer.Count; a++)
  61. {
  62. NetMsg netMsg = sendMsgBuffer[a];
  63. if(netMsg != null)
  64. {
  65. NetMsg typeMsg = FindWithCode(netMsg.code);
  66. if(typeMsg != null) typeMsg.SendMessage(client, netMsg, ref writer);
  67. else Debug.LogError($"Could not find NetMsg with code {netMsg.code}");
  68. }
  69. }
  70. client.m_Driver.EndSend(writer);
  71. sendMsgBuffer.Clear();
  72. }
  73. private void ReadBufferedData()
  74. {
  75. for (int a = 0; a < receivedMsgBuffer.Count; a++)
  76. {
  77. NetMsg msg = receivedMsgBuffer[a];
  78. msg.ReadMessage(client);
  79. }
  80. receivedMsgBuffer.Clear();
  81. }
  82. }
  83. }