123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- using Sirenix.OdinInspector;
- namespace KairoEngine.Multiplayer
- {
- public enum ServerState
- {
- Offline,
- Online
- }
- [HideMonoScript]
- public class ServerStatus : MonoBehaviour
- {
- public ServerBehaviour server;
- [ReadOnly] public ServerState state;
- [ReadOnly] public int clientCount;
- [ReadOnly] public int maxClients;
- [ReadOnly] public float uptime = 0;
- [ReadOnly] public int tick = 0;
- [ReadOnly] public string ipAddress;
- [ReadOnly] public int packetsSent;
- [ReadOnly] public int packetsReceived;
- private void OnEnable()
- {
- NetMsgEvents.StartListening($"{server.eventStreamName}_InvalidOpCode", OnReceivePacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnSentPacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnReceivePacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_HandshakeReceived", OnReceivePacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_AcceptingHandshake", OnSentPacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_PingSent", OnSentPacket);
- NetMsgEvents.StartListening($"{server.eventStreamName}_PongReceived", OnReceivePacket);
- GenericEvents.StartListening($"{server.eventStreamName}_OnTick", OnTick);
- }
- private void OnDisable()
- {
- NetMsgEvents.StopListening($"{server.eventStreamName}_InvalidOpCode", OnReceivePacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnSentPacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_ClientDisconnect", OnReceivePacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_HandshakeReceived", OnReceivePacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnSentPacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_PingSent", OnSentPacket);
- NetMsgEvents.StopListening($"{server.eventStreamName}_PongReceived", OnReceivePacket);
- GenericEvents.StopListening($"{server.eventStreamName}_OnTick", OnTick);
- }
- private void Update()
- {
- if(server.m_Driver.IsCreated == false)
- {
- state = ServerState.Offline;
- clientCount = 0;
- uptime = 0;
- packetsSent = 0;
- packetsReceived = 0;
- }
- else
- {
- state = ServerState.Online;
- clientCount = 0;
- for (int i = 0; i < server.m_Connections.Length; i++)
- {
- if(server.m_Connections[i].IsCreated) clientCount += 1;
- }
- uptime += Time.deltaTime;
- }
- maxClients = server.players;
- }
- private void OnSentPacket(string text, int clientId, uint code, NetMsg netMsg) => packetsSent += 1;
- private void OnReceivePacket(string text, int clientId, uint code, NetMsg netMsg) => packetsReceived += 1;
- private void OnTick(int newTick) => tick = newTick;
- }
- }
|