Esempio n. 1
0
 public void CreatingMemoryPipe()
 {
     using (var memory = new MemoryPipe())
     {
         for (int i = 0; i < 2; i++)
         {
             using (var writeStream = memory.GetWriteStream(null))
                 using (var writer = new StreamWriter(writeStream))
                 {
                     writer.Write("Hello World");
                 }
             // Getting another write stream should wipe the buffer
             using (var writeStream = memory.GetWriteStream(null))
                 using (var writer = new StreamWriter(writeStream))
                 {
                     writer.Write("Hello2 World2");
                 }
             using (var readStream = memory.GetReadStream(null))
                 using (var reader = new StreamReader(readStream))
                 {
                     Assert.AreEqual("Hello2 World2", reader.ReadLine());
                 }
         }
     }
 }
        public void WaitForWrite()
        {
            var pipe = new MemoryPipe();

            var count = 0;

            var t = new Thread(() =>
            {
                var bytes2 = new byte[32];
                count      = pipe.Stream2.Read(bytes2, 0, 12);
            });

            t.Start();
            Thread.Sleep(1000);

            Assert.That(count, Is.Zero);

            const string s      = "hello, world";
            var          bytes1 = Encoding.UTF8.GetBytes(s);

            pipe.Stream1.Write(bytes1, 0, bytes1.Length);

            t.Join();
            Assert.That(count, Is.EqualTo(12));
        }
        public async Task WaitForWriteAsync2()
        {
            var pipe = new MemoryPipe();

            const string s      = "hello, world";
            var          bytes1 = Encoding.UTF8.GetBytes(s);
            await pipe.Stream1.WriteAsync(bytes1, 0, bytes1.Length);

            var bytes2 = new byte[32];
            await pipe.Stream2.ReadAsync(bytes2, 0, 12);

            var count = 0;

            var t = Task.Run(async() =>
            {
                count = await pipe.Stream2.ReadAsync(bytes2, 0, 12);
            });

            await Task.Delay(1000);

            Assert.That(count, Is.Zero);

            await pipe.Stream1.WriteAsync(bytes1, 0, bytes1.Length);

            await t;

            Assert.That(count, Is.EqualTo(12));
        }
        public void WriteOneReadOne()
        {
            var pipe = new MemoryPipe();

            const string s = "hello, world";

            for (var i = 0; i < 100; i++)
            {
                var bytes1 = Encoding.UTF8.GetBytes(s);
                pipe.Stream1.Write(bytes1, 0, bytes1.Length);

                var bytes2 = new byte[32];
                var count  = pipe.Stream2.Read(bytes2, 0, bytes2.Length);

                Assert.That(count, Is.EqualTo(12), $"at i={i}");
                Assert.That(Encoding.UTF8.GetString(bytes2, 0, count), Is.EqualTo(s), $"at i={i}");
            }
        }
Esempio n. 5
0
 public void OpeningMemoryPipe()
 {
     using (MemoryPipe pipe = new MemoryPipe())
     {
         var dataOut = new OpenWriteStreamFromMemoryPipe()
         {
             Name = "OpenWritePipe",
             Pipe = CreateParameter(pipe, "Pipe")
         };
         var dataIn = new OpenReadStreamFromMemoryPipe()
         {
             Name = "OpenWritePipe",
             Pipe = CreateParameter(pipe, "Pipe")
         };
         using (var writer = new StreamWriter(dataOut.Invoke()))
         {
             writer.Write("Hello World");
         }
         using (var reader = new StreamReader(dataIn.Invoke()))
         {
             Assert.AreEqual("Hello World", reader.ReadLine());
         }
     }
 }