Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using UnityEngine;
  2. using Unity.Networking.Transport;
  3. using Unity.Collections;
  4.  
  5. using UdpCNetworkDriver = Unity.Networking.Transport.UdpNetworkDriver;
  6.  
  7. public class Server : MonoBehaviour {
  8. private UdpCNetworkDriver driver = default;
  9. private NativeList<NetworkConnection> connections = default;
  10.  
  11. private uint turn = 0;
  12. private static int cellID = -1;
  13. private static Color cellColor = Color.white;
  14.  
  15. private void Start() {
  16. driver = new UdpCNetworkDriver(new INetworkParameter[0]);
  17. if (driver.Bind(NetworkEndPoint.Parse("0.0.0.0", 9000)) != 0) {
  18. ServerLog.Log("Failed to bind to port ...");
  19. }
  20. else {
  21. driver.Listen();
  22. }
  23.  
  24. connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
  25. }
  26.  
  27. private void Update() {
  28. driver.ScheduleUpdate().Complete();
  29.  
  30. for (int i = 0; i < connections.Length; i++) {
  31. if (!connections[i].IsCreated) {
  32. connections.RemoveAtSwapBack(i);
  33. --i;
  34. }
  35. }
  36.  
  37. NetworkConnection c;
  38. while ((c = driver.Accept()) != default) {
  39. connections.Add(c);
  40. ServerLog.Log("Accepted a connection");
  41. }
  42.  
  43. DataStreamReader stream;
  44. uint currentAmountOfConnections = 0;
  45. for (int i = 0; i < connections.Length; i++) {
  46. if (connections[i] == default) continue;
  47. currentAmountOfConnections++;
  48. }
  49.  
  50. for (int i = 0; i < connections.Length; i++) {
  51. if (connections[i] == default) continue;
  52. uint id = (uint)i + 1;
  53.  
  54. NetworkEvent.Type cmd;
  55. while ((cmd = driver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty) {
  56. if (cmd == NetworkEvent.Type.Data) {
  57. DataStreamReader.Context readerCtx = default;
  58. bool leftMouse = stream.ReadUInt(ref readerCtx) == 1;
  59. bool rightMouse = stream.ReadUInt(ref readerCtx) == 1;
  60. float xO = stream.ReadFloat(ref readerCtx);
  61. float yO = stream.ReadFloat(ref readerCtx);
  62. float zO = stream.ReadFloat(ref readerCtx);
  63. float xD = stream.ReadFloat(ref readerCtx);
  64. float yD = stream.ReadFloat(ref readerCtx);
  65. float zD = stream.ReadFloat(ref readerCtx);
  66.  
  67. uint pressedKey = 0;
  68.  
  69. if (i == turn % currentAmountOfConnections) {
  70. Ray ray = new Ray(new Vector3(xO, yO, zO), new Vector3(xD, yD, zD));
  71. if (Physics.Raycast(ray, out RaycastHit hit)) {
  72. cellID = hit.collider.GetComponent<Cell>().ID;
  73. if (leftMouse) {
  74. pressedKey = 1;
  75. turn++;
  76. }
  77. else if (rightMouse) {
  78. switch (id) {
  79. case 1: cellColor = Color.blue; break;
  80. case 2: cellColor = Color.red; break;
  81. case 3: cellColor = Color.green; break;
  82. case 4: cellColor = Color.yellow; break;
  83. default: cellColor = Color.black; break;
  84. }
  85. pressedKey = 2;
  86. turn++;
  87. }
  88. }
  89. ServerLog.Log("Turn {1}: Client{0}'s turn", id, turn);
  90. }
  91.  
  92. using (var writer = new DataStreamWriter(28, Allocator.Temp)) {
  93. writer.Write(id);
  94. writer.Write(cellID);
  95. writer.Write(pressedKey);
  96. writer.Write(cellColor.r);
  97. writer.Write(cellColor.g);
  98. writer.Write(cellColor.b);
  99. writer.Write(cellColor.a);
  100. connections[i].Send(driver, writer);
  101. }
  102. }
  103. else if (cmd == NetworkEvent.Type.Disconnect) {
  104. ServerLog.Log("Client{0} has disconnected", id);
  105. }
  106. }
  107. }
  108. }
  109.  
  110. public void OnDestroy() {
  111. driver.Dispose();
  112. connections.Dispose();
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement