示例#1
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);
            }
        }
    }
示例#2
0
        public void UdpC_Timeout_ShouldDisconnect()
        {
            int customTimeout = 1000;

            using (var server = new UdpNetworkDriver(new NetworkConfigParameter {
                disconnectTimeoutMS = customTimeout
            }))
                using (var client = new UdpNetworkDriver(new NetworkConfigParameter {
                    disconnectTimeoutMS = customTimeout
                }))
                {
                    ushort serverPort = 50006;

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

                    server.Listen();

                    var id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);
                    Assert.AreEqual(id, serverConnection);

                    // Force timeout
                    Thread.Sleep(customTimeout + 500);

                    var message = new DataStreamWriter(7, Allocator.Persistent);
                    message.Write((byte)'m');
                    message.Write((byte)'e');
                    message.Write((byte)'s');
                    message.Write((byte)'s');
                    message.Write((byte)'a');
                    message.Write((byte)'g');
                    message.Write((byte)'e');
                    server.Send(NetworkPipeline.Null, clientConnection, message);

                    var data = new byte[1472];
                    int size = -1;
                    NetworkConnection from;
                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));
                    Assert.AreEqual(from, clientConnection);
                }
        }
示例#3
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]);
                }
            }
        }
示例#4
0
        public void Update()
        {
            if (!m_Socket.Listening && !conn.IsCreated)
            {
                conn = m_Socket.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), (ushort)serverPort));
            }
            else if (!m_Socket.Listening && dataCounter == 0 && !conn.IsCreated)
            {
                using (var message = new DataStreamWriter(7, Allocator.Persistent))
                {
                    message.Write((byte)'m');
                    message.Write((byte)'e');
                    message.Write((byte)'s');
                    message.Write((byte)'s');
                    message.Write((byte)'a');
                    message.Write((byte)'g');
                    message.Write((byte)'e');

                    m_Socket.Send(NetworkPipeline.Null, conn, message);
                }
            }
            else if (!m_Socket.Listening && conn.IsCreated &&
                     UnityEngine.Random.Range(0, 1000) < 10)
            {
                m_Socket.Disconnect(conn);
                conn = default(NetworkConnection);
            }

            NetworkConnection connection;
            DataStreamReader  reader;
            var context = default(DataStreamReader.Context);
            var ev      = m_Socket.PopEvent(out connection, out reader);

            if (ev == ExperimentalEventType.Empty)
            {
                return;
            }

            using (var writer = new DataStreamWriter(reader.Length, Allocator.Temp))
            {
                unsafe
                {
                    reader.ReadBytes(ref context, writer.GetUnsafePtr(), reader.Length);
                }
                switch (ev)
                {
                case ExperimentalEventType.Connect:
                    connectCounter++;
                    break;

                case ExperimentalEventType.Disconnect:
                    conn = default(NetworkConnection);
                    disconnectCounter++;
                    break;

                case ExperimentalEventType.Data:
                    dataCounter++;
                    m_Socket.Send(NetworkPipeline.Null, connection, writer);
                    break;
                }
            }
        }