NetPongMsg.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Networking.Transport;
  5. namespace KairoEngine.Multiplayer
  6. {
  7. public class NetPongMsg : NetMsg
  8. {
  9. public override uint code { get { return (uint)NetOpCode.Pong; }}
  10. public uint pingId;
  11. public NetPongMsg() {}
  12. public NetPongMsg(uint pingId)
  13. {
  14. this.pingId = pingId;
  15. }
  16. public override void Serialize(ref DataStreamWriter writer)
  17. {
  18. writer.WriteByte((byte)code);
  19. writer.WriteUInt(pingId);
  20. }
  21. public override void Deserialize(ref DataStreamReader reader)
  22. {
  23. pingId = reader.ReadUInt();
  24. }
  25. public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer )
  26. {
  27. if(client == null) return;
  28. NetPongMsg netPongMsg = (NetPongMsg)netMsg;
  29. string text = $"Responding pong ({netPongMsg.pingId}))";
  30. if(client.debug) Debug.Log(text);
  31. NetMsgEvents.Trigger($"{client.eventStreamName}_PongSent", text, -1, code, netPongMsg);
  32. netPongMsg.Serialize(ref writer);
  33. }
  34. public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
  35. {
  36. NetPongMsg netPongMsg = new NetPongMsg();
  37. netPongMsg.Deserialize(ref stream);
  38. string text = $"Received pong from client (ID {clientId}, PingId = {netPongMsg.pingId})";
  39. if(server.debug) Debug.Log(text);
  40. NetMsgEvents.Trigger($"{server.eventStreamName}_PongReceived", text, clientId, code, netPongMsg);
  41. return netPongMsg;
  42. }
  43. public override void ReadMessage(ServerBehaviour server, int clientId = -1)
  44. {
  45. }
  46. }
  47. }