ClientBehaviour.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. DataStreamWriter writer = new DataStreamWriter();
  56. m_Driver.BeginSend(m_Connection, out writer);
  57. netMsgController.SendData(code, netMsg, ref writer, this);
  58. m_Driver.EndSend(writer);
  59. }
  60. public void OnDestroy()
  61. {
  62. if(m_Driver.IsCreated) m_Driver.Dispose();
  63. }
  64. void Update()
  65. {
  66. if(!m_Driver.IsCreated) return;
  67. m_Driver.ScheduleUpdate().Complete();
  68. if (!m_Connection.IsCreated)
  69. {
  70. if (!m_Done)
  71. {
  72. if(debug) Debug.Log("Something went wrong during connect");
  73. GenericEvents.Trigger($"{eventStreamName}_ErrorConnecting", "Error connecting to server");
  74. m_Done = true;
  75. }
  76. return;
  77. }
  78. DataStreamReader stream;
  79. NetworkEvent.Type cmd;
  80. while ((cmd = m_Connection.PopEvent(m_Driver, out stream)) != NetworkEvent.Type.Empty)
  81. {
  82. if (cmd == NetworkEvent.Type.Connect)
  83. {
  84. if(debug) Debug.Log("We are now connected to the server");
  85. GenericEvents.Trigger($"{eventStreamName}_Connected", "Connected to server");
  86. }
  87. else if (cmd == NetworkEvent.Type.Data)
  88. {
  89. //if(debug) Debug.Log("Client received data from the server");
  90. netMsgController.ReceiveData(ref stream, this);
  91. }
  92. else if (cmd == NetworkEvent.Type.Disconnect)
  93. {
  94. if(debug) Debug.Log("Client got disconnected from server");
  95. GenericEvents.Trigger($"{eventStreamName}_ServerDisconnected", "Client got disconnected from server");
  96. m_Connection = default(NetworkConnection);
  97. }
  98. }
  99. }
  100. }
  101. }