Ejemplo n.º 1
0
 protected override ServerClientPair CreateServerClientPair()
 {
     ServerClientPair ret = new ServerClientPair();
     ret.writeablePipe = new AnonymousPipeServerStream(PipeDirection.Out);
     ret.readablePipe = new AnonymousPipeClientStream(PipeDirection.In, ((AnonymousPipeServerStream)ret.writeablePipe).ClientSafePipeHandle);
     return ret;
 }
Ejemplo n.º 2
0
        public void ReadWithNegativeOffset_Throws_ArgumentOutOfRangeException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanRead);

                // Offset must be nonnegative
                Assert.Throws <ArgumentOutOfRangeException>("offset", () => pipe.Read(new byte[6], -1, 1));
                Assert.Throws <ArgumentOutOfRangeException>("offset", () => { pipe.ReadAsync(new byte[4], -1, 1); });
            }
        }
Ejemplo n.º 3
0
        public virtual void WriteToPipeWithClosedPartner_Throws_IOException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                pair.readablePipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <IOException>(() => pair.writeablePipe.Write(buffer, 0, buffer.Length));
                Assert.Throws <IOException>(() => pair.writeablePipe.WriteByte(123));
                Assert.Throws <IOException>(() => { pair.writeablePipe.WriteAsync(buffer, 0, buffer.Length); });
                Assert.Throws <IOException>(() => pair.writeablePipe.Flush());
            }
        }
Ejemplo n.º 4
0
        public void ReadWithNegativeCount_Throws_ArgumentOutOfRangeException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanRead);

                // Count must be nonnegative
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => pipe.Read(new byte[3], 0, -1));
                AssertExtensions.Throws <System.ArgumentOutOfRangeException>("count", () => { pipe.ReadAsync(new byte[7], 0, -1); });
            }
        }
Ejemplo n.º 5
0
        public async Task WriteZeroLengthBuffer_Nop()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;

                // Shouldn't throw
                pipe.Write(Array.Empty <byte>(), 0, 0);

                Task  writeTask = pipe.WriteAsync(Array.Empty <byte>(), 0, 0);
                await writeTask;
            }
        }
Ejemplo n.º 6
0
 public void CopyToAsync_InvalidArgs_Throws()
 {
     using (ServerClientPair pair = CreateServerClientPair())
     {
         AssertExtensions.Throws <ArgumentNullException>("destination", () => { pair.readablePipe.CopyToAsync(null); });
         AssertExtensions.Throws <ArgumentOutOfRangeException>("bufferSize", () => { pair.readablePipe.CopyToAsync(new MemoryStream(), 0); });
         Assert.Throws <NotSupportedException>(() => { pair.readablePipe.CopyToAsync(new MemoryStream(new byte[1], writable: false)); });
         if (!pair.writeablePipe.CanRead)
         {
             Assert.Throws <NotSupportedException>(() => { pair.writeablePipe.CopyToAsync(new MemoryStream()); });
         }
     }
 }
Ejemplo n.º 7
0
        public void WriteWithNegativeCount_Throws_ArgumentOutOfRangeException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanWrite);

                // Count must be nonnegative
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => pipe.Write(new byte[5], 0, -1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => { pipe.WriteAsync(new byte[5], 0, -1); });
            }
        }
Ejemplo n.º 8
0
        public void WriteWithNegativeOffset_Throws_ArgumentOutOfRangeException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanWrite);
                Assert.False(pipe.CanSeek);

                // Offset must be nonnegative
                Assert.Throws <ArgumentOutOfRangeException>("offset", () => pipe.Write(new byte[5], -1, 1));
                Assert.Throws <ArgumentOutOfRangeException>("offset", () => { pipe.WriteAsync(new byte[5], -1, 1); });
            }
        }
Ejemplo n.º 9
0
        public void ReadOnDisposedReadablePipe_Throws_ObjectDisposedException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                pipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <ObjectDisposedException>(() => pipe.Flush());
                Assert.Throws <ObjectDisposedException>(() => pipe.Read(buffer, 0, buffer.Length));
                Assert.Throws <ObjectDisposedException>(() => pipe.ReadByte());
                Assert.Throws <ObjectDisposedException>(() => { pipe.ReadAsync(buffer, 0, buffer.Length); });
                Assert.Throws <ObjectDisposedException>(() => pipe.IsMessageComplete);
            }
        }
        protected override ServerClientPair CreateServerClientPair()
        {
            ServerClientPair ret = new ServerClientPair();
            string pipeName = GetUniquePipeName();
            var readablePipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            var writeablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            Task serverConnect = Task.Factory.FromAsync(readablePipe.BeginWaitForConnection, readablePipe.EndWaitForConnection, null);
            writeablePipe.Connect();
            serverConnect.Wait();

            ret.readablePipe = readablePipe;
            ret.writeablePipe = writeablePipe;
            return ret;
        }
        protected override ServerClientPair CreateServerClientPair()
        {
            ServerClientPair ret = new ServerClientPair();
            string pipeName = GetUniquePipeName();
            var writeablePipe = new NamedPipeServerStream(pipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            var readablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.In, PipeOptions.Asynchronous);

            Task clientConnect = readablePipe.ConnectAsync();
            writeablePipe.WaitForConnection();
            clientConnect.Wait();

            ret.readablePipe = readablePipe;
            ret.writeablePipe = writeablePipe;
            return ret;
        }
Ejemplo n.º 12
0
        protected override ServerClientPair CreateServerClientPair()
        {
            ServerClientPair ret      = new ServerClientPair();
            string           pipeName = GetUniquePipeName();
            var writeablePipe         = new NamedPipeServerStream(pipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            var readablePipe          = new NamedPipeClientStream(".", pipeName, PipeDirection.In, PipeOptions.Asynchronous);

            Task clientConnect = readablePipe.ConnectAsync();

            writeablePipe.WaitForConnection();
            clientConnect.Wait();

            ret.readablePipe  = readablePipe;
            ret.writeablePipe = writeablePipe;
            return(ret);
        }
Ejemplo n.º 13
0
        public async Task ValidWriteAsync_ValidReadAsync()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                Assert.True(pair.writeablePipe.IsConnected);
                Assert.True(pair.readablePipe.IsConnected);

                byte[] sent     = new byte[] { 123, 0, 5 };
                byte[] received = new byte[] { 0, 0, 0 };

                Task write = pair.writeablePipe.WriteAsync(sent, 0, sent.Length);
                Assert.Equal(sent.Length, await pair.readablePipe.ReadAsync(received, 0, sent.Length));
                Assert.Equal(sent, received);
                await write;
            }
        }
        protected override ServerClientPair CreateServerClientPair()
        {
            ServerClientPair ret      = new ServerClientPair();
            string           pipeName = GetUniquePipeName();
            var readablePipe          = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            var writeablePipe         = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            Task serverConnect = Task.Factory.FromAsync(readablePipe.BeginWaitForConnection, readablePipe.EndWaitForConnection, null);

            writeablePipe.Connect();
            serverConnect.Wait();

            ret.readablePipe  = readablePipe;
            ret.writeablePipe = writeablePipe;
            return(ret);
        }
Ejemplo n.º 15
0
        public virtual async Task ReadFromPipeWithClosedPartner_Span_ReadNoBytes()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                pair.writeablePipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                // The pipe won't be marked as Broken until the first read, so prime it
                // to test both the case where it's not yet marked as "Broken" and then
                // where it is.
                Assert.Equal(0, pair.readablePipe.Read(new Span <byte>(buffer)));

                Assert.Equal(0, pair.readablePipe.Read(new Span <byte>(buffer)));
                Assert.Equal(0, await pair.readablePipe.ReadAsync(new Memory <byte>(buffer)));
            }
        }
Ejemplo n.º 16
0
        public async Task ValidWriteAsync_ValidReadAsync_APM()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                Assert.True(pair.writeablePipe.IsConnected);
                Assert.True(pair.readablePipe.IsConnected);

                byte[] sent     = new byte[] { 123, 0, 5 };
                byte[] received = new byte[] { 0, 0, 0 };

                Task       write = Task.Factory.FromAsync <byte[], int, int>(pair.writeablePipe.BeginWrite, pair.writeablePipe.EndWrite, sent, 0, sent.Length, null);
                Task <int> read  = Task.Factory.FromAsync <byte[], int, int, int>(pair.readablePipe.BeginRead, pair.readablePipe.EndRead, received, 0, received.Length, null);
                Assert.Equal(sent.Length, await read);
                Assert.Equal(sent, received);
                await write;
            }
        }
Ejemplo n.º 17
0
        public void ReadOnWriteOnlyPipe_Span_Throws_NotSupportedException()
        {
            if (SupportsBidirectionalReadingWriting)
            {
                return;
            }

            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.False(pipe.CanRead);

                Assert.Throws <NotSupportedException>(() => pipe.Read(new Span <byte>(new byte[9])));
                Assert.Throws <NotSupportedException>(() => { pipe.ReadAsync(new Memory <byte>(new byte[10])); });
            }
        }
Ejemplo n.º 18
0
        public virtual void ReadOnWriteOnlyPipe_Throws_NotSupportedException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.False(pipe.CanRead);

                Assert.Throws <NotSupportedException>(() => pipe.Read(new byte[9], 0, 5));

                Assert.Throws <NotSupportedException>(() => pipe.ReadByte());

                Assert.Throws <NotSupportedException>(() => pipe.InBufferSize);

                Assert.Throws <NotSupportedException>(() => { pipe.ReadAsync(new byte[10], 0, 5); });
            }
        }
Ejemplo n.º 19
0
        public void WriteWithOutOfBoundsArray_Throws_ArgumentException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanWrite);

                // offset out of bounds
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[1], 1, 1));

                // offset out of bounds for 0 count read
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[1], 2, 0));

                // offset out of bounds even for 0 length buffer
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[0], 1, 0));

                // combination offset and count out of bounds
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[2], 1, 2));

                // edges
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[0], int.MaxValue, 0));
                Assert.Throws <ArgumentException>(null, () => pipe.Write(new byte[0], int.MaxValue, int.MaxValue));

                Assert.Throws <ArgumentException>(() => pipe.Write(new byte[5], 3, 4));

                // offset out of bounds
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[1], 1, 1); });

                // offset out of bounds for 0 count read
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[1], 2, 0); });

                // offset out of bounds even for 0 length buffer
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[0], 1, 0); });

                // combination offset and count out of bounds
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[2], 1, 2); });

                // edges
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[0], int.MaxValue, 0); });
                Assert.Throws <ArgumentException>(null, () => { pipe.WriteAsync(new byte[0], int.MaxValue, int.MaxValue); });

                Assert.Throws <ArgumentException>(() => { pipe.WriteAsync(new byte[5], 3, 4); });
            }
        }
Ejemplo n.º 20
0
        public async Task ReadWrite(int size)
        {
            Random rand = new Random(314);

            byte[] sent     = new byte[size];
            byte[] received = new byte[size];
            rand.NextBytes(sent);
            foreach (var iteration in Benchmark.Iterations)
            {
                using (ServerClientPair pair = CreateServerClientPair())
                    using (iteration.StartMeasurement())
                    {
                        Task write = Task.Run(() => pair.writeablePipe.Write(sent, 0, sent.Length));
                        pair.readablePipe.Read(received, 0, size);
                        await write;
                    }
            }
        }
Ejemplo n.º 21
0
        public void WritePipeUnsupportedMembers_Throws_NotSupportedException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);

                Assert.Throws <NotSupportedException>(() => pipe.Length);

                Assert.Throws <NotSupportedException>(() => pipe.SetLength(10L));

                Assert.Throws <NotSupportedException>(() => pipe.Position);

                Assert.Throws <NotSupportedException>(() => pipe.Position = 10L);

                Assert.Throws <NotSupportedException>(() => pipe.Seek(10L, System.IO.SeekOrigin.Begin));
            }
        }
Ejemplo n.º 22
0
        public void WriteToReadOnlyPipe_Span_Throws_NotSupportedException()
        {
            if (SupportsBidirectionalReadingWriting)
            {
                return;
            }

            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                Assert.True(pipe.IsConnected);
                Assert.False(pipe.CanWrite);
                Assert.False(pipe.CanSeek);

                Assert.Throws <NotSupportedException>(() => pipe.Write(new ReadOnlySpan <byte>(new byte[5])));
                Assert.Throws <NotSupportedException>(() => { pipe.WriteAsync(new ReadOnlyMemory <byte>(new byte[5])); });
            }
        }
Ejemplo n.º 23
0
        public void ValidWrite_ValidRead()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                Assert.True(pair.writeablePipe.IsConnected);
                Assert.True(pair.readablePipe.IsConnected);

                byte[] sent     = new byte[] { 123, 0, 5 };
                byte[] received = new byte[] { 0, 0, 0 };

                Task.Run(() => { pair.writeablePipe.Write(sent, 0, sent.Length); });
                Assert.Equal(sent.Length, pair.readablePipe.Read(received, 0, sent.Length));
                Assert.Equal(sent, received);
                if (OperatingSystem.IsWindows()) // WaitForPipeDrain isn't supported on Unix
                {
                    pair.writeablePipe.WaitForPipeDrain();
                }
            }
        }
Ejemplo n.º 24
0
        public void ValidWrite_ValidRead()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                Assert.True(pair.writeablePipe.IsConnected);
                Assert.True(pair.readablePipe.IsConnected);

                byte[] sent     = new byte[] { 123, 0, 5 };
                byte[] received = new byte[] { 0, 0, 0 };

                Task.Run(() => { pair.writeablePipe.Write(sent, 0, sent.Length); });
                Assert.Equal(sent.Length, pair.readablePipe.Read(received, 0, sent.Length));
                Assert.Equal(sent, received);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    pair.writeablePipe.WaitForPipeDrain();
                }
            }
        }
Ejemplo n.º 25
0
        public virtual void WriteToPipeWithClosedPartner_Span_Throws_IOException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
                    (pair.readablePipe is NamedPipeClientStream || pair.writeablePipe is NamedPipeClientStream))
                {
                    // On Unix, NamedPipe*Stream is implemented in term of sockets, where information
                    // about shutdown is not immediately propagated.
                    return;
                }

                pair.readablePipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <IOException>(() => pair.writeablePipe.Write(new Span <byte>(buffer)));
                Assert.Throws <IOException>(() => { pair.writeablePipe.WriteAsync(new Memory <byte>(buffer)); });
            }
        }
Ejemplo n.º 26
0
        public void WriteWithNullBuffer_Throws_ArgumentNullException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.writeablePipe;
                Assert.True(pipe.IsConnected);
                Assert.True(pipe.CanWrite);

                // Null is an invalid Buffer
                Assert.Throws <ArgumentNullException>("buffer", () => pipe.Write(null, 0, 1));
                Assert.Throws <ArgumentNullException>("buffer", () => { pipe.WriteAsync(null, 0, 1); });

                // Buffer validity is checked before Offset
                Assert.Throws <ArgumentNullException>("buffer", () => pipe.Write(null, -1, 1));
                Assert.Throws <ArgumentNullException>("buffer", () => { pipe.WriteAsync(null, -1, 1); });

                // Buffer validity is checked before Count
                Assert.Throws <ArgumentNullException>("buffer", () => pipe.Write(null, -1, -1));
                Assert.Throws <ArgumentNullException>("buffer", () => { pipe.WriteAsync(null, -1, -1); });
            }
        }
Ejemplo n.º 27
0
        public virtual void WriteToPipeWithClosedPartner_Throws_IOException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                if (!OperatingSystem.IsWindows() &&
                    (pair.readablePipe is NamedPipeClientStream || pair.writeablePipe is NamedPipeClientStream))
                {
                    // On Unix, NamedPipe*Stream is implemented in term of sockets, where information
                    // about shutdown is not immediately propagated.
                    return;
                }

                pair.readablePipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <IOException>(() => pair.writeablePipe.Write(buffer, 0, buffer.Length));
                Assert.Throws <IOException>(() => pair.writeablePipe.WriteByte(123));
                Assert.Throws <IOException>(() => { pair.writeablePipe.WriteAsync(buffer, 0, buffer.Length); });
                Assert.Throws <IOException>(() => pair.writeablePipe.Flush());
            }
        }
Ejemplo n.º 28
0
        public virtual void WriteToReadOnlyPipe_Throws_NotSupportedException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                Assert.True(pipe.IsConnected);
                Assert.False(pipe.CanWrite);

                Assert.Throws <NotSupportedException>(() => pipe.Write(new byte[5], 0, 5));

                Assert.Throws <NotSupportedException>(() => pipe.WriteByte(123));

                Assert.Throws <NotSupportedException>(() => pipe.Flush());

                Assert.Throws <NotSupportedException>(() => pipe.OutBufferSize);

                Assert.Throws <NotSupportedException>(() => pipe.WaitForPipeDrain());

                Assert.Throws <NotSupportedException>(() => { pipe.WriteAsync(new byte[5], 0, 5); });
            }
        }
Ejemplo n.º 29
0
            /// <summary>Callback after receiving a request to accept.</summary>
            /// <param name="result">Result of the operation.</param>
            private void AcceptCallback(IAsyncResult result)
            {
                if (IsConnected())
                {
                    TcpClient client = m_TcpServer.EndAcceptTcpClient(result);
                    m_TcpServer.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), m_TcpServer);

                    m_Clients.Add(client);
                    m_ReadBuffers.Add(new Byte[READ_BUFFER_SIZE]);

                    Int32 clientIndex = m_Clients.Count - 1;

                    ServerClientPair pair = new ServerClientPair(this, client, m_ReadCallback, m_ReadBuffers[clientIndex]);
                    client.Client.BeginReceive(m_ReadBuffers[clientIndex],
                                               0,
                                               ( int )READ_BUFFER_SIZE,
                                               SocketFlags.None,
                                               new AsyncCallback(ReadCallback),
                                               pair);
                }
            }
Ejemplo n.º 30
0
            /// <summary>Callback after receiving a packet of data.</summary>
            /// <param name="result">Result of the operation.</param>
            private static void ReadCallback(IAsyncResult result)
            {
                ServerClientPair pair = ( ServerClientPair )result.AsyncState;

                if (pair.ReadCallback != null)
                {
                    TcpNetworkServer server = pair.Server;
                    TcpClient        client = pair.Client;
                    Byte[]           buffer = pair.Buffer;

                    Int32 dataReceived = client.Client.EndReceive(result);

                    if (dataReceived == 0)
                    {
                        server.DisconnectClient(client);
                    }
                    else if (dataReceived > 0)
                    {
                        pair.ReadCallback(server, client, buffer, ( UInt32 )dataReceived);
                        client.Client.BeginReceive(buffer, 0, ( int )READ_BUFFER_SIZE, SocketFlags.None, ReadCallback, pair);
                    }
                }
            }
Ejemplo n.º 31
0
        public async Task AsyncReadWriteChain_CopyToAsync(int iterations, int writeBufferSize, int readBufferSize, bool cancelableToken)
        {
            var writeBuffer = new byte[writeBufferSize * iterations];

            new Random().NextBytes(writeBuffer);
            var cancellationToken = cancelableToken ? new CancellationTokenSource().Token : CancellationToken.None;

            using (ServerClientPair pair = CreateServerClientPair())
            {
                var  readData = new MemoryStream();
                Task copyTask = pair.readablePipe.CopyToAsync(readData, readBufferSize, cancellationToken);

                for (int iter = 0; iter < iterations; iter++)
                {
                    await pair.writeablePipe.WriteAsync(writeBuffer, iter *writeBufferSize, writeBufferSize, cancellationToken);
                }
                pair.writeablePipe.Dispose();

                await copyTask;
                Assert.Equal(writeBuffer.Length, readData.Length);
                Assert.Equal(writeBuffer, readData.ToArray());
            }
        }