Exemplo n.º 1
0
        internal static int GetMaxLength(LengthPrefixStyle lps, int length)
        {
            switch (lps)
            {
            case LengthPrefixStyle.Int8:
                return(Math.Min(length, sbyte.MaxValue));

            case LengthPrefixStyle.Int16:
                return(Math.Min(length, short.MaxValue));

            case LengthPrefixStyle.Int32:
            case LengthPrefixStyle.Int64:
            case LengthPrefixStyle.Varint:
                return(Math.Min(length, int.MaxValue));

            case LengthPrefixStyle.UInt8:
                return(Math.Min(length, byte.MaxValue));

            case LengthPrefixStyle.UInt16:
                return(Math.Min(length, ushort.MaxValue));

            case LengthPrefixStyle.UInt32:
            case LengthPrefixStyle.UInt64:
            case LengthPrefixStyle.UVarint:
                return(Math.Min(length, int.MaxValue));

            default:
                throw new ArgumentOutOfRangeException(nameof(lps), lps, null);
            }
        }
Exemplo n.º 2
0
 public async Task TestReadWriteAsync(LengthPrefixStyle lps)
 {
     using (var mem = new MemoryStream())
         using (var stream = new MessageStream(mem, true, lps))
         {
             await SubtestReadWriteAsync(stream, lps, () => stream?.Seek(0, SeekOrigin.Begin));
         }
 }
Exemplo n.º 3
0
 public void TestReadWrite(LengthPrefixStyle lps)
 {
     using (var mem = new MemoryStream())
         using (var stream = new MessageStream(mem, true, lps))
         {
             SubtestReadWrite(stream, lps, () => stream?.Seek(0, SeekOrigin.Begin));
         }
 }
Exemplo n.º 4
0
        public MessageStream(Stream stream, bool leaveOpen = false, LengthPrefixStyle lps = LengthPrefixStyle.Varint,
                             Endianess endianess           = Endianess.Big)
        {
            _stream    = stream;
            _leaveOpen = leaveOpen;

            _reader = _stream.CanRead ? new MessageReader(_stream, leaveOpen, lps, endianess) : null;
            _writer = _stream.CanWrite ? new MessageWriter(_stream, leaveOpen, lps, endianess) : null;
        }
Exemplo n.º 5
0
 public MessageWriter(Stream stream, bool leaveOpen = false, LengthPrefixStyle lps = LengthPrefixStyle.Varint,
                      Endianess endianess           = Endianess.Big)
 {
     _stream    = stream;
     _leaveOpen = leaveOpen;
     _lps       = lps;
     _endianess = endianess;
     _semaphore = new SemaphoreSlim(1, 1);
     _codec     = (_lps != LengthPrefixStyle.UVarint && _lps != LengthPrefixStyle.Varint) ? GetCodec() : null;
 }
Exemplo n.º 6
0
 public void TestReadWriteRandomLengths(LengthPrefixStyle lps)
 {
     using (var stream = new MemoryStream())
     {
         using (var writer = new MessageWriter(stream, true, lps))
             using (var reader = new MessageReader(stream, true, lps))
             {
                 SubtestReadWriteRandomLengths(writer, reader, lps, () => stream?.Seek(0, SeekOrigin.Begin));
             }
     }
 }
Exemplo n.º 7
0
 public async Task TestReadWriteMessageAsync(LengthPrefixStyle lps)
 {
     using (var stream = new MemoryStream())
     {
         using (var writer = new MessageWriter(stream, true, lps))
             using (var reader = new MessageReader(stream, true, lps))
             {
                 await SubtestReadWriteMessageAsync(writer, reader, lps, () => stream?.Seek(0, SeekOrigin.Begin));
             }
     }
 }
Exemplo n.º 8
0
 public void TestReadWrite(LengthPrefixStyle lps)
 {
     using (var stream = new MemoryStream())
     {
         using (var writer = new MessageWriter(stream, true, lps))
             using (var reader = new MessageReader(stream, true, lps))
             {
                 SubtestReadWrite(writer, reader, lps, () =>
                 {
                     stream?.Flush();
                     stream?.ToArray();
                     stream?.Seek(0, SeekOrigin.Begin);
                 });
             }
     }
 }
Exemplo n.º 9
0
        public void CombineStreamPair_TestReadWriteMessage(LengthPrefixStyle lps)
        {
            Stream a = null, b = null;

            try
            {
                MakeTcpStreamPair(out a, out b);
                using (var stream = MessageStream.Combine(new MessageReader(a, true, lps), new MessageWriter(b, true, lps)))
                {
                    SubtestReadWriteMessage(stream, lps, () => {});
                }
            }
            finally
            {
                a?.Dispose();
                b?.Dispose();
            }
        }
Exemplo n.º 10
0
        private static void SubtestReadWriteMessage(MessageStream stream, LengthPrefixStyle lps, Action reset)
        {
            var rand = new Random(Environment.TickCount);
            var msgs = Enumerable.Range(0, 64).Select(_ =>
            {
                var msg = new byte[ReaderWriterTests.GetMaxLength(lps, rand.Next(16, 1024))];
                rand.NextBytes(msg);
                return(msg);
            }).ToList();

            msgs.ForEach(stream.WriteMessage);

            reset();

            foreach (var msg in msgs)
            {
                var msg2 = stream.ReadMessage();

                Assert.That(msg2, Is.EqualTo(msg));
            }
        }
Exemplo n.º 11
0
        private static void SubtestReadWrite(MessageWriter w, MessageReader r, LengthPrefixStyle lps, Action reset)
        {
            var rand = new Random(Environment.TickCount);
            var msgs = Enumerable.Range(0, 64).Select(_ =>
            {
                var msg = new byte[GetMaxLength(lps, rand.Next(16, 1024))];
                rand.NextBytes(msg);
                return(msg);
            }).ToList();

            msgs.ForEach(msg => w.Write(msg, 0, msg.Length));

            reset();

            foreach (var msg in msgs)
            {
                var msg2 = new byte[msg.Length];
                r.Read(msg2, 0, msg2.Length);

                Assert.That(msg2, Is.EqualTo(msg));
            }
        }
Exemplo n.º 12
0
        private static async Task SubtestReadWriteMessageAsync(MessageStream stream, LengthPrefixStyle lps, Action reset)
        {
            var rand = new Random(Environment.TickCount);
            var msgs = Enumerable.Range(0, 64).Select(_ =>
            {
                var msg = new byte[ReaderWriterTests.GetMaxLength(lps, rand.Next(16, 1024))];
                rand.NextBytes(msg);
                return(msg);
            }).ToList();

            var tasks = msgs.Select(msg => stream.WriteMessageAsync(msg, CancellationToken.None));
            await Task.WhenAll(tasks);

            reset();

            foreach (var msg in msgs)
            {
                var msg2 = await stream.ReadMessageAsync(CancellationToken.None);

                Assert.That(msg2, Is.EqualTo(msg));
            }
        }
Exemplo n.º 13
0
        private static void SubtestReadWriteRandomLengths(MessageWriter w, MessageReader r, LengthPrefixStyle lps, Action reset)
        {
            var rand = new Random(Environment.TickCount);
            var msgs = Enumerable.Range(0, 32).Select(_ =>
            {
                var msg = new byte[GetMaxLength(lps, rand.Next(16, 1024))];
                rand.NextBytes(msg);
                return(msg);
            }).ToList();

            msgs.ForEach(msg => w.Write(msg, 0, msg.Length));

            reset();

            var expected = msgs.SelectMany(msg => msg).ToArray();

            var result = new byte[expected.Length];
            var total  = 0;

            while (total < expected.Length)
            {
                var bytesRead = r.Read(result, total, rand.Next(4, 64));
                if (bytesRead < 1)
                {
                    break;
                }

                total += bytesRead;
            }

            Assert.That(result, Is.EqualTo(expected));
        }
Exemplo n.º 14
0
        private static async Task SubtestReadWriteRandomLengthsAsync(MessageWriter w, MessageReader r, LengthPrefixStyle lps, Action reset)
        {
            var rand = new Random(Environment.TickCount);
            var msgs = Enumerable.Range(0, 32).Select(_ =>
            {
                var msg = new byte[GetMaxLength(lps, rand.Next(16, 1024))];
                rand.NextBytes(msg);
                return(msg);
            }).ToList();

            var tasks = msgs.Select(msg => w.WriteAsync(msg, 0, msg.Length, CancellationToken.None));
            await Task.WhenAll(tasks).ConfigureAwait(false);

            reset();

            var expected = msgs.SelectMany(msg => msg).ToArray();

            var result = new byte[expected.Length];
            var total  = 0;

            while (total < expected.Length)
            {
                var bytesRead = await r.ReadAsync(result, total, rand.Next(4, 64), CancellationToken.None).ConfigureAwait(false);

                if (bytesRead < 1)
                {
                    break;
                }

                total += bytesRead;
            }

            Assert.That(result, Is.EqualTo(expected));
        }