Example #1
0
        public override async Task WriteData(ITransport transport, ITransportMetadata metadata, Action <IProtocolWriter> handler)
        {
            using (var buffer = new MemoryStream())
            {
                if (this.CompressionEnabled)
                {
                    using (var compressionStream = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Compress))
                    {
                        var writer = new BinaryProtocolWriter(compressionStream);

                        handler(writer);
                    }
                }
                else
                {
                    var writer = new BinaryProtocolWriter(buffer);

                    handler(writer);

                    buffer.Position = 0;
                }

                var data = buffer.ToArray();
                await transport.Send(new TransportMessage(data, VolatileTransportMetadata.FromMetadata(metadata)));
            }
        }
Example #2
0
        public override async Task <Func <Func <IProtocolReader, ITransportMetadata, TResult>, Task <TResult> > > WriteAndReadData <TResult>(ITransport transport, ITransportMetadata metadata, Action <IProtocolWriter> handler)
        {
            Func <Task <TransportMessage> > receiveHandler;

            using (var buffer = new MemoryStream())
            {
                if (this.CompressionEnabled)
                {
                    using (var compressionStream = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Compress))
                    {
                        var writer = new BinaryProtocolWriter(compressionStream);

                        handler(writer);
                    }
                }
                else
                {
                    var writer = new BinaryProtocolWriter(buffer);

                    handler(writer);

                    buffer.Position = 0;
                }

                var data = buffer.ToArray();
                receiveHandler = await transport.SendAndReceive(new TransportMessage(data, VolatileTransportMetadata.FromMetadata(metadata)));
            }

            return(new Func <Func <IProtocolReader, ITransportMetadata, TResult>, Task <TResult> >(async(readHandler) => {
                var receivedData = await receiveHandler();

                var buffer = new MemoryStream(receivedData.Payload);
                BinaryProtocolReader reader;
                if (this.CompressionEnabled)
                {
                    var compressionStream = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Decompress);
                    reader = new BinaryProtocolReader(compressionStream);
                }
                else
                {
                    reader = new BinaryProtocolReader(buffer);
                }

                return readHandler(reader, receivedData.Metadata);
            }));
        }