NetClientDisconnectedMsg.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Networking.Transport;
  5. namespace KairoEngine.Multiplayer
  6. {
  7. /// <summary>Network message sent from the server to alert clients that a peer has disconnected.</summary>
  8. public class NetClientDisconnectedMsg : NetMsg
  9. {
  10. public override uint code { get { return (uint)NetOpCode.ClientDisconnected; }}
  11. public string playerName = "";
  12. public NetClientDisconnectedMsg() {}
  13. public override void Serialize(ref DataStreamWriter writer)
  14. {
  15. writer.WriteByte((byte)code);
  16. writer.WriteFixedString64(playerName);
  17. }
  18. public override void Deserialize(ref DataStreamReader reader)
  19. {
  20. playerName = reader.ReadFixedString64().ToString();
  21. }
  22. public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId)
  23. {
  24. if(server == null) return;
  25. NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg;
  26. string text = $"Alerting clients of new connection (playerName = {netClientConnectedMsg.playerName})";
  27. if(server.debug) Debug.Log(text);
  28. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientConnectedMsg", text, clientId, code, netClientConnectedMsg);
  29. netClientConnectedMsg.Serialize(ref writer);
  30. }
  31. public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
  32. {
  33. NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
  34. netClientDisconnectedMsg.Deserialize(ref stream);
  35. return netClientDisconnectedMsg;
  36. }
  37. public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
  38. {
  39. NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
  40. netClientDisconnectedMsg.Deserialize(ref stream);
  41. return netClientDisconnectedMsg;
  42. }
  43. public override void ReadMessage(ClientBehaviour client)
  44. {
  45. string text = $"Peer disconnected to server (playerName = {playerName})";
  46. if(client.debug) Debug.Log(text);
  47. NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, this);
  48. }
  49. }
  50. }