Provide length when getting serialization buffer writer

pull/20691/head
James Newton-King 5 years ago
parent 2d80e830c0
commit d8d272119a
No known key found for this signature in database
GPG Key ID: A66B2F456BF5526
  1. 4
      src/csharp/Grpc.Core.Api/SerializationContext.cs
  2. 10
      src/csharp/Grpc.Core.Tests/Internal/DefaultSerializationContextTest.cs
  3. 2
      src/csharp/Grpc.Core/Internal/DefaultSerializationContext.cs

@ -41,7 +41,8 @@ namespace Grpc.Core
/// Gets buffer writer that can be used to write the serialized data. Once serialization is finished,
/// <c>Complete()</c> needs to be called.
/// </summary>
public virtual IBufferWriter<byte> GetBufferWriter()
/// <param name="payloadLength">The total length of the payload in bytes.</param>
public virtual IBufferWriter<byte> GetBufferWriter(int payloadLength)
{
throw new NotImplementedException();
}
@ -52,7 +53,6 @@ namespace Grpc.Core
public virtual void Complete()
{
throw new NotImplementedException();
}
}
}

@ -84,7 +84,7 @@ namespace Grpc.Core.Internal.Tests
var context = scope.Context;
var origPayload = GetTestBuffer(payloadSize);
var bufferWriter = context.GetBufferWriter();
var bufferWriter = context.GetBufferWriter(payloadSize);
origPayload.AsSpan().CopyTo(bufferWriter.GetSpan(payloadSize));
bufferWriter.Advance(payloadSize);
context.Complete();
@ -106,7 +106,7 @@ namespace Grpc.Core.Internal.Tests
var context = scope.Context;
var origPayload = GetTestBuffer(payloadSize);
var bufferWriter = context.GetBufferWriter();
var bufferWriter = context.GetBufferWriter(payloadSize);
origPayload.AsSpan().CopyTo(bufferWriter.GetMemory(payloadSize).Span);
bufferWriter.Advance(payloadSize);
context.Complete();
@ -131,7 +131,7 @@ namespace Grpc.Core.Internal.Tests
var context = scope.Context;
var origPayload = GetTestBuffer(payloadSize);
var bufferWriter = context.GetBufferWriter();
var bufferWriter = context.GetBufferWriter(payloadSize);
for (int offset = 0; offset < payloadSize; offset += maxSliceSize)
{
var sliceSize = Math.Min(maxSliceSize, payloadSize - offset);
@ -165,7 +165,7 @@ namespace Grpc.Core.Internal.Tests
var origPayload2 = GetTestBuffer(20);
var bufferWriter = context.GetBufferWriter();
var bufferWriter = context.GetBufferWriter(20);
origPayload2.AsSpan().CopyTo(bufferWriter.GetMemory(origPayload2.Length).Span);
bufferWriter.Advance(origPayload2.Length);
context.Complete();
@ -185,7 +185,7 @@ namespace Grpc.Core.Internal.Tests
var context = scope.Context;
context.Complete(GetTestBuffer(10));
Assert.Throws(typeof(InvalidOperationException), () => context.GetBufferWriter());
Assert.Throws(typeof(InvalidOperationException), () => context.GetBufferWriter(10));
}
}

@ -50,7 +50,7 @@ namespace Grpc.Core.Internal
/// <summary>
/// Expose serializer as buffer writer
/// </summary>
public override IBufferWriter<byte> GetBufferWriter()
public override IBufferWriter<byte> GetBufferWriter(int payloadLength)
{
GrpcPreconditions.CheckState(!isComplete);
return sliceBuffer;

Loading…
Cancel
Save