ClientBehaviour.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using Unity.Networking.Transport;
  3. using KairoEngine.Core;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Multiplayer
  6. {
  7. [HideMonoScript]
  8. public class ClientBehaviour : MonoBehaviour
  9. {
  10. public NetMsgController netMsgController;
  11. public string eventStreamName = "ClientEvents";
  12. public string address = "192.168.1.1";
  13. public ushort port = 9000;
  14. public string playerName = "Player 1";
  15. public bool autoConnect = true;
  16. public bool debug = false;
  17. public NetworkDriver m_Driver;
  18. public NetworkConnection m_Connection;
  19. public bool m_Done;
  20. private void Awake()
  21. {
  22. if(netMsgController == null) Debug.LogError("Client is missing NetMsgController!", this);
  23. }
  24. private void Start ()
  25. {
  26. if(autoConnect) ConnectToServer();
  27. }
  28. public void ConnectToServer()
  29. {
  30. if(netMsgController == null)
  31. {
  32. Debug.LogError("Client cannot connect to server with missing NetMsgController!", this);
  33. return;
  34. }
  35. m_Driver = NetworkDriver.Create();
  36. m_Connection = default(NetworkConnection);
  37. var endpoint = NetworkEndPoint.Parse(address, port);
  38. m_Connection = m_Driver.Connect(endpoint);
  39. if(debug) Debug.Log($"Client Connecting to <b>{address}:{port}</b>");
  40. GenericEvents.Trigger($"{eventStreamName}_Connecting", $"Connecting to <b>{address}:{port}</b>");
  41. }
  42. public void DisconnectFromServer()
  43. {
  44. NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg();
  45. SendNetMsgToServer((uint)NetOpCode.Disconnect, netDisconnectMsg);
  46. Timer.ExecuteRealTime(500, () => {
  47. if(debug) Debug.Log("Client disconnected from server");
  48. GenericEvents.Trigger($"{eventStreamName}_Disconected", "Client disconnected from server");
  49. m_Connection = default(NetworkConnection);
  50. if(m_Driver.IsCreated) m_Driver.Dispose();
  51. });
  52. }
  53. public void SendNetMsgToServer(uint code, NetMsg netMsg)
  54. {
  55. netMsgController.SendData(code, netMsg, this);
  56. }
  57. public void OnDestroy()
  58. {
  59. if(m_Driver.IsCreated) m_Driver.Dispose();
  60. }
  61. void Update()
  62. {
  63. if(!m_Driver.IsCreated) return;
  64. m_Driver.ScheduleUpdate().Complete();
  65. if (!m_Connection.IsCreated)
  66. {
  67. if (!m_Done)
  68. {
  69. if(debug) Debug.Log("Something went wrong during connect");
  70. GenericEvents.Trigger($"{eventStreamName}_ErrorConnecting", "Error connecting to server");
  71. m_Done = true;
  72. }
  73. return;
  74. }
  75. DataStreamReader stream;
  76. NetworkEvent.Type cmd;
  77. while ((cmd = m_Connection.PopEvent(m_Driver, out stream)) != NetworkEvent.Type.Empty)
  78. {
  79. if (cmd == NetworkEvent.Type.Connect)
  80. {
  81. if(debug) Debug.Log("We are now connected to the server");
  82. GenericEvents.Trigger($"{eventStreamName}_Connected", "Connected to server");
  83. }
  84. else if (cmd == NetworkEvent.Type.Data)
  85. {
  86. //if(debug) Debug.Log("Client received data from the server");
  87. netMsgController.ReceiveData(ref stream, this);
  88. }
  89. else if (cmd == NetworkEvent.Type.Disconnect)
  90. {
  91. if(debug) Debug.Log("Client got disconnected from server");
  92. GenericEvents.Trigger($"{eventStreamName}_ServerDisconnected", "Client got disconnected from server");
  93. m_Connection = default(NetworkConnection);
  94. }
  95. }
  96. }
  97. }
  98. }