123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using System.Collections;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Networking.Transport;
- using KairoEngine.Core;
- using Sirenix.OdinInspector;
- namespace KairoEngine.Multiplayer
- {
- [HideMonoScript]
- public class NetMsgController : MonoBehaviour
- {
- private static List<NetMsg> netMessageTypes = new List<NetMsg>();
- private void Start()
- {
- GetNetMessageTypes();
- }
- protected void GetNetMessageTypes()
- {
- netMessageTypes = ReflectiveEnumerator.GetEnumerableOfType<NetMsg>()
- .Where(x => x.code != 0)
- .Select(x => { return x; })
- .ToList();
- }
- public virtual NetMsg FindWithCode(uint code)
- {
- for (int i = 0; i < netMessageTypes.Count; i++)
- {
- if(netMessageTypes[i].code == code) return netMessageTypes[i];
- }
- return null;
- }
- public virtual void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId)
- {
- uint code = stream.ReadByte();
- NetMsg msg = FindWithCode(code).ReceiveMessage(server, ref stream, clientId);
- msg.ReadMessage(server, clientId);
- }
- public virtual void ReceiveData(ref DataStreamReader stream, ClientBehaviour client)
- {
- uint code = stream.ReadByte();
- NetMsg msg = FindWithCode(code).ReceiveMessage(client, ref stream);
- msg.ReadMessage(client);
- }
- public virtual void SendData(uint code, NetMsg netMsg, ServerBehaviour server, int clientId)
- {
- DataStreamWriter writer = new DataStreamWriter();
- server.m_Driver.BeginSend(server.m_Connections[clientId], out writer);
- FindWithCode(code).SendMessage(server, netMsg, ref writer, clientId);
- server.m_Driver.EndSend(writer);
- }
- public virtual void SendData(uint code, NetMsg netMsg, ClientBehaviour client)
- {
- DataStreamWriter writer = new DataStreamWriter();
- client.m_Driver.BeginSend(client.m_Connection, out writer);
- FindWithCode(code).SendMessage(client, netMsg, ref writer);
- client.m_Driver.EndSend(writer);
- }
- // Remove rest of the code below
- /*
- public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, int clientId = -1)
- => Deserialize(code, ref stream, server, null, clientId);
- public static void Deserialize(uint code, ref DataStreamReader stream, ClientBehaviour client, int clientId = -1)
- => Deserialize(code, ref stream, null, client, clientId);
- public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, ClientBehaviour client, int clientId)
- {
- NetOpCode opCode = (NetOpCode)code;
- switch (opCode)
- {
- case NetOpCode.Disconnect:
- NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
- netDisconnectMsg.Deserialize(ref stream);
- if(client != null)
- {
- string text = $"Server disconnected client";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_ServerDisconnect", text, -1, code, netDisconnectMsg);
- }
- if(server != null)
- {
- string text = $"Client is disconnecting";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_ClientDisconnect", text, clientId, code, netDisconnectMsg);
- }
- break;
- case NetOpCode.Handshake:
- NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
- netHandshakeMsg.Deserialize(ref stream);
- if(client != null)
- {
- string text = $"Server accepted handshake (playerName = {netHandshakeMsg.playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, netHandshakeMsg);
-
- }
- if(server != null)
- {
- string text = $"Received handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, netHandshakeMsg);
- }
- break;
- case NetOpCode.ClientConnected:
- NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg();
- netClientConnectedMsg.Deserialize(ref stream);
- if(client != null)
- {
- string text = $"New peer connected to server (playerName = {netClientConnectedMsg.playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_PeerConnected", text, -1, code, netClientConnectedMsg);
- }
- break;
- case NetOpCode.ClientDisconnected:
- NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg();
- netClientDisconnectedMsg.Deserialize(ref stream);
- if(client != null)
- {
- string text = $"Peer disconnected to server (playerName = {netClientDisconnectedMsg.playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, netClientDisconnectedMsg);
- }
- break;
- default:
- NetMsg netMsg = new NetMsg();
- netMsg.Deserialize(ref stream);
- string error = $"Received data with invalid NetOpCode {code}";
- if(client != null)
- {
- if(client.debug) Debug.Log(error);
- NetMsgEvents.Trigger($"{client.eventStreamName}_InvalidOpCode", error, -1, code, netMsg);
- }
- if(server != null)
- {
- if(server.debug) Debug.Log(error);
- NetMsgEvents.Trigger($"{server.eventStreamName}_InvalidOpCode", error, clientId, code, netMsg);
- }
- break;
- }
- }
- public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId = -1)
- => Serialize(code, netMsg, ref writer, server, null, clientId);
- public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client, int clientId = -1 )
- => Serialize(code, netMsg, ref writer, null, client, clientId);
- public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, ClientBehaviour client, int clientId)
- {
- NetOpCode opCode = (NetOpCode)code;
- switch (opCode)
- {
- case NetOpCode.Disconnect:
- NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg;
- if(client != null)
- {
- string text = $"Sending disconnect message to server";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_SendingDisconnectMessageToServer", text, clientId, code, netDisconnectMsg);
- }
- if(server != null)
- {
- string text = $"Sending disconnect message to client (ID {clientId})";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_SendingDisconnectMessageToClient", text, clientId, code, netDisconnectMsg);
- }
- netDisconnectMsg.Serialize(ref writer);
- break;
- case NetOpCode.Handshake:
- NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
- if(client != null)
- {
- string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";
- if(client.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, clientId, code, netHandshakeMsg);
- }
- if(server != null)
- {
- 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);
- break;
- case NetOpCode.ClientConnected:
- NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg;
- if(server != null)
- {
- 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);
- break;
- case NetOpCode.ClientDisconnected:
- NetClientDisconnectedMsg netClientDisconnectedMsg = (NetClientDisconnectedMsg)netMsg;
- if(server != null)
- {
- string text = $"Alerting clients of disconnection (playerName = {netClientDisconnectedMsg.playerName})";
- if(server.debug) Debug.Log(text);
- NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientDisconnectedMsg", text, clientId, code, netClientDisconnectedMsg);
- }
- netClientDisconnectedMsg.Serialize(ref writer);
- break;
- default:
- break;
- }
- }
- */
- }
- }
|