123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Networking.Transport;
- namespace KairoEngine.Multiplayer
- {
- /// <summary>Network message sent from the server to alert clients that a peer has disconnected.</summary>
- public class NetClientDisconnectedMsg : NetMsg
- {
- public override uint code { get { return (uint)NetOpCode.ClientDisconnected; }}
- public string playerName = "";
- public NetClientDisconnectedMsg() {}
- 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;
- NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg;
- string text = $"Alerting clients of new connection (playerName = {netClientConnectedMsg.playerName})";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientConnectedMsg", text, clientId, code, netClientConnectedMsg);
- netClientConnectedMsg.Serialize(ref writer);
- }
- public override NetMsg ReceiveMessage(ServerBehaviour server, ref DataStreamReader stream, int clientId = -1)
- {
- NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
- netClientDisconnectedMsg.Deserialize(ref stream);
- return netClientDisconnectedMsg;
- }
- public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
- {
- NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
- netClientDisconnectedMsg.Deserialize(ref stream);
- return netClientDisconnectedMsg;
- }
- public override void ReadMessage(ClientBehaviour client)
- {
- string text = $"Peer disconnected to server (playerName = {playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, this);
- }
- }
- }
|