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