From c2fd689bad731f30b2ab43a5613e164f7e44be5c Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 25 Sep 2018 23:02:42 +0200 Subject: address comments --- src/csharp/Grpc.Core/Marshaller.cs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/csharp') diff --git a/src/csharp/Grpc.Core/Marshaller.cs b/src/csharp/Grpc.Core/Marshaller.cs index 7f010dc419..0af9aa586b 100644 --- a/src/csharp/Grpc.Core/Marshaller.cs +++ b/src/csharp/Grpc.Core/Marshaller.cs @@ -57,6 +57,8 @@ namespace Grpc.Core { this.contextualSerializer = GrpcPreconditions.CheckNotNull(serializer, nameof(serializer)); this.contextualDeserializer = GrpcPreconditions.CheckNotNull(deserializer, nameof(deserializer)); + // TODO(jtattermusch): once gRPC C# library switches to using contextual (de)serializer, + // emulating the simple (de)serializer will become unnecessary. this.serializer = EmulateSimpleSerializer; this.deserializer = EmulateSimpleDeserializer; } @@ -87,6 +89,7 @@ namespace Grpc.Core private byte[] EmulateSimpleSerializer(T msg) { // TODO(jtattermusch): avoid the allocation by passing a thread-local instance + // This code will become unnecessary once gRPC C# library switches to using contextual (de)serializer. var context = new EmulatedSerializationContext(); this.contextualSerializer(msg, context); return context.GetPayload(); @@ -96,6 +99,7 @@ namespace Grpc.Core private T EmulateSimpleDeserializer(byte[] payload) { // TODO(jtattermusch): avoid the allocation by passing a thread-local instance + // This code will become unnecessary once gRPC C# library switches to using contextual (de)serializer. var context = new EmulatedDeserializationContext(payload); return this.contextualDeserializer(context); } @@ -134,6 +138,7 @@ namespace Grpc.Core internal class EmulatedDeserializationContext : DeserializationContext { readonly byte[] payload; + bool alreadyCalledPayloadAsNewBuffer; public EmulatedDeserializationContext(byte[] payload) { @@ -144,6 +149,8 @@ namespace Grpc.Core public override byte[] PayloadAsNewBuffer() { + GrpcPreconditions.CheckState(!alreadyCalledPayloadAsNewBuffer); + alreadyCalledPayloadAsNewBuffer = true; return payload; } } -- cgit v1.2.3