JobifiedServerBehaviour.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. using Unity.Jobs;
  5. using Unity.Collections;
  6. using Unity.Networking.Transport;
  7. using Sirenix.OdinInspector;
  8. using KairoEngine.Core;
  9. namespace KairoEngine.Multiplayer
  10. {
  11. [HideMonoScript]
  12. public class JobifiedServerBehaviour : MonoBehaviour
  13. {
  14. [HideInInspector] public NetworkDriver m_Driver;
  15. [HideInInspector] public NativeList<NetworkConnection> m_Connections;
  16. public ushort port = 9000;
  17. public int players = 4;
  18. public bool autoStart = true;
  19. private JobHandle ServerJobHandle;
  20. [HideInInspector] private NativeQueue<JobEvent> m_eventQueue;
  21. private bool resourceLock = false;
  22. void Start ()
  23. {
  24. if(autoStart) StartServer();
  25. }
  26. public void StartServer()
  27. {
  28. resourceLock = true;
  29. m_eventQueue = new NativeQueue<JobEvent>(Allocator.Persistent);
  30. m_Connections = new NativeList<NetworkConnection>(players, Allocator.Persistent);
  31. m_Driver = NetworkDriver.Create();
  32. var endpoint = NetworkEndPoint.AnyIpv4;
  33. endpoint.Port = port;
  34. if (m_Driver.Bind(endpoint) != 0)
  35. {
  36. Debug.Log($"Failed to bind to port {port}");
  37. GenericEvents.Trigger("ServerEvent", $"Failed to bind to port {port}");
  38. }
  39. else
  40. {
  41. Debug.Log($"Server listening on port {port}");
  42. GenericEvents.Trigger("ServerEvent", $"Server listening on port {port}");
  43. m_Driver.Listen();
  44. }
  45. }
  46. public void OnDestroy()
  47. {
  48. // Make sure we run our jobs to completion before exiting.
  49. ServerJobHandle.Complete();
  50. m_Connections.Dispose();
  51. m_Driver.Dispose();
  52. m_eventQueue.Dispose();
  53. }
  54. void Update ()
  55. {
  56. if(resourceLock == false) return;
  57. ServerJobHandle.Complete();
  58. while(m_eventQueue.TryDequeue(out JobEvent jobEvent))
  59. {
  60. TriggerJobEvent(jobEvent);
  61. }
  62. var connectionJob = new ServerUpdateConnectionsJob
  63. {
  64. driver = m_Driver,
  65. connections = m_Connections,
  66. eventQueue = m_eventQueue.AsParallelWriter()
  67. };
  68. var serverUpdateJob = new ServerUpdateJob
  69. {
  70. driver = m_Driver.ToConcurrent(),
  71. connections = m_Connections.AsDeferredJobArray(),
  72. eventQueue = m_eventQueue.AsParallelWriter(),
  73. };
  74. ServerJobHandle = m_Driver.ScheduleUpdate();
  75. ServerJobHandle = connectionJob.Schedule(ServerJobHandle);
  76. ServerJobHandle = serverUpdateJob.Schedule(m_Connections, 1, ServerJobHandle);
  77. }
  78. public void TriggerJobEvent(JobEvent jobEvent)
  79. {
  80. switch (jobEvent.code)
  81. {
  82. case 1:
  83. GenericEvents.Trigger("ServerEvent", $"Client connected ");
  84. break;
  85. case 2:
  86. GenericEvents.Trigger("ServerEvent", $"Client disconnected");
  87. break;
  88. case 3:
  89. GenericEvents.Trigger("ServerEvent", $"Data received from client");
  90. break;
  91. case 4:
  92. GenericEvents.Trigger("ServerEvent", $"Error connecting to client");
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. }
  99. struct ServerUpdateConnectionsJob : IJob
  100. {
  101. public NetworkDriver driver;
  102. public NativeList<NetworkConnection> connections;
  103. public NativeQueue<JobEvent>.ParallelWriter eventQueue;
  104. public void Execute()
  105. {
  106. // CleanUpConnections
  107. for (int i = 0; i < connections.Length; i++)
  108. {
  109. if (!connections[i].IsCreated)
  110. {
  111. connections.RemoveAtSwapBack(i);
  112. --i;
  113. }
  114. }
  115. // AcceptNewConnections
  116. NetworkConnection c;
  117. while ((c = driver.Accept()) != default(NetworkConnection))
  118. {
  119. connections.Add(c);
  120. Debug.Log("Accepted a connection");
  121. eventQueue.Enqueue(new JobEvent((int)NetworkingEvents.Message.ClientConnected));
  122. }
  123. }
  124. }
  125. struct ServerUpdateJob : IJobParallelForDefer
  126. {
  127. public NetworkDriver.Concurrent driver;
  128. public NativeArray<NetworkConnection> connections;
  129. public NativeQueue<JobEvent>.ParallelWriter eventQueue;
  130. public void Execute(int index)
  131. {
  132. DataStreamReader stream;
  133. Assert.IsTrue(connections[index].IsCreated);
  134. // Receive data from clients
  135. NetworkEvent.Type cmd;
  136. while ((cmd = driver.PopEventForConnection(connections[index], out stream)) != NetworkEvent.Type.Empty)
  137. {
  138. if (cmd == NetworkEvent.Type.Data)
  139. {
  140. eventQueue.Enqueue(new JobEvent((int)NetworkingEvents.Message.ServerReceivedData));
  141. uint number = stream.ReadUInt();
  142. Debug.Log("Got " + number + " from the Client adding + 2 to it.");
  143. number +=2;
  144. DataStreamWriter writer = new DataStreamWriter();
  145. var result = driver.BeginSend(connections[index], out writer);
  146. writer.WriteUInt(number);
  147. driver.EndSend(writer);
  148. }
  149. else if (cmd == NetworkEvent.Type.Disconnect)
  150. {
  151. eventQueue.Enqueue(new JobEvent((int)NetworkingEvents.Message.ClientDisconnected));
  152. Debug.Log("Client disconnected from server");
  153. connections[index] = default(NetworkConnection);
  154. }
  155. }
  156. // Send data to clients
  157. }
  158. }
  159. }