NetDisconnectMsg.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 NetDisconnectMsg : NetMsg
  8. {
  9. public override uint code { get { return (uint)NetOpCode.Disconnect; }}
  10. public NetDisconnectMsg() {}
  11. public override void Serialize(ref DataStreamWriter writer)
  12. {
  13. writer.WriteByte((byte)code);
  14. }
  15. public override void Deserialize(ref DataStreamReader reader)
  16. {
  17. }
  18. public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId)
  19. {
  20. if(server == null) return;
  21. NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg;
  22. string text = $"Sending disconnect message to client (ID {clientId})";
  23. if(server.debug) Debug.Log(text);
  24. NetMsgEvents.Trigger($"{server.eventStreamName}_SendingDisconnectMessageToClient", text, clientId, code, netDisconnectMsg);
  25. netDisconnectMsg.Serialize(ref writer);
  26. }
  27. public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer )
  28. {
  29. if(client == null) return;
  30. NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg;
  31. string text = $"Sending disconnect message to server";
  32. if(client.debug) Debug.Log(text);
  33. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingDisconnectMessageToServer", text, -1, code, netDisconnectMsg);
  34. netDisconnectMsg.Serialize(ref writer);
  35. }
  36. public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
  37. {
  38. NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
  39. netDisconnectMsg.Deserialize(ref stream);
  40. return netDisconnectMsg;
  41. }
  42. public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
  43. {
  44. NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
  45. netDisconnectMsg.Deserialize(ref stream);
  46. return netDisconnectMsg;
  47. }
  48. public override void ReadMessage(ServerBehaviour server, int clientId = -1)
  49. {
  50. string text = $"Client is disconnecting";
  51. if(server.debug) Debug.Log(text);
  52. NetMsgEvents.Trigger($"{server.eventStreamName}_ClientDisconnect", text, clientId, code, this);
  53. }
  54. public override void ReadMessage(ClientBehaviour client)
  55. {
  56. string text = $"Server disconnected client";
  57. if(client.debug) Debug.Log(text);
  58. NetMsgEvents.Trigger($"{client.eventStreamName}_ServerDisconnect", text, -1, code, this);
  59. }
  60. }
  61. }