123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.Multiplayer
- {
- public enum ClientState
- {
- Disconnected,
- Connecting,
- Connected,
- }
- [HideMonoScript]
- public class ClientStatus : MonoBehaviour
- {
- public ClientBehaviour client;
- [ReadOnly] public ClientState state;
- [ReadOnly] public int peerCount;
- [ReadOnly] public float uptime = 0;
- [ReadOnly] public int tick = 0;
- [ReadOnly] public string serverAddress;
- [ReadOnly] public int packetsSent;
- [ReadOnly] public int packetsReceived;
- private void OnEnable()
- {
- NetMsgEvents.StartListening($"{client.eventStreamName}_InvalidOpCode", OnReceivePacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnSentPacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_ServerDisconnect", OnReceivePacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_SendingHandshake", OnSentPacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_HandshakeAccepted", OnReceivePacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_PingReceived", OnReceivePacket);
- NetMsgEvents.StartListening($"{client.eventStreamName}_PongSent", OnSentPacket);
- GenericEvents.StartListening($"{client.eventStreamName}_OnTick", OnTick);
- }
- private void OnDisable()
- {
- NetMsgEvents.StopListening($"{client.eventStreamName}_InvalidOpCode", OnReceivePacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnSentPacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_ServerDisconnect", OnReceivePacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_SendingHandshake", OnSentPacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnReceivePacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_PingReceived", OnReceivePacket);
- NetMsgEvents.StopListening($"{client.eventStreamName}_PongSent", OnSentPacket);
- }
- private void Update()
- {
- if(client.m_Driver.IsCreated == false)
- {
- state = ClientState.Disconnected;
- peerCount = 0;
- uptime = 0;
- packetsSent = 0;
- packetsReceived = 0;
- }
- else
- {
- state = ClientState.Connected;
- peerCount = 0;
- uptime += Time.deltaTime;
- }
- }
- 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;
- }
- }
|