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 connected.</summary>
- public class NetClientConnectedMsg : NetMsg
- {
- public override uint code { get { return (uint)NetOpCode.ClientConnected; }}
- public string playerName = "";
- public NetClientConnectedMsg() {}
- 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)
- {
- NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg();
- netClientConnectedMsg.Deserialize(ref stream);
- return netClientConnectedMsg;
- }
- public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream)
- {
- NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg();
- netClientConnectedMsg.Deserialize(ref stream);
- return netClientConnectedMsg;
- }
- public override void ReadMessage(ClientBehaviour client)
- {
- string text = $"New peer connected to server (playerName = {playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_PeerConnected", text, -1, code, this);
- }
- }
- }
|