1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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);
- 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);
- 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;
- }
- }
|