ServerStatus.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.Multiplayer
  7. {
  8. public enum ServerState
  9. {
  10. Offline,
  11. Online
  12. }
  13. [HideMonoScript]
  14. public class ServerStatus : MonoBehaviour
  15. {
  16. public ServerBehaviour server;
  17. [ReadOnly] public ServerState state;
  18. [ReadOnly] public int clientCount;
  19. [ReadOnly] public int maxClients;
  20. [ReadOnly] public float uptime = 0;
  21. [ReadOnly] public int tick = 0;
  22. [ReadOnly] public string ipAddress;
  23. [ReadOnly] public int packetsSent;
  24. [ReadOnly] public int packetsReceived;
  25. private void OnEnable()
  26. {
  27. NetMsgEvents.StartListening($"{server.eventStreamName}_InvalidOpCode", OnReceivePacket);
  28. NetMsgEvents.StartListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnSentPacket);
  29. NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnReceivePacket);
  30. NetMsgEvents.StartListening($"{server.eventStreamName}_HandshakeReceived", OnReceivePacket);
  31. NetMsgEvents.StartListening($"{server.eventStreamName}_AcceptingHandshake", OnSentPacket);
  32. NetMsgEvents.StartListening($"{server.eventStreamName}_PingSent", OnSentPacket);
  33. NetMsgEvents.StartListening($"{server.eventStreamName}_PongReceived", OnReceivePacket);
  34. GenericEvents.StartListening($"{server.eventStreamName}_OnTick", OnTick);
  35. }
  36. private void OnDisable()
  37. {
  38. NetMsgEvents.StopListening($"{server.eventStreamName}_InvalidOpCode", OnReceivePacket);
  39. NetMsgEvents.StopListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnSentPacket);
  40. NetMsgEvents.StopListening($"{server.eventStreamName}_ClientDisconnect", OnReceivePacket);
  41. NetMsgEvents.StopListening($"{server.eventStreamName}_HandshakeReceived", OnReceivePacket);
  42. NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnSentPacket);
  43. NetMsgEvents.StopListening($"{server.eventStreamName}_PingSent", OnSentPacket);
  44. NetMsgEvents.StopListening($"{server.eventStreamName}_PongReceived", OnReceivePacket);
  45. GenericEvents.StopListening($"{server.eventStreamName}_OnTick", OnTick);
  46. }
  47. private void Update()
  48. {
  49. if(server.m_Driver.IsCreated == false)
  50. {
  51. state = ServerState.Offline;
  52. clientCount = 0;
  53. uptime = 0;
  54. packetsSent = 0;
  55. packetsReceived = 0;
  56. }
  57. else
  58. {
  59. state = ServerState.Online;
  60. clientCount = 0;
  61. for (int i = 0; i < server.m_Connections.Length; i++)
  62. {
  63. if(server.m_Connections[i].IsCreated) clientCount += 1;
  64. }
  65. uptime += Time.deltaTime;
  66. }
  67. maxClients = server.players;
  68. }
  69. private void OnSentPacket(string text, int clientId, uint code, NetMsg netMsg) => packetsSent += 1;
  70. private void OnReceivePacket(string text, int clientId, uint code, NetMsg netMsg) => packetsReceived += 1;
  71. private void OnTick(int newTick) => tick = newTick;
  72. }
  73. }