123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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 <b>{address}:{port}</b>");
- GenericEvents.Trigger($"{eventStreamName}_Connecting", $"Connecting to <b>{address}:{port}</b>");
- }
- 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)
- {
- netMsgController.SendData(code, netMsg, this);
- }
- 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);
- }
- }
- }
- }
- }
|