From 4422292ff5f74f3c865c8291ea1595d008341c06 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 3 Nov 2017 13:45:17 -0400 Subject: [PATCH] Make c# protobuf and immutable protobuf implement common IImmutableMessage interface --- src/client/spark_api/api/ApiServer.cs | 2 +- src/client/spark_api/api/ApiUtils.cs | 4 ++-- src/client/spark_api/api/ClientChannel.cs | 2 +- src/client/spark_api/utils/IImmutableProto.cs | 7 ++++--- src/lib/api/service_compiler/templates/c#/immutable.pb.cs | 6 +++++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/client/spark_api/api/ApiServer.cs b/src/client/spark_api/api/ApiServer.cs index 66f6e2fa6..fa431657e 100644 --- a/src/client/spark_api/api/ApiServer.cs +++ b/src/client/spark_api/api/ApiServer.cs @@ -28,7 +28,7 @@ namespace bts.api.core } public abstract void dispatch(RequestHeader header, Stream request, Action on_response); - protected void respond(int request_id, Task task, Action on_response) where T : Google.Protobuf.IMessage { + protected void respond(int request_id, Task task, Action on_response) where T : IMessage, IImmutableMessage { var header = new ResponseHeader() { RequestId = request_id }; if (task.IsCanceled || task.IsFaulted) { header.Code = ApiError.ServerFault; diff --git a/src/client/spark_api/api/ApiUtils.cs b/src/client/spark_api/api/ApiUtils.cs index 3ffcf3383..56a51d27a 100644 --- a/src/client/spark_api/api/ApiUtils.cs +++ b/src/client/spark_api/api/ApiUtils.cs @@ -11,7 +11,7 @@ namespace bts.api { internal static class ApiUtils { - public static byte[] serialize(this RequestHeader header, T body) where T : IMessage { + public static byte[] serialize(this RequestHeader header, T body) where T : IImmutableMessage { int body_size = body?.CalculateSize() ?? 0; int header_size = header.CalculateSize() + 1; // 1 byte header length var buffer = new byte[header_size+body_size]; @@ -44,7 +44,7 @@ namespace bts.api { return mkresult(header, body); } - public static byte[] serialize(this ResponseHeader header, T body) where T : IMessage { + public static byte[] serialize(this ResponseHeader header, T body) where T : IImmutableMessage { int header_size = 1 + header.CalculateSize(); int body_size = body == null ? 0 : body.CalculateSize(); var buffer = new byte[header_size + body_size]; diff --git a/src/client/spark_api/api/ClientChannel.cs b/src/client/spark_api/api/ClientChannel.cs index 7b1eea315..f727f6a0e 100644 --- a/src/client/spark_api/api/ClientChannel.cs +++ b/src/client/spark_api/api/ClientChannel.cs @@ -12,7 +12,7 @@ namespace bts.api.core { } public abstract void Dispose(); public abstract Task> send(int method_id, Request request) - where Request: IMessage where Response : class,IMessage,new(); + where Request: IImmutableMessage where Response : class,IMessage,new(); public abstract IObservable> subscribe(string method_and_qualifier, MessageParser parser) where T : IMessage; diff --git a/src/client/spark_api/utils/IImmutableProto.cs b/src/client/spark_api/utils/IImmutableProto.cs index 5c1788aa1..7764789ec 100644 --- a/src/client/spark_api/utils/IImmutableProto.cs +++ b/src/client/spark_api/utils/IImmutableProto.cs @@ -8,19 +8,20 @@ using System.Text; using System.Threading.Tasks; namespace bts { - internal interface IImmutableProto { + internal interface IImmutableMessage { + MessageDescriptor Descriptor { get; } int CalculateSize(); void WriteTo(CodedOutputStream output); } internal static class ImmutableProtoExtensions { - public static void WriteTo(this IImmutableProto message, Stream output) { + public static void WriteTo(this IImmutableMessage message, Stream output) { var stream = new CodedOutputStream(output); message.WriteTo(stream); stream.Flush(); } - public static byte[] ToByteArray(this IImmutableProto message) { + public static byte[] ToByteArray(this IImmutableMessage message) { var result = new byte[message.CalculateSize()]; var output = new CodedOutputStream(result); message.WriteTo(output); diff --git a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs index 755766789..45eb3ca7c 100644 --- a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs +++ b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs @@ -8,10 +8,14 @@ using Google.Protobuf.WellKnownTypes; namespace {{package}} { {{#each message_type}} - partial struct {{name}}Immutable : bts.IImmutableProto { + partial class {{name}} : bts.IImmutableMessage { + MessageDescriptor bts.IImmutableMessage.Descriptor => {{name}}.Descriptor; + } + partial struct {{name}}Immutable : bts.IImmutableMessage { readonly {{name}} _impl; public static MessageDescriptor Descriptor => {{name}}.Descriptor; + MessageDescriptor bts.IImmutableMessage.Descriptor => {{name}}.Descriptor; public int CalculateSize() => _impl.CalculateSize(); public void WriteTo(CodedOutputStream output) => _impl.WriteTo(output);