public void RequestStream()
        {
            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint))
            {
                var response = client.StreamRequest(new StreamRequest());

                var stream = client.EndGetStream(client.BeginGetStream((int)response.StreamId, null, null));

                using (var reader = new StreamReader(stream.Stream))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
        }
        public void ExpectMemoryStream()
        {
            var configuration = new ProtoClientConfiguration
            {
                StreamManager = new HybridStreamManager(Path.GetTempPath(), 50)
            };

            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint, configuration))
            {
                var response = client.StreamRequest(new StreamRequest { Length = 10 });

                using (var stream = client.EndGetStream(client.BeginGetStream((int)response.StreamId, null, null)))
                {
                    Assert.That(stream.Stream, Is.InstanceOf<MemoryStream>());
                }
            }
        }
        public void StreamTooLong()
        {
            var configuration = new ProtoClientConfiguration
            {
                StreamManager = new HybridStreamManager(Path.GetTempPath(), 50, 100)
            };

            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint, configuration))
            {
                // TODO: Communicating that the stream was rejected isn't yet
                // communicated clearly.

                var response = client.StreamRequest(new StreamRequest { Length = 200 });

                using (client.EndGetStream(client.BeginGetStream((int)response.StreamId, null, null)))
                {
                }
            }
        }
        public void RequestStream(StreamDisposition disposition)
        {
            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint))
            {
                var response = client.StreamRequest(new StreamRequest
                {
                    Attachment = disposition == StreamDisposition.Attachment
                });

                using (var protoStream = client.EndGetStream(client.BeginGetStream((int)response.StreamId, null, null)))
                {
                    Assert.AreEqual(protoStream.Disposition, disposition);

                    using (var stream = protoStream.DetachStream())
                    using (var reader = new StreamReader(stream))
                    {
                        Console.WriteLine(reader.ReadToEnd());
                    }
                }
            }
        }
        public void LargeSendStream()
        {
            var callback = new ClientCallbackService();

            var configuration = new ProtoClientConfiguration
            {
                CallbackObject = callback
            };

            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint, configuration))
            {
                int streamId = client.SendStream(
                    new MemoryStream(new byte[1 << 20]),
                    "Payload.txt",
                    "text/plain"
                );

                client.StreamUpload(new StreamResponse { StreamId = (uint)streamId });

                Assert.True(callback.CallbackReceivedEvent.WaitOne(TimeSpan.FromSeconds(1)));
            }
        }
        public void GracefullCloseWithoutFullTimeout()
        {
            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            {
                var stopwatch = new Stopwatch();

                var client = new ClientService(host.LocalEndPoint);

                ThreadPool.QueueUserWorkItem(p =>
                {
                    using (client)
                    {
                        Thread.Sleep(100);
                    }
                });

                stopwatch.Start();
                host.Close(CloseMode.Gracefully, TimeSpan.FromMilliseconds(500));
                stopwatch.Stop();

                Assert.GreaterOrEqual(stopwatch.ElapsedMilliseconds, 90);
                Assert.LessOrEqual(stopwatch.ElapsedMilliseconds, 150);
            }
        }
        private void SendWithStreamFailure(long length, StreamFailureType type, bool expectFailure)
        {
            var callback = new ClientCallbackService();

            var configuration = new ProtoClientConfiguration
            {
                CallbackObject = callback
            };

            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint, configuration))
            {
                int streamId = client.SendStream(
                    new FailingStream(length, type),
                    "Payload.txt",
                    "text/plain"
                );

                client.StreamUpload(new StreamResponse { StreamId = (uint)streamId });

                Assert.True(callback.CallbackReceivedEvent.WaitOne(TimeSpan.FromSeconds(1)));

                if (expectFailure)
                    Assert.AreEqual("Receive stream failed", callback.OneWayPingPayload);
                else
                    Assert.AreNotEqual("Receive stream failed", callback.OneWayPingPayload);
            }
        }
        private void SendStream(StreamDisposition disposition)
        {
            var callback = new ClientCallbackService();

            var configuration = new ProtoClientConfiguration
            {
                CallbackObject = callback
            };

            using (var host = new ProtoHost<ServerService>(new IPEndPoint(IPAddress.Loopback, 0)))
            using (var client = new ClientService(host.LocalEndPoint, configuration))
            {
                int streamId = client.SendStream(
                    new MemoryStream(Encoding.UTF8.GetBytes("Payload")),
                    "Payload.txt",
                    "text/plain",
                    disposition
                );

                client.StreamUpload(new StreamResponse { StreamId = (uint)streamId });

                Assert.True(callback.CallbackReceivedEvent.WaitOne(TimeSpan.FromSeconds(1)));
                Assert.True(callback.OneWayPingPayload.Contains(disposition.ToString()));
            }
        }