diff options
Diffstat (limited to 'src/ProtoGen/ServiceInterfaceGenerator.cs')
-rw-r--r-- | src/ProtoGen/ServiceInterfaceGenerator.cs | 436 |
1 files changed, 239 insertions, 197 deletions
diff --git a/src/ProtoGen/ServiceInterfaceGenerator.cs b/src/ProtoGen/ServiceInterfaceGenerator.cs index 019df4c8..698b7016 100644 --- a/src/ProtoGen/ServiceInterfaceGenerator.cs +++ b/src/ProtoGen/ServiceInterfaceGenerator.cs @@ -1,4 +1,5 @@ #region Copyright notice and license
+
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
@@ -30,6 +31,7 @@ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
#endregion
using System;
@@ -37,215 +39,255 @@ using System.Collections.Generic; using Google.ProtocolBuffers.DescriptorProtos;
using Google.ProtocolBuffers.Descriptors;
-namespace Google.ProtocolBuffers.ProtoGen {
- internal class ServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
-
- readonly CSharpServiceType svcType;
- ISourceGenerator _generator;
-
- internal ServiceGenerator(ServiceDescriptor descriptor)
- : base(descriptor) {
- svcType = descriptor.File.CSharpOptions.ServiceGeneratorType;
- switch (svcType) {
- case CSharpServiceType.NONE:
- _generator = new NoServicesGenerator(descriptor);
- break;
- case CSharpServiceType.GENERIC:
- _generator = new GenericServiceGenerator(descriptor);
- break;
- case CSharpServiceType.INTERFACE:
- _generator = new ServiceInterfaceGenerator(descriptor);
- break;
- case CSharpServiceType.IRPCDISPATCH:
- _generator = new RpcServiceGenerator(descriptor);
- break;
- default: throw new ApplicationException("Unknown ServiceGeneratorType = " + svcType.ToString());
- }
- }
+namespace Google.ProtocolBuffers.ProtoGen
+{
+ internal class ServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
+ {
+ private readonly CSharpServiceType svcType;
+ private ISourceGenerator _generator;
- public void Generate(TextGenerator writer) {
- _generator.Generate(writer);
- }
-
- class NoServicesGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
-
- public NoServicesGenerator(ServiceDescriptor descriptor)
- : base(descriptor) {
- }
- public virtual void Generate(TextGenerator writer) {
- writer.WriteLine("/*");
- writer.WriteLine("* Service generation is now disabled by default, use the following option to enable:");
- writer.WriteLine("* option (google.protobuf.csharp_file_options).service_generator_type = GENERIC;");
- writer.WriteLine("*/");
- }
- }
+ internal ServiceGenerator(ServiceDescriptor descriptor)
+ : base(descriptor)
+ {
+ svcType = descriptor.File.CSharpOptions.ServiceGeneratorType;
+ switch (svcType)
+ {
+ case CSharpServiceType.NONE:
+ _generator = new NoServicesGenerator(descriptor);
+ break;
+ case CSharpServiceType.GENERIC:
+ _generator = new GenericServiceGenerator(descriptor);
+ break;
+ case CSharpServiceType.INTERFACE:
+ _generator = new ServiceInterfaceGenerator(descriptor);
+ break;
+ case CSharpServiceType.IRPCDISPATCH:
+ _generator = new RpcServiceGenerator(descriptor);
+ break;
+ default:
+ throw new ApplicationException("Unknown ServiceGeneratorType = " + svcType.ToString());
+ }
+ }
- class ServiceInterfaceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
-
- public ServiceInterfaceGenerator(ServiceDescriptor descriptor)
- : base(descriptor) {
- }
+ public void Generate(TextGenerator writer)
+ {
+ _generator.Generate(writer);
+ }
- public virtual void Generate(TextGenerator writer) {
+ private class NoServicesGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
+ {
+ public NoServicesGenerator(ServiceDescriptor descriptor)
+ : base(descriptor)
+ {
+ }
- CSharpServiceOptions options = Descriptor.Options.GetExtension(CSharpOptions.CsharpServiceOptions);
- if (options != null && options.HasInterfaceId) {
- writer.WriteLine("[global::System.Runtime.InteropServices.GuidAttribute(\"{0}\")]", new Guid(options.InterfaceId));
+ public virtual void Generate(TextGenerator writer)
+ {
+ writer.WriteLine("/*");
+ writer.WriteLine("* Service generation is now disabled by default, use the following option to enable:");
+ writer.WriteLine("* option (google.protobuf.csharp_file_options).service_generator_type = GENERIC;");
+ writer.WriteLine("*/");
+ }
}
- writer.WriteLine("{0} partial interface I{1} {{", ClassAccessLevel, Descriptor.Name);
- writer.Indent();
- foreach (MethodDescriptor method in Descriptor.Methods)
+ private class ServiceInterfaceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
{
- CSharpMethodOptions mth = method.Options.GetExtension(CSharpOptions.CsharpMethodOptions);
- if(mth.HasDispatchId) {
- writer.WriteLine("[global::System.Runtime.InteropServices.DispId({0})]", mth.DispatchId);
- }
- writer.WriteLine("{0} {1}({2} {3});", GetClassName(method.OutputType), NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType), NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
+ public ServiceInterfaceGenerator(ServiceDescriptor descriptor)
+ : base(descriptor)
+ {
+ }
+
+ public virtual void Generate(TextGenerator writer)
+ {
+ CSharpServiceOptions options = Descriptor.Options.GetExtension(CSharpOptions.CsharpServiceOptions);
+ if (options != null && options.HasInterfaceId)
+ {
+ writer.WriteLine("[global::System.Runtime.InteropServices.GuidAttribute(\"{0}\")]",
+ new Guid(options.InterfaceId));
+ }
+ writer.WriteLine("{0} partial interface I{1} {{", ClassAccessLevel, Descriptor.Name);
+ writer.Indent();
+
+ foreach (MethodDescriptor method in Descriptor.Methods)
+ {
+ CSharpMethodOptions mth = method.Options.GetExtension(CSharpOptions.CsharpMethodOptions);
+ if (mth.HasDispatchId)
+ {
+ writer.WriteLine("[global::System.Runtime.InteropServices.DispId({0})]", mth.DispatchId);
+ }
+ writer.WriteLine("{0} {1}({2} {3});", GetClassName(method.OutputType),
+ NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType),
+ NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
+ }
+
+ writer.Outdent();
+ writer.WriteLine("}");
+ }
}
- writer.Outdent();
- writer.WriteLine("}");
- }
- }
-
- class RpcServiceGenerator : ServiceInterfaceGenerator {
+ private class RpcServiceGenerator : ServiceInterfaceGenerator
+ {
+ public RpcServiceGenerator(ServiceDescriptor descriptor)
+ : base(descriptor)
+ {
+ }
- public RpcServiceGenerator(ServiceDescriptor descriptor)
- : base(descriptor) {
- }
+ public override void Generate(TextGenerator writer)
+ {
+ base.Generate(writer);
- public override void Generate(TextGenerator writer)
- {
- base.Generate(writer);
+ writer.WriteLine();
- writer.WriteLine();
+ // CLIENT Proxy
+ {
+ if (Descriptor.File.CSharpOptions.ClsCompliance)
+ writer.WriteLine("[global::System.CLSCompliant(false)]");
+ writer.WriteLine("{0} partial class {1} : I{1}, pb::IRpcDispatch, global::System.IDisposable {{",
+ ClassAccessLevel, Descriptor.Name);
+ writer.Indent();
+ writer.WriteLine("private readonly bool dispose;");
+ writer.WriteLine("private readonly pb::IRpcDispatch dispatch;");
- // CLIENT Proxy
- {
- if (Descriptor.File.CSharpOptions.ClsCompliance)
- writer.WriteLine("[global::System.CLSCompliant(false)]");
- writer.WriteLine("{0} partial class {1} : I{1}, pb::IRpcDispatch, global::System.IDisposable {{", ClassAccessLevel, Descriptor.Name);
- writer.Indent();
- writer.WriteLine("private readonly bool dispose;");
- writer.WriteLine("private readonly pb::IRpcDispatch dispatch;");
-
- writer.WriteLine("public {0}(pb::IRpcDispatch dispatch) : this(dispatch, true) {{", Descriptor.Name);
- writer.WriteLine("}");
- writer.WriteLine("public {0}(pb::IRpcDispatch dispatch, bool dispose) {{", Descriptor.Name);
- writer.WriteLine(" if (null == (this.dispatch = dispatch)) throw new global::System.ArgumentNullException();");
- writer.WriteLine(" this.dispose = dispose && dispatch is global::System.IDisposable;");
- writer.WriteLine("}");
- writer.WriteLine();
-
- writer.WriteLine("public void Dispose() {");
- writer.WriteLine(" if (dispose) ((global::System.IDisposable)dispatch).Dispose();");
- writer.WriteLine("}");
- writer.WriteLine();
- writer.WriteLine("TMessage pb::IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response) {");
- writer.WriteLine(" return dispatch.CallMethod(method, request, response);");
- writer.WriteLine("}");
- writer.WriteLine();
-
- foreach (MethodDescriptor method in Descriptor.Methods) {
- writer.WriteLine("public {0} {1}({2} {3}) {{", GetClassName(method.OutputType), NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType), NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
- writer.WriteLine(" return dispatch.CallMethod(\"{0}\", {1}, {2}.CreateBuilder());",
- method.Name,
- NameHelpers.UnderscoresToCamelCase(method.InputType.Name),
- GetClassName(method.OutputType)
- );
- writer.WriteLine("}");
- writer.WriteLine();
- }
- }
- // SERVER - DISPATCH
- {
- if (Descriptor.File.CSharpOptions.ClsCompliance)
- writer.WriteLine("[global::System.CLSCompliant(false)]");
- writer.WriteLine("public partial class Dispatch : pb::IRpcDispatch, global::System.IDisposable {");
- writer.Indent();
- writer.WriteLine("private readonly bool dispose;");
- writer.WriteLine("private readonly I{0} implementation;", Descriptor.Name);
-
- writer.WriteLine("public Dispatch(I{0} implementation) : this(implementation, true) {{", Descriptor.Name);
- writer.WriteLine("}");
- writer.WriteLine("public Dispatch(I{0} implementation, bool dispose) {{", Descriptor.Name);
- writer.WriteLine(" if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
- writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
- writer.WriteLine("}");
- writer.WriteLine();
-
- writer.WriteLine("public void Dispose() {");
- writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
- writer.WriteLine("}");
- writer.WriteLine();
-
- writer.WriteLine("public TMessage CallMethod<TMessage, TBuilder>(string methodName, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response)");
- writer.WriteLine(" where TMessage : IMessageLite<TMessage, TBuilder>");
- writer.WriteLine(" where TBuilder : IBuilderLite<TMessage, TBuilder> {");
- writer.Indent();
- writer.WriteLine("switch(methodName) {");
- writer.Indent();
-
- foreach (MethodDescriptor method in Descriptor.Methods) {
- writer.WriteLine("case \"{0}\": return response.MergeFrom(implementation.{1}(({2})request)).Build();", method.Name, NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType));
- }
- writer.WriteLine("default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
- writer.Outdent();
- writer.WriteLine("}");//end switch
- writer.Outdent();
- writer.WriteLine("}");//end invoke
- writer.Outdent();
- writer.WriteLine("}");//end server
- }
- // SERVER - STUB
- {
- if (Descriptor.File.CSharpOptions.ClsCompliance)
- writer.WriteLine("[global::System.CLSCompliant(false)]");
- writer.WriteLine("public partial class ServerStub : pb::IRpcServerStub, global::System.IDisposable {");
- writer.Indent();
- writer.WriteLine("private readonly bool dispose;");
- writer.WriteLine("private readonly pb::IRpcDispatch implementation;", Descriptor.Name);
-
- writer.WriteLine("public ServerStub(I{0} implementation) : this(implementation, true) {{", Descriptor.Name);
- writer.WriteLine("}");
- writer.WriteLine("public ServerStub(I{0} implementation, bool dispose) : this(new Dispatch(implementation, dispose), dispose) {{", Descriptor.Name);
- writer.WriteLine("}");
-
- writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation) : this(implementation, true) {");
- writer.WriteLine("}");
- writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation, bool dispose) {");
- writer.WriteLine(" if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
- writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
- writer.WriteLine("}");
- writer.WriteLine();
-
- writer.WriteLine("public void Dispose() {");
- writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
- writer.WriteLine("}");
- writer.WriteLine();
-
- writer.WriteLine("public pb::IMessageLite CallMethod(string methodName, pb::CodedInputStream input, pb::ExtensionRegistry registry) {{", Descriptor.Name);
- writer.Indent();
- writer.WriteLine("switch(methodName) {");
- writer.Indent();
-
- foreach (MethodDescriptor method in Descriptor.Methods)
- {
- writer.WriteLine("case \"{0}\": return implementation.CallMethod(methodName, {1}.ParseFrom(input, registry), {2}.CreateBuilder());", method.Name, GetClassName(method.InputType), GetClassName(method.OutputType));
- }
- writer.WriteLine("default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
- writer.Outdent();
- writer.WriteLine("}");//end switch
- writer.Outdent();
- writer.WriteLine("}");//end invoke
- writer.Outdent();
- writer.WriteLine("}");//end server
- }
+ writer.WriteLine("public {0}(pb::IRpcDispatch dispatch) : this(dispatch, true) {{", Descriptor.Name);
+ writer.WriteLine("}");
+ writer.WriteLine("public {0}(pb::IRpcDispatch dispatch, bool dispose) {{", Descriptor.Name);
+ writer.WriteLine(
+ " if (null == (this.dispatch = dispatch)) throw new global::System.ArgumentNullException();");
+ writer.WriteLine(" this.dispose = dispose && dispatch is global::System.IDisposable;");
+ writer.WriteLine("}");
+ writer.WriteLine();
+
+ writer.WriteLine("public void Dispose() {");
+ writer.WriteLine(" if (dispose) ((global::System.IDisposable)dispatch).Dispose();");
+ writer.WriteLine("}");
+ writer.WriteLine();
+ writer.WriteLine(
+ "TMessage pb::IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response) {");
+ writer.WriteLine(" return dispatch.CallMethod(method, request, response);");
+ writer.WriteLine("}");
+ writer.WriteLine();
+
+ foreach (MethodDescriptor method in Descriptor.Methods)
+ {
+ writer.WriteLine("public {0} {1}({2} {3}) {{", GetClassName(method.OutputType),
+ NameHelpers.UnderscoresToPascalCase(method.Name),
+ GetClassName(method.InputType),
+ NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
+ writer.WriteLine(" return dispatch.CallMethod(\"{0}\", {1}, {2}.CreateBuilder());",
+ method.Name,
+ NameHelpers.UnderscoresToCamelCase(method.InputType.Name),
+ GetClassName(method.OutputType)
+ );
+ writer.WriteLine("}");
+ writer.WriteLine();
+ }
+ }
+ // SERVER - DISPATCH
+ {
+ if (Descriptor.File.CSharpOptions.ClsCompliance)
+ writer.WriteLine("[global::System.CLSCompliant(false)]");
+ writer.WriteLine("public partial class Dispatch : pb::IRpcDispatch, global::System.IDisposable {");
+ writer.Indent();
+ writer.WriteLine("private readonly bool dispose;");
+ writer.WriteLine("private readonly I{0} implementation;", Descriptor.Name);
+
+ writer.WriteLine("public Dispatch(I{0} implementation) : this(implementation, true) {{",
+ Descriptor.Name);
+ writer.WriteLine("}");
+ writer.WriteLine("public Dispatch(I{0} implementation, bool dispose) {{", Descriptor.Name);
+ writer.WriteLine(
+ " if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
+ writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
+ writer.WriteLine("}");
+ writer.WriteLine();
+
+ writer.WriteLine("public void Dispose() {");
+ writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
+ writer.WriteLine("}");
+ writer.WriteLine();
- writer.Outdent();
- writer.WriteLine("}");
- }
+ writer.WriteLine(
+ "public TMessage CallMethod<TMessage, TBuilder>(string methodName, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response)");
+ writer.WriteLine(" where TMessage : IMessageLite<TMessage, TBuilder>");
+ writer.WriteLine(" where TBuilder : IBuilderLite<TMessage, TBuilder> {");
+ writer.Indent();
+ writer.WriteLine("switch(methodName) {");
+ writer.Indent();
+
+ foreach (MethodDescriptor method in Descriptor.Methods)
+ {
+ writer.WriteLine(
+ "case \"{0}\": return response.MergeFrom(implementation.{1}(({2})request)).Build();",
+ method.Name, NameHelpers.UnderscoresToPascalCase(method.Name),
+ GetClassName(method.InputType));
+ }
+ writer.WriteLine(
+ "default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
+ writer.Outdent();
+ writer.WriteLine("}"); //end switch
+ writer.Outdent();
+ writer.WriteLine("}"); //end invoke
+ writer.Outdent();
+ writer.WriteLine("}"); //end server
+ }
+ // SERVER - STUB
+ {
+ if (Descriptor.File.CSharpOptions.ClsCompliance)
+ writer.WriteLine("[global::System.CLSCompliant(false)]");
+ writer.WriteLine(
+ "public partial class ServerStub : pb::IRpcServerStub, global::System.IDisposable {");
+ writer.Indent();
+ writer.WriteLine("private readonly bool dispose;");
+ writer.WriteLine("private readonly pb::IRpcDispatch implementation;", Descriptor.Name);
+
+ writer.WriteLine("public ServerStub(I{0} implementation) : this(implementation, true) {{",
+ Descriptor.Name);
+ writer.WriteLine("}");
+ writer.WriteLine(
+ "public ServerStub(I{0} implementation, bool dispose) : this(new Dispatch(implementation, dispose), dispose) {{",
+ Descriptor.Name);
+ writer.WriteLine("}");
+
+ writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation) : this(implementation, true) {");
+ writer.WriteLine("}");
+ writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation, bool dispose) {");
+ writer.WriteLine(
+ " if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
+ writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
+ writer.WriteLine("}");
+ writer.WriteLine();
+
+ writer.WriteLine("public void Dispose() {");
+ writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
+ writer.WriteLine("}");
+ writer.WriteLine();
+
+ writer.WriteLine(
+ "public pb::IMessageLite CallMethod(string methodName, pb::CodedInputStream input, pb::ExtensionRegistry registry) {{",
+ Descriptor.Name);
+ writer.Indent();
+ writer.WriteLine("switch(methodName) {");
+ writer.Indent();
+
+ foreach (MethodDescriptor method in Descriptor.Methods)
+ {
+ writer.WriteLine(
+ "case \"{0}\": return implementation.CallMethod(methodName, {1}.ParseFrom(input, registry), {2}.CreateBuilder());",
+ method.Name, GetClassName(method.InputType), GetClassName(method.OutputType));
+ }
+ writer.WriteLine(
+ "default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
+ writer.Outdent();
+ writer.WriteLine("}"); //end switch
+ writer.Outdent();
+ writer.WriteLine("}"); //end invoke
+ writer.Outdent();
+ writer.WriteLine("}"); //end server
+ }
+
+ writer.Outdent();
+ writer.WriteLine("}");
+ }
+ }
}
- }
-}
+}
\ No newline at end of file |