Ejemplo n.º 1
0
        public override async Task <Func <Func <IProtocolReader, ITransportMetadata, TResult>, Task <TResult> > > WriteAndReadData <TResult>(ITransport transport, ITransportMetadata metadata, CancellationToken cancellationToken, Action <IProtocolWriter> handler)
        {
            Func <Task <TransportMessage> > receiveHandler;

            using (var buffer = new MemoryStream())
            {
                if (this.Compress)
                {
                    using (var gzip = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Compress))
                    {
                        var writer = new EntanglementProtocolWriter(gzip);
                        handler(writer);
                    }
                }
                else
                {
                    var writer = new EntanglementProtocolWriter(buffer);
                    handler(writer);

                    buffer.Position = 0;
                }

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

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

                using (var buffer = new MemoryStream(receivedData.Payload))
                {
                    if (this.Compress)
                    {
                        using (var gzip = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Decompress))
                        {
                            var reader = new EntanglementProtocolReader(gzip);

                            return readHandler(reader, receivedData.Metadata);
                        }
                    }
                    else
                    {
                        var reader = new EntanglementProtocolReader(buffer);

                        return readHandler(reader, receivedData.Metadata);
                    }
                }
            }));
        }
Ejemplo n.º 2
0
        public T Read <T>(byte[] data, Func <IProtocolReader, T> handler)
        {
            using (var buffer = new MemoryStream(data))
            {
                if (this.Compress)
                {
                    using (var gzip = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Decompress))
                    {
                        var reader = new EntanglementProtocolReader(gzip);

                        return(handler(reader));
                    }
                }
                else
                {
                    var reader = new EntanglementProtocolReader(buffer);

                    return(handler(reader));
                }
            }
        }
Ejemplo n.º 3
0
        public override async Task <TResult> ReadTaggedData <TResult>(ITransport transport, CancellationToken cancellationToken, Func <IProtocolReader, string, ITransportMetadata, TResult> handler)
        {
            var receivedData = await transport.ReceiveTagged(cancellationToken);

            using (var buffer = new MemoryStream(receivedData.Message.Payload))
            {
                if (this.Compress)
                {
                    using (var gzip = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Decompress))
                    {
                        var reader = new EntanglementProtocolReader(gzip);

                        return(handler(reader, receivedData.Id, receivedData.Message.Metadata));
                    }
                }
                else
                {
                    var reader = new EntanglementProtocolReader(buffer);

                    return(handler(reader, receivedData.Id, receivedData.Message.Metadata));
                }
            }
        }
Ejemplo n.º 4
0
        public override async Task ReadTaggedData(ITransport transport, Action <IProtocolReader, string, ITransportMetadata> handler)
        {
            var receivedData = await transport.ReceiveTagged();

            using (var buffer = new MemoryStream(receivedData.Message.Payload))
            {
                if (this.Compress)
                {
                    using (var gzip = new System.IO.Compression.DeflateStream(buffer, System.IO.Compression.CompressionMode.Decompress))
                    {
                        var reader = new EntanglementProtocolReader(gzip);

                        handler(reader, receivedData.Id, receivedData.Message.Metadata);
                    }
                }
                else
                {
                    var reader = new EntanglementProtocolReader(buffer);

                    handler(reader, receivedData.Id, receivedData.Message.Metadata);
                }
            }
        }