NetAcceptHandshakeMsg.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 NetAcceptHandshakeMsg : NetMsg
  8. {
  9. public override uint code { get { return (uint)NetOpCode.AcceptHandshake; }}
  10. public string playerName = "";
  11. public uint tick = 0;
  12. public NetAcceptHandshakeMsg() {}
  13. public override void Serialize(ref DataStreamWriter writer)
  14. {
  15. writer.WriteByte((byte)code);
  16. writer.WriteFixedString64(playerName);
  17. writer.WriteUInt(tick);
  18. }
  19. public override void Deserialize(ref DataStreamReader reader)
  20. {
  21. playerName = reader.ReadFixedString64().ToString();
  22. tick = reader.ReadUInt();
  23. }
  24. public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId)
  25. {
  26. if(server == null) return;
  27. NetAcceptHandshakeMsg netAcceptHandshakeMsg = (NetAcceptHandshakeMsg)netMsg;
  28. string text = $"Accepting handshake from client (ID {clientId}, playerName = {netAcceptHandshakeMsg.playerName}, tick = {netAcceptHandshakeMsg.tick})";
  29. if(server.debug) Debug.Log(text);
  30. NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netAcceptHandshakeMsg);
  31. netAcceptHandshakeMsg.Serialize(ref writer);
  32. }
  33. public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
  34. {
  35. NetAcceptHandshakeMsg netAcceptHandshakeMsg = new NetAcceptHandshakeMsg();
  36. netAcceptHandshakeMsg.Deserialize(ref stream);
  37. return netAcceptHandshakeMsg;
  38. }
  39. public override void ReadMessage(ClientBehaviour client)
  40. {
  41. string text = $"Server accepted handshake (playerName = {playerName}, tick = {tick})";
  42. if(client.debug) Debug.Log(text);
  43. NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, this);
  44. }
  45. }
  46. }