ClientStatus.cs 2.4 KB

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