Esempio n. 1
0
        public void VirtualNetwork_MultiThreadIntegrityTest_Ok()
        {
            var rnd = new Random();

            var network = new VirtualNetwork();
            var checksums = new ConcurrentDictionary<int, uint>();

            Parallel.For(0, 100000, async (int i) =>
            {
                int bufferSize = rnd.Next(5, 2048);

                byte[] writeFrame = new byte[bufferSize];
                rnd.NextBytes(writeFrame);
                
                // First 4 bytes represent the sequence number.
                byte [] sequenceNo = BitConverter.GetBytes(i);
                sequenceNo.CopyTo(writeFrame, 0);

                uint writeChecksum = Fletcher32.Checksum(writeFrame, 0, writeFrame.Length);
                checksums.AddOrUpdate(i, writeChecksum, (seq, checkSum) => { Debug.Fail("Attempt to update checksum."); return 0; });

                network.WriteFrame(i % 2 == 0, writeFrame);

                int delayMilliseconds = rnd.Next(0, 1000);
                await Task.Delay(delayMilliseconds);

                byte[] readFrame;
                network.ReadFrame(i % 2 == 1, out readFrame);

                uint readChecksum = Fletcher32.Checksum(readFrame, 0, readFrame.Length);

                int idx = BitConverter.ToInt32(readFrame, 0);
                Assert.Equal(checksums[idx], readChecksum);
            });
        }
        public void VirtualNetwork_MultiThreadIntegrityTest_Ok()
        {
            var rnd = new Random();

            var network   = new VirtualNetwork();
            var checksums = new ConcurrentDictionary <int, uint>();

            Parallel.For(0, 100000, async(int i) =>
            {
                int bufferSize = rnd.Next(5, 2048);

                byte[] writeFrame = new byte[bufferSize];
                rnd.NextBytes(writeFrame);

                // First 4 bytes represent the sequence number.
                byte [] sequenceNo = BitConverter.GetBytes(i);
                sequenceNo.CopyTo(writeFrame, 0);

                uint writeChecksum = Fletcher32.Checksum(writeFrame, 0, writeFrame.Length);
                checksums.AddOrUpdate(i, writeChecksum, (seq, checkSum) => { Debug.Fail("Attempt to update checksum."); return(0); });

                network.WriteFrame(i % 2 == 0, writeFrame);

                int delayMilliseconds = rnd.Next(0, 1000);
                await Task.Delay(delayMilliseconds);

                byte[] readFrame = network.ReadFrame(i % 2 == 1);

                uint readChecksum = Fletcher32.Checksum(readFrame, 0, readFrame.Length);

                int idx = BitConverter.ToInt32(readFrame, 0);
                Assert.Equal(checksums[idx], readChecksum);
            });
        }
Esempio n. 3
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            lock (_readStreamLock)
            {
                if (_readStream == null || (_readStream.Position >= _readStream.Length))
                {
                    byte[] innerBuffer;

                    _network.ReadFrame(_isServer, out innerBuffer);
                    _readStream = new MemoryStream(innerBuffer);
                }

                return(_readStream.Read(buffer, offset, count));
            }
        }
        public void VirtualNetwork_SingleThreadIntegrityTest_Ok()
        {
            var rnd = new Random();

            var network = new VirtualNetwork();

            for (int i = 0; i < 100000; i++)
            {
                int bufferSize = rnd.Next(1, 2048);

                byte [] writeFrame = new byte[bufferSize];
                rnd.NextBytes(writeFrame);
                uint writeChecksum = Fletcher32.Checksum(writeFrame, 0, writeFrame.Length);

                network.WriteFrame(i % 2 == 0, writeFrame);

                byte [] readFrame = network.ReadFrame(i % 2 == 1);

                uint readChecksum = Fletcher32.Checksum(readFrame, 0, readFrame.Length);

                Assert.Equal(writeChecksum, readChecksum);
            }
        }
Esempio n. 5
0
        public void VirtualNetwork_SingleThreadIntegrityTest_Ok()
        {
            var rnd = new Random();

            var network = new VirtualNetwork();

            for (int i = 0; i < 100000; i++)
            {
                int bufferSize = rnd.Next(1, 2048);

                byte [] writeFrame = new byte[bufferSize];
                rnd.NextBytes(writeFrame);
                uint writeChecksum = Fletcher32.Checksum(writeFrame, 0, writeFrame.Length);

                network.WriteFrame(i % 2 == 0, writeFrame);

                byte [] readFrame;
                network.ReadFrame(i % 2 == 1, out readFrame);

                uint readChecksum = Fletcher32.Checksum(readFrame, 0, readFrame.Length);

                Assert.Equal(writeChecksum, readChecksum);
            }
        }