ClientStatus.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.Multiplayer
  7. {
  8. public enum ClientState
  9. {
  10. Disconnected,
  11. Connecting,
  12. Connected,
  13. }
  14. [HideMonoScript]
  15. public class ClientStatus : MonoBehaviour
  16. {
  17. public ClientBehaviour client;
  18. [ReadOnly] public ClientState state;
  19. [ReadOnly] public int peerCount;
  20. [ReadOnly] public float uptime = 0;
  21. [ReadOnly] public int tick = 0;
  22. [ReadOnly] public string serverAddress;
  23. [ReadOnly] public int packetsSent;
  24. [ReadOnly] public int packetsReceived;
  25. private void OnEnable()
  26. {
  27. NetMsgEvents.StartListening($"{client.eventStreamName}_InvalidOpCode", OnReceivePacket);
  28. NetMsgEvents.StartListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnSentPacket);
  29. NetMsgEvents.StartListening($"{client.eventStreamName}_ServerDisconnect", OnReceivePacket);
  30. NetMsgEvents.StartListening($"{client.eventStreamName}_SendingHandshake", OnSentPacket);
  31. NetMsgEvents.StartListening($"{client.eventStreamName}_HandshakeAccepted", OnReceivePacket);
  32. NetMsgEvents.StartListening($"{client.eventStreamName}_PingReceived", OnReceivePacket);
  33. NetMsgEvents.StartListening($"{client.eventStreamName}_PongSent", OnSentPacket);
  34. GenericEvents.StartListening($"{client.eventStreamName}_OnTick", OnTick);
  35. }
  36. private void OnDisable()
  37. {
  38. NetMsgEvents.StopListening($"{client.eventStreamName}_InvalidOpCode", OnReceivePacket);
  39. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnSentPacket);
  40. NetMsgEvents.StopListening($"{client.eventStreamName}_ServerDisconnect", OnReceivePacket);
  41. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingHandshake", OnSentPacket);
  42. NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnReceivePacket);
  43. NetMsgEvents.StopListening($"{client.eventStreamName}_PingReceived", OnReceivePacket);
  44. NetMsgEvents.StopListening($"{client.eventStreamName}_PongSent", OnSentPacket);
  45. }
  46. private void Update()
  47. {
  48. if(client.m_Driver.IsCreated == false)
  49. {
  50. state = ClientState.Disconnected;
  51. peerCount = 0;
  52. uptime = 0;
  53. packetsSent = 0;
  54. packetsReceived = 0;
  55. }
  56. else
  57. {
  58. state = ClientState.Connected;
  59. peerCount = 0;
  60. uptime += Time.deltaTime;
  61. }
  62. }
  63. private void OnSentPacket(string text, int clientId, uint code, NetMsg netMsg) => packetsSent += 1;
  64. private void OnReceivePacket(string text, int clientId, uint code, NetMsg netMsg) => packetsReceived += 1;
  65. private void OnTick(int newTick) => tick = newTick;
  66. }
  67. }