예제 #1
0
        internal static Task Write(ConnectionContext connection, GrainId grainId, SiloAddress siloAddress)
        {
            var output       = connection.Transport.Output;
            var outputWriter = new PrefixingBufferWriter <byte, PipeWriter>(output, sizeof(int), 1024, MemoryPool <byte> .Shared);
            var writer       = new BinaryTokenStreamWriter2 <PrefixingBufferWriter <byte, PipeWriter> >(outputWriter);

            writer.Write(grainId);
            if (!(siloAddress is null))
            {
                writer.Write(siloAddress);
            }
            writer.Commit();

            var length = outputWriter.CommittedBytes;

            if (length > MaxPreambleLength)
            {
                throw new InvalidOperationException($"Created preamble of length {length}, which is greater than maximum allowed size of {MaxPreambleLength}.");
            }

            Span <byte> lengthSpan = stackalloc byte[4];

            BinaryPrimitives.WriteInt32LittleEndian(lengthSpan, length);
            outputWriter.Complete(lengthSpan);

            var flushTask = output.FlushAsync();

            if (flushTask.IsCompletedSuccessfully)
            {
                return(Task.CompletedTask);
            }
            return(flushTask.AsTask());
        }
        internal static async System.Threading.Tasks.ValueTask Write(ConnectionContext connection, GrainId nodeIdentity, NetworkProtocolVersion protocolVersion, SiloAddress siloAddress)
        {
            var output       = connection.Transport.Output;
            var outputWriter = new PrefixingBufferWriter <byte, PipeWriter>(sizeof(int), 1024, MemoryPool <byte> .Shared);

            outputWriter.Reset(output);
            var writer = new BinaryTokenStreamWriter2 <PrefixingBufferWriter <byte, PipeWriter> >(outputWriter);

            writer.Write(nodeIdentity);
            writer.Write((byte)protocolVersion);

            if (siloAddress is null)
            {
                writer.WriteNull();
            }
            else
            {
                writer.Write((byte)SerializationTokenType.SiloAddress);
                writer.Write(siloAddress);
            }

            writer.Commit();

            var length = outputWriter.CommittedBytes;

            if (length > MaxPreambleLength)
            {
                throw new InvalidOperationException($"Created preamble of length {length}, which is greater than maximum allowed size of {MaxPreambleLength}.");
            }

            WriteLength(outputWriter, length);

            var flushResult = await output.FlushAsync();

            if (flushResult.IsCanceled)
            {
                throw new OperationCanceledException("Flush canceled");
            }

            return;
        }
예제 #3
0
            public void Serialize <TBufferWriter>(TBufferWriter output, Message.HeadersContainer value) where TBufferWriter : IBufferWriter <byte>
            {
                var streamWriter = this.serializationContext.StreamWriter;

                if (streamWriter is BinaryTokenStreamWriter2 <TBufferWriter> writer)
                {
                    writer.PartialReset(output);
                }
                else
                {
                    this.serializationContext.StreamWriter = writer = new BinaryTokenStreamWriter2 <TBufferWriter>(output);
                }

                try
                {
                    Message.HeadersContainer.Serializer(value, this.serializationContext, null);
                }
                finally
                {
                    writer.Commit();
                    this.serializationContext.Reset();
                }
            }
예제 #4
0
            public void Serialize <TBufferWriter>(TBufferWriter output, T value) where TBufferWriter : IBufferWriter <byte>
            {
                var streamWriter = this.serializationContext.StreamWriter;

                if (streamWriter is BinaryTokenStreamWriter2 <TBufferWriter> writer)
                {
                    writer.PartialReset(output);
                }
                else
                {
                    this.serializationContext.StreamWriter = writer = new BinaryTokenStreamWriter2 <TBufferWriter>(output);
                }

                try
                {
                    SerializationManager.SerializeInner(this.serializationManager, value, typeof(T), this.serializationContext, writer);
                }
                finally
                {
                    writer.Commit();
                    this.serializationContext.Reset();
                }
            }