ServiceInterfaceGenerator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using Google.ProtocolBuffers.DescriptorProtos;
  36. using Google.ProtocolBuffers.Descriptors;
  37. namespace Google.ProtocolBuffers.ProtoGen
  38. {
  39. internal class ServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
  40. {
  41. private readonly CSharpServiceType svcType;
  42. private ISourceGenerator _generator;
  43. internal ServiceGenerator(ServiceDescriptor descriptor)
  44. : base(descriptor)
  45. {
  46. svcType = descriptor.File.CSharpOptions.ServiceGeneratorType;
  47. switch (svcType)
  48. {
  49. case CSharpServiceType.NONE:
  50. _generator = new NoServicesGenerator(descriptor);
  51. break;
  52. case CSharpServiceType.GENERIC:
  53. _generator = new GenericServiceGenerator(descriptor);
  54. break;
  55. case CSharpServiceType.INTERFACE:
  56. _generator = new ServiceInterfaceGenerator(descriptor);
  57. break;
  58. case CSharpServiceType.IRPCDISPATCH:
  59. _generator = new RpcServiceGenerator(descriptor);
  60. break;
  61. default:
  62. throw new ApplicationException("Unknown ServiceGeneratorType = " + svcType.ToString());
  63. }
  64. }
  65. public void Generate(TextGenerator writer)
  66. {
  67. _generator.Generate(writer);
  68. }
  69. private class NoServicesGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
  70. {
  71. public NoServicesGenerator(ServiceDescriptor descriptor)
  72. : base(descriptor)
  73. {
  74. }
  75. public virtual void Generate(TextGenerator writer)
  76. {
  77. writer.WriteLine("/*");
  78. writer.WriteLine("* Service generation is now disabled by default, use the following option to enable:");
  79. writer.WriteLine("* option (google.protobuf.csharp_file_options).service_generator_type = GENERIC;");
  80. writer.WriteLine("*/");
  81. }
  82. }
  83. private class ServiceInterfaceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
  84. {
  85. public ServiceInterfaceGenerator(ServiceDescriptor descriptor)
  86. : base(descriptor)
  87. {
  88. }
  89. public virtual void Generate(TextGenerator writer)
  90. {
  91. CSharpServiceOptions options = Descriptor.Options.GetExtension(CSharpOptions.CsharpServiceOptions);
  92. if (options != null && options.HasInterfaceId)
  93. {
  94. writer.WriteLine("[global::System.Runtime.InteropServices.GuidAttribute(\"{0}\")]",
  95. new Guid(options.InterfaceId));
  96. }
  97. WriteGeneratedCodeAttributes(writer);
  98. writer.WriteLine("{0} partial interface I{1} {{", ClassAccessLevel, Descriptor.Name);
  99. writer.Indent();
  100. foreach (MethodDescriptor method in Descriptor.Methods)
  101. {
  102. CSharpMethodOptions mth = method.Options.GetExtension(CSharpOptions.CsharpMethodOptions);
  103. if (mth.HasDispatchId)
  104. {
  105. writer.WriteLine("[global::System.Runtime.InteropServices.DispId({0})]", mth.DispatchId);
  106. }
  107. writer.WriteLine("{0} {1}({2} {3});", GetClassName(method.OutputType),
  108. NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType),
  109. NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
  110. }
  111. writer.Outdent();
  112. writer.WriteLine("}");
  113. }
  114. }
  115. private class RpcServiceGenerator : ServiceInterfaceGenerator
  116. {
  117. public RpcServiceGenerator(ServiceDescriptor descriptor)
  118. : base(descriptor)
  119. {
  120. }
  121. public override void Generate(TextGenerator writer)
  122. {
  123. base.Generate(writer);
  124. writer.WriteLine();
  125. // CLIENT Proxy
  126. {
  127. if (Descriptor.File.CSharpOptions.ClsCompliance)
  128. {
  129. writer.WriteLine("[global::System.CLSCompliant(false)]");
  130. }
  131. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  132. WriteGeneratedCodeAttributes(writer);
  133. writer.WriteLine("{0} partial class {1} : I{1}, pb::IRpcDispatch, global::System.IDisposable {{",
  134. ClassAccessLevel, Descriptor.Name);
  135. writer.Indent();
  136. writer.WriteLine("private readonly bool dispose;");
  137. writer.WriteLine("private readonly pb::IRpcDispatch dispatch;");
  138. writer.WriteLine("public {0}(pb::IRpcDispatch dispatch) : this(dispatch, true) {{", Descriptor.Name);
  139. writer.WriteLine("}");
  140. writer.WriteLine("public {0}(pb::IRpcDispatch dispatch, bool dispose) {{", Descriptor.Name);
  141. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.dispatch = dispatch, \"dispatch\");");
  142. writer.WriteLine(" this.dispose = dispose && dispatch is global::System.IDisposable;");
  143. writer.WriteLine("}");
  144. writer.WriteLine();
  145. writer.WriteLine("public void Dispose() {");
  146. writer.WriteLine(" if (dispose) ((global::System.IDisposable)dispatch).Dispose();");
  147. writer.WriteLine("}");
  148. writer.WriteLine();
  149. writer.WriteLine(
  150. "TMessage pb::IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response) {");
  151. writer.WriteLine(" return dispatch.CallMethod(method, request, response);");
  152. writer.WriteLine("}");
  153. writer.WriteLine();
  154. foreach (MethodDescriptor method in Descriptor.Methods)
  155. {
  156. writer.WriteLine("public {0} {1}({2} {3}) {{", GetClassName(method.OutputType),
  157. NameHelpers.UnderscoresToPascalCase(method.Name),
  158. GetClassName(method.InputType),
  159. NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
  160. writer.WriteLine(" return dispatch.CallMethod(\"{0}\", {1}, {2}.CreateBuilder());",
  161. method.Name,
  162. NameHelpers.UnderscoresToCamelCase(method.InputType.Name),
  163. GetClassName(method.OutputType)
  164. );
  165. writer.WriteLine("}");
  166. writer.WriteLine();
  167. }
  168. }
  169. // SERVER - DISPATCH
  170. {
  171. if (Descriptor.File.CSharpOptions.ClsCompliance)
  172. {
  173. writer.WriteLine("[global::System.CLSCompliant(false)]");
  174. }
  175. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  176. WriteGeneratedCodeAttributes(writer);
  177. writer.WriteLine("public partial class Dispatch : pb::IRpcDispatch, global::System.IDisposable {");
  178. writer.Indent();
  179. writer.WriteLine("private readonly bool dispose;");
  180. writer.WriteLine("private readonly I{0} implementation;", Descriptor.Name);
  181. writer.WriteLine("public Dispatch(I{0} implementation) : this(implementation, true) {{",
  182. Descriptor.Name);
  183. writer.WriteLine("}");
  184. writer.WriteLine("public Dispatch(I{0} implementation, bool dispose) {{", Descriptor.Name);
  185. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.implementation = implementation, \"implementation\");");
  186. writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
  187. writer.WriteLine("}");
  188. writer.WriteLine();
  189. writer.WriteLine("public void Dispose() {");
  190. writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
  191. writer.WriteLine("}");
  192. writer.WriteLine();
  193. writer.WriteLine(
  194. "public TMessage CallMethod<TMessage, TBuilder>(string methodName, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response)");
  195. writer.WriteLine(" where TMessage : pb::IMessageLite<TMessage, TBuilder>");
  196. writer.WriteLine(" where TBuilder : pb::IBuilderLite<TMessage, TBuilder> {");
  197. writer.Indent();
  198. writer.WriteLine("switch(methodName) {");
  199. writer.Indent();
  200. foreach (MethodDescriptor method in Descriptor.Methods)
  201. {
  202. writer.WriteLine(
  203. "case \"{0}\": return response.MergeFrom(implementation.{1}(({2})request)).Build();",
  204. method.Name, NameHelpers.UnderscoresToPascalCase(method.Name),
  205. GetClassName(method.InputType));
  206. }
  207. writer.WriteLine("default: throw pb::ThrowHelper.CreateMissingMethod(typeof(I{0}), methodName);", Descriptor.Name);
  208. writer.Outdent();
  209. writer.WriteLine("}"); //end switch
  210. writer.Outdent();
  211. writer.WriteLine("}"); //end invoke
  212. writer.Outdent();
  213. writer.WriteLine("}"); //end server
  214. }
  215. // SERVER - STUB
  216. {
  217. if (Descriptor.File.CSharpOptions.ClsCompliance)
  218. {
  219. writer.WriteLine("[global::System.CLSCompliant(false)]");
  220. }
  221. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  222. WriteGeneratedCodeAttributes(writer);
  223. writer.WriteLine(
  224. "public partial class ServerStub : pb::IRpcServerStub, global::System.IDisposable {");
  225. writer.Indent();
  226. writer.WriteLine("private readonly bool dispose;");
  227. writer.WriteLine("private readonly pb::IRpcDispatch implementation;", Descriptor.Name);
  228. writer.WriteLine("public ServerStub(I{0} implementation) : this(implementation, true) {{",
  229. Descriptor.Name);
  230. writer.WriteLine("}");
  231. writer.WriteLine(
  232. "public ServerStub(I{0} implementation, bool dispose) : this(new Dispatch(implementation, dispose), dispose) {{",
  233. Descriptor.Name);
  234. writer.WriteLine("}");
  235. writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation) : this(implementation, true) {");
  236. writer.WriteLine("}");
  237. writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation, bool dispose) {");
  238. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.implementation = implementation, \"implementation\");");
  239. writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
  240. writer.WriteLine("}");
  241. writer.WriteLine();
  242. writer.WriteLine("public void Dispose() {");
  243. writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
  244. writer.WriteLine("}");
  245. writer.WriteLine();
  246. writer.WriteLine(
  247. "public pb::IMessageLite CallMethod(string methodName, pb::ICodedInputStream input, pb::ExtensionRegistry registry) {{",
  248. Descriptor.Name);
  249. writer.Indent();
  250. writer.WriteLine("switch(methodName) {");
  251. writer.Indent();
  252. foreach (MethodDescriptor method in Descriptor.Methods)
  253. {
  254. writer.WriteLine(
  255. "case \"{0}\": return implementation.CallMethod(methodName, {1}.ParseFrom(input, registry), {2}.CreateBuilder());",
  256. method.Name, GetClassName(method.InputType), GetClassName(method.OutputType));
  257. }
  258. writer.WriteLine("default: throw pb::ThrowHelper.CreateMissingMethod(typeof(I{0}), methodName);", Descriptor.Name);
  259. writer.Outdent();
  260. writer.WriteLine("}"); //end switch
  261. writer.Outdent();
  262. writer.WriteLine("}"); //end invoke
  263. writer.Outdent();
  264. writer.WriteLine("}"); //end server
  265. }
  266. writer.Outdent();
  267. writer.WriteLine("}");
  268. }
  269. }
  270. }
  271. }