using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.Networking.Transport; namespace KairoEngine.Multiplayer { public class NetPingMsg : NetMsg { public override uint code { get { return (uint)NetOpCode.Ping; }} public uint pingId; public NetPingMsg() {} public NetPingMsg(uint pingId) { this.pingId = pingId; } public override void Serialize(ref DataStreamWriter writer) { writer.WriteByte((byte)code); writer.WriteUInt(pingId); } public override void Deserialize(ref DataStreamReader reader) { pingId = reader.ReadUInt(); } public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId) { if(server == null) return; NetPingMsg netPingMsg = (NetPingMsg)netMsg; string text = $"Sending ping to client (ID {clientId}, PingId = {netPingMsg.pingId})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_PingSent", text, clientId, code, netPingMsg); netPingMsg.Serialize(ref writer); } public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream) { NetPingMsg netPingMsg = new NetPingMsg(); netPingMsg.Deserialize(ref stream); return netPingMsg; } public override void ReadMessage(ClientBehaviour client) { string text = $"Server sent a ping (PingId = {pingId})"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_PingReceived", text, -1, code, this); } } }