12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Networking.Transport;
- namespace KairoEngine.Multiplayer
- {
- public class NetHandshakeMsg : NetMsg
- {
- public override uint code { get { return (uint)NetOpCode.Handshake; }}
- public string playerName = "";
- public NetHandshakeMsg() {}
- public override void Serialize(ref DataStreamWriter writer)
- {
- writer.WriteByte((byte)code);
- writer.WriteFixedString64(playerName);
- }
- public override void Deserialize(ref DataStreamReader reader)
- {
- playerName = reader.ReadFixedString64().ToString();
- }
- // public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer, int clientId)
- // {
- // if(server == null) return;
- // NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
- // string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
- // if(server.debug) Debug.Log(text);
- // NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
- // netHandshakeMsg.Serialize(ref writer);
- // }
- public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer )
- {
- if(client == null) return;
- NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
- string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, -1, code, netHandshakeMsg);
- netHandshakeMsg.Serialize(ref writer);
- }
- public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
- {
- NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
- netHandshakeMsg.Deserialize(ref stream);
- return netHandshakeMsg;
- }
- // public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
- // {
- // NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
- // netHandshakeMsg.Deserialize(ref stream);
- // return netHandshakeMsg;
- // }
- public override void ReadMessage(ServerBehaviour server, int clientId = -1)
- {
- string text = $"Received handshake from client (ID {clientId}, playerName = {playerName})";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, this);
- }
- public override void ReadMessage(ClientBehaviour client)
- {
- string text = $"Server accepted handshake (playerName = {playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, this);
- }
- }
- }
|