Exemplo n.º 1
0
        void ConnectTogether(UdpNetworkDriver server, UdpNetworkDriver client, int maxIterations, out NetworkConnection serverConnection, out NetworkConnection clientConnection)
        {
            int servers = 0, clients = 0, iterations = 0;

            serverConnection = default(NetworkConnection);
            clientConnection = default(NetworkConnection);

            DataStreamReader reader;

            NetworkConnection poppedConnection = default(NetworkConnection);

            while (clients != 1 || servers != 1)
            {
                Assert.Less(iterations++, maxIterations);

                server.ScheduleUpdate().Complete();

                var newConnection = server.Accept();
                if (newConnection != default(NetworkConnection))
                {
                    clients++;
                    clientConnection = newConnection;
                }

                if (client.PopEvent(out poppedConnection, out reader) == ExperimentalEventType.Connect)
                {
                    serverConnection = poppedConnection;
                    servers++;
                }

                client.ScheduleUpdate().Complete();
                Assert.AreNotEqual(clientConnection, default(NetworkConnection));
            }
        }
Exemplo n.º 2
0
        ExperimentalEventType PollEvent(ExperimentalEventType ev, int maxIterations, UdpNetworkDriver socket, ref byte[] buffer, out int size, out NetworkConnection connection)
        {
            int iterator = 0;

            size       = 0;
            connection = default(NetworkConnection);

            while (iterator++ < maxIterations)
            {
                DataStreamReader reader;
                var context = default(DataStreamReader.Context);
                ExperimentalEventType e;
                if ((e = socket.PopEvent(out connection, out reader)) == ev)
                {
                    if (reader.IsCreated)
                    {
                        reader.ReadBytesIntoArray(ref context, ref buffer, reader.Length);
                        size = reader.Length;
                    }
                    return(e);
                }
                socket.ScheduleUpdate().Complete();
            }
            return(ExperimentalEventType.Empty);
        }
Exemplo n.º 3
0
    private void Update()
    {
        Driver.ScheduleUpdate().Complete();

        if (!Connection.IsCreated)
        {
            return;
        }

        NetworkEvent.Type cmd;

        while ((cmd = Connection.PopEvent(Driver, out DataStreamReader stream)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Connect)
            {
                Debug.Log("We are now connected to the server");

                //using (DataStreamWriter writer = new DataStreamWriter(4, Allocator.Temp)) {
                //    Connection.Send(Driver, writer);
                //}
            }
            else if (cmd == NetworkEvent.Type.Data)
            {
                DataStreamReader.Context context = default;
                float   x     = stream.ReadFloat(ref context);
                float   y     = stream.ReadFloat(ref context);
                float   z     = stream.ReadFloat(ref context);
                Vector3 point = new Vector3(x, y, z);
                if (Physics.Raycast(Camera.main.ScreenPointToRay(point), out RaycastHit hit, 1 << LayerMask.NameToLayer("Cell")))
                {
                    Cell cell = hit.collider.GetComponent <Cell>();
                    if (cell == null)
                    {
                        return;
                    }
                    cell.Reveal(Color.blue);
                }
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Client got disconnected from server");
                Connection = default;
            }
        }

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            using (DataStreamWriter writer = new DataStreamWriter(12, Allocator.Temp)) {
                writer.Write(Input.mousePosition.x);
                writer.Write(Input.mousePosition.y);
                writer.Write(Input.mousePosition.z);
                Driver.Send(NetworkPipeline.Null, Connection, writer);
            }
        }
    }
Exemplo n.º 4
0
        public void UdpC_ReconnectAndResend_ShouldReconnectAndResend()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                {
                    ushort serverPort = 50007;

                    server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    client.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                    server.Listen();

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    var id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);

                    client.Disconnect(id);

                    server.ScheduleUpdate().Complete();

                    var data = new byte[1472];
                    var size = 1472;
                    NetworkConnection from;

                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));

                    id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);

                    var message = new byte[]
                    {
                        (byte)'m',
                        (byte)'e',
                        (byte)'s',
                        (byte)'s',
                        (byte)'a',
                        (byte)'g',
                        (byte)'e'
                    };

                    SendReceive(client, server, clientConnection, serverConnection, message, maxIterations);
                }
        }
Exemplo n.º 5
0
        void SendReceive(UdpNetworkDriver sender, UdpNetworkDriver receiver, NetworkConnection from, NetworkConnection to, byte[] data, int maxIterations)
        {
            using (var writer = new DataStreamWriter(data.Length, Allocator.Persistent))
            {
                writer.Write(data, data.Length);
                sender.Send(NetworkPipeline.Null, to, writer);

                sender.ScheduleUpdate().Complete();
                receiver.ScheduleUpdate().Complete();

                var buffer = new byte[1472];
                int size   = 0;
                NetworkConnection connection;
                PollEvent(ExperimentalEventType.Data, maxIterations, receiver, ref buffer, out size, out connection);

                Assert.AreEqual(from, connection);
                Assert.AreEqual(data.Length, size);

                for (int i = 0; i < data.Length; i++)
                {
                    Assert.AreEqual(data[i], buffer[i]);
                }
            }
        }
Exemplo n.º 6
0
    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);
            Debug.Log("Accepted a connection");
        }

        float x = 0, y = 0, z = 0;

        for (int i = 0; i < Connections.Length; i++)
        {
            if (!Connections[i].IsCreated)
            {
                Assert.IsTrue(true);
            }
            NetworkEvent.Type cmd;

            while ((cmd = Driver.PopEventForConnection(Connections[i], out DataStreamReader stream)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    DataStreamReader.Context context = default;
                    x = stream.ReadFloat(ref context);
                    y = stream.ReadFloat(ref context);
                    z = stream.ReadFloat(ref context);
                    Vector3 point = new Vector3(x, y, z);
                    if (Physics.Raycast(Camera.main.ScreenPointToRay(point), out RaycastHit hit, 1 << LayerMask.NameToLayer("Cell")))
                    {
                        Cell cell = hit.collider.GetComponent <Cell>();
                        if (cell == null)
                        {
                            return;
                        }
                        cell.Reveal(Color.blue);
                    }
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Client disconnected from server");
                    Connections[i] = default;
                }
            }
        }

        using (var writer = new DataStreamWriter(12, Allocator.Temp)) {
            writer.Write(x);
            writer.Write(y);
            writer.Write(z);
            for (int i = 0; i < Connections.Length; ++i)
            {
                Connections[i].Send(Driver, writer);
            }
        }
    }