Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using Unity.Networking.Transport;
- using Unity.Collections;
- using UdpCNetworkDriver = Unity.Networking.Transport.UdpNetworkDriver;
- public class Server : MonoBehaviour {
- private UdpCNetworkDriver driver = default;
- private NativeList<NetworkConnection> connections = default;
- private uint turn = 0;
- private static int cellID = -1;
- private static Color cellColor = Color.white;
- private void Start() {
- driver = new UdpCNetworkDriver(new INetworkParameter[0]);
- if (driver.Bind(NetworkEndPoint.Parse("0.0.0.0", 9000)) != 0) {
- ServerLog.Log("Failed to bind to port ...");
- }
- else {
- driver.Listen();
- }
- connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
- }
- private void Update() {
- driver.ScheduleUpdate().Complete();
- for (int i = 0; i < connections.Length; i++) {
- if (!connections[i].IsCreated) {
- connections.RemoveAtSwapBack(i);
- --i;
- }
- }
- NetworkConnection c;
- while ((c = driver.Accept()) != default) {
- connections.Add(c);
- ServerLog.Log("Accepted a connection");
- }
- DataStreamReader stream;
- uint currentAmountOfConnections = 0;
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i] == default) continue;
- currentAmountOfConnections++;
- }
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i] == default) continue;
- uint id = (uint)i + 1;
- NetworkEvent.Type cmd;
- while ((cmd = driver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty) {
- if (cmd == NetworkEvent.Type.Data) {
- DataStreamReader.Context readerCtx = default;
- bool leftMouse = stream.ReadUInt(ref readerCtx) == 1;
- bool rightMouse = stream.ReadUInt(ref readerCtx) == 1;
- float xO = stream.ReadFloat(ref readerCtx);
- float yO = stream.ReadFloat(ref readerCtx);
- float zO = stream.ReadFloat(ref readerCtx);
- float xD = stream.ReadFloat(ref readerCtx);
- float yD = stream.ReadFloat(ref readerCtx);
- float zD = stream.ReadFloat(ref readerCtx);
- uint pressedKey = 0;
- if (i == turn % currentAmountOfConnections) {
- Ray ray = new Ray(new Vector3(xO, yO, zO), new Vector3(xD, yD, zD));
- if (Physics.Raycast(ray, out RaycastHit hit)) {
- cellID = hit.collider.GetComponent<Cell>().ID;
- if (leftMouse) {
- pressedKey = 1;
- turn++;
- }
- else if (rightMouse) {
- switch (id) {
- case 1: cellColor = Color.blue; break;
- case 2: cellColor = Color.red; break;
- case 3: cellColor = Color.green; break;
- case 4: cellColor = Color.yellow; break;
- default: cellColor = Color.black; break;
- }
- pressedKey = 2;
- turn++;
- }
- }
- ServerLog.Log("Turn {1}: Client{0}'s turn", id, turn);
- }
- using (var writer = new DataStreamWriter(28, Allocator.Temp)) {
- writer.Write(id);
- writer.Write(cellID);
- writer.Write(pressedKey);
- writer.Write(cellColor.r);
- writer.Write(cellColor.g);
- writer.Write(cellColor.b);
- writer.Write(cellColor.a);
- connections[i].Send(driver, writer);
- }
- }
- else if (cmd == NetworkEvent.Type.Disconnect) {
- ServerLog.Log("Client{0} has disconnected", id);
- }
- }
- }
- }
- public void OnDestroy() {
- driver.Dispose();
- connections.Dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement