private void ClientReceivePackets(GenericNetworkDriver <IPCSocket, TestNetworkPipelineStageCollection> clientDriver, int packetCount, NetworkConnection clientToServer, long startTime, int minDelay)
        {
            DataStreamReader readStrm;

            NetworkEvent.Type netEvent;
            var abortTimer = new Timer();

            while (true)
            {
                if (abortTimer.ElapsedMilliseconds > 2000)
                {
                    Assert.Fail("Did not receive first delayed packet");
                }
                netEvent = clientToServer.PopEvent(clientDriver, out readStrm);
                if (netEvent == NetworkEvent.Type.Data)
                {
                    break;
                }
                m_ServerDriver.ScheduleUpdate().Complete();
                clientDriver.ScheduleUpdate().Complete();
            }

            // All delayed packets (from first patch) should be poppable now
            for (int i = 0; i < packetCount; i++)
            {
                var delay = Stopwatch.GetTimestamp() / TimeSpan.TicksPerMillisecond - startTime;
                Assert.AreEqual(NetworkEvent.Type.Data, netEvent);
                Assert.That(delay >= minDelay, "Delay too low on packet " + i + ": " + delay);
                Assert.AreEqual(64, readStrm.Length);
                var readCtx = default(DataStreamReader.Context);
                for (int j = 0; j < 16; j++)
                {
                    var read = readStrm.ReadInt(ref readCtx);
                    Assert.AreEqual(i + 1, read);
                    Assert.True(read > 0 && read <= packetCount, "read incorrect value: " + read);
                }

                // Test done when all packets have been verified
                if (i == packetCount - 1)
                {
                    break;
                }

                // It could be not all patch of packets were processed in one update (depending on how the timers land)
                abortTimer = new Timer();
                while ((netEvent = clientToServer.PopEvent(clientDriver, out readStrm)) == NetworkEvent.Type.Empty)
                {
                    if (abortTimer.ElapsedMilliseconds > 1000)
                    {
                        Assert.Fail("Didn't receive all delayed packets");
                    }
                    clientDriver.ScheduleUpdate().Complete();
                    m_ServerDriver.ScheduleUpdate().Complete();
                }
            }
        }
        private void TestPipeline(int packetCount, NetworkPipeline serverPipe, int packetDelay = 100)
        {
            // Connect to server
            var clientToServer  = m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint());
            var clientToServer2 = m_ClientDriver2.Connect(m_ServerDriver.LocalEndPoint());

            Assert.AreNotEqual(default(NetworkConnection), clientToServer);
            Assert.AreNotEqual(default(NetworkConnection), clientToServer2);
            m_ClientDriver.ScheduleUpdate().Complete();
            m_ClientDriver2.ScheduleUpdate().Complete();

            // Driver only updates time in update, so must read start time before update
            var startTime = Stopwatch.GetTimestamp() / TimeSpan.TicksPerMillisecond;

            // Handle incoming connection from client
            m_ServerDriver.ScheduleUpdate().Complete();
            var serverToClient = m_ServerDriver.Accept();

            Assert.AreNotEqual(default(NetworkConnection), serverToClient);
            var serverToClient2 = m_ServerDriver.Accept();

            Assert.AreNotEqual(default(NetworkConnection), serverToClient2);

            // Send given packetCount number of packets in a row in one update
            // Write 1's for packet 1, 2's for packet 2 and so on and verify they're received in same order
            var strm = new DataStreamWriter(64, Allocator.Temp);

            for (int i = 0; i < packetCount; i++)
            {
                strm.Clear();
                for (int j = 0; j < 16; j++)
                {
                    strm.Write((int)i + 1);
                }
                m_ServerDriver.Send(serverPipe, serverToClient, strm);
                m_ServerDriver.Send(serverPipe, serverToClient2, strm);
            }

            m_ServerDriver.ScheduleUpdate().Complete();

            // Receive incoming message from server
            m_ClientDriver.ScheduleUpdate().Complete();
            m_ClientDriver2.ScheduleUpdate().Complete();
            DataStreamReader readStrm;

            Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver2, out readStrm));

            ClientReceivePackets(m_ClientDriver, packetCount, clientToServer, startTime, packetDelay);
            ClientReceivePackets(m_ClientDriver2, packetCount, clientToServer2, startTime, packetDelay);
        }
        public void NetworkPipeline_CanExtendHeader()
        {
            // Create pipeline
            var clientPipe = m_ClientDriver.CreatePipeline(typeof(TestPipelineStageWithHeader));
            var serverPipe = m_ServerDriver.CreatePipeline(typeof(TestPipelineStageWithHeader));

            Assert.AreEqual(clientPipe, serverPipe);

            // Connect to server
            var clientToServer = m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint());

            Assert.AreNotEqual(default(NetworkConnection), clientToServer);
            m_ClientDriver.ScheduleUpdate().Complete();

            // Handle incoming connection from client
            m_ServerDriver.ScheduleUpdate().Complete();
            var serverToClient = m_ServerDriver.Accept();

            Assert.AreNotEqual(default(NetworkConnection), serverToClient);

            // Send message to client
            var strm = new DataStreamWriter(4, Allocator.Temp);

            strm.Write((int)42);
            m_ServerDriver.Send(serverPipe, serverToClient, strm);
            m_ServerDriver.ScheduleUpdate().Complete();

            // Receive incoming message from server
            m_ClientDriver.ScheduleUpdate().Complete();
            DataStreamReader readStrm;

            Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(NetworkEvent.Type.Data, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(4, readStrm.Length);
            var readCtx = default(DataStreamReader.Context);

            Assert.AreEqual(42, readStrm.ReadInt(ref readCtx));
        }
Exemple #4
0
 public JobHandle ScheduleUpdate(JobHandle dep = default(JobHandle))
 {
     return(m_genericDriver.ScheduleUpdate(dep));
 }