NetHandshakeMsg.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 NetHandshakeMsg : NetMsg
  8. {
  9. public override uint code { get { return (uint)NetOpCode.Handshake; }}
  10. public string playerName = "";
  11. public NetHandshakeMsg() {}
  12. public override void Serialize(ref DataStreamWriter writer)
  13. {
  14. writer.WriteByte((byte)code);
  15. writer.WriteFixedString64(playerName);
  16. }
  17. public override void Deserialize(ref DataStreamReader reader)
  18. {
  19. playerName = reader.ReadFixedString64().ToString();
  20. }
  21. // public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId)
  22. // {
  23. // if(server == null) return;
  24. // NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
  25. // string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
  26. // if(server.debug) Debug.Log(text);
  27. // NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
  28. // netHandshakeMsg.Serialize(ref writer);
  29. // }
  30. public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer )
  31. {
  32. if(client == null) return;
  33. NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
  34. string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";
  35. if(client.debug) Debug.Log(text);
  36. NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, -1, code, netHandshakeMsg);
  37. netHandshakeMsg.Serialize(ref writer);
  38. }
  39. public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
  40. {
  41. NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
  42. netHandshakeMsg.Deserialize(ref stream);
  43. return netHandshakeMsg;
  44. }
  45. // public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
  46. // {
  47. // NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
  48. // netHandshakeMsg.Deserialize(ref stream);
  49. // return netHandshakeMsg;
  50. // }
  51. public override void ReadMessage(ServerBehaviour server, int clientId = -1)
  52. {
  53. string text = $"Received handshake from client (ID {clientId}, playerName = {playerName})";
  54. if(server.debug) Debug.Log(text);
  55. NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, this);
  56. }
  57. public override void ReadMessage(ClientBehaviour client)
  58. {
  59. string text = $"Server accepted handshake (playerName = {playerName})";
  60. if(client.debug) Debug.Log(text);
  61. NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, this);
  62. }
  63. }
  64. }