using UnityEngine; using Unity.Networking.Transport; using KairoEngine.Core; using Sirenix.OdinInspector; namespace KairoEngine.Multiplayer { [HideMonoScript] public class ClientBehaviour : MonoBehaviour { public NetMsgController netMsgController; public string eventStreamName = "ClientEvents"; public string address = "192.168.1.1"; public ushort port = 9000; public string playerName = "Player 1"; public bool autoConnect = true; public bool debug = false; public NetworkDriver m_Driver; public NetworkConnection m_Connection; public bool m_Done; private void Awake() { if(netMsgController == null) Debug.LogError("Client is missing NetMsgController!", this); } private void Start () { if(autoConnect) ConnectToServer(); } public void ConnectToServer() { if(netMsgController == null) { Debug.LogError("Client cannot connect to server with missing NetMsgController!", this); return; } m_Driver = NetworkDriver.Create(); m_Connection = default(NetworkConnection); var endpoint = NetworkEndPoint.Parse(address, port); m_Connection = m_Driver.Connect(endpoint); if(debug) Debug.Log($"Client Connecting to {address}:{port}"); GenericEvents.Trigger($"{eventStreamName}_Connecting", $"Connecting to {address}:{port}"); } public void DisconnectFromServer() { NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg(); SendNetMsgToServer((uint)NetOpCode.Disconnect, netDisconnectMsg); Timer.ExecuteRealTime(500, () => { if(debug) Debug.Log("Client disconnected from server"); GenericEvents.Trigger($"{eventStreamName}_Disconected", "Client disconnected from server"); m_Connection = default(NetworkConnection); if(m_Driver.IsCreated) m_Driver.Dispose(); }); } public void SendNetMsgToServer(uint code, NetMsg netMsg) { DataStreamWriter writer = new DataStreamWriter(); m_Driver.BeginSend(m_Connection, out writer); netMsgController.SendData(code, netMsg, ref writer, this); m_Driver.EndSend(writer); } public void OnDestroy() { if(m_Driver.IsCreated) m_Driver.Dispose(); } void Update() { if(!m_Driver.IsCreated) return; m_Driver.ScheduleUpdate().Complete(); if (!m_Connection.IsCreated) { if (!m_Done) { if(debug) Debug.Log("Something went wrong during connect"); GenericEvents.Trigger($"{eventStreamName}_ErrorConnecting", "Error connecting to server"); m_Done = true; } return; } DataStreamReader stream; NetworkEvent.Type cmd; while ((cmd = m_Connection.PopEvent(m_Driver, out stream)) != NetworkEvent.Type.Empty) { if (cmd == NetworkEvent.Type.Connect) { if(debug) Debug.Log("We are now connected to the server"); GenericEvents.Trigger($"{eventStreamName}_Connected", "Connected to server"); } else if (cmd == NetworkEvent.Type.Data) { //if(debug) Debug.Log("Client received data from the server"); netMsgController.ReceiveData(ref stream, this); } else if (cmd == NetworkEvent.Type.Disconnect) { if(debug) Debug.Log("Client got disconnected from server"); GenericEvents.Trigger($"{eventStreamName}_ServerDisconnected", "Client got disconnected from server"); m_Connection = default(NetworkConnection); } } } } }