PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ProtoGen/ServiceInterfaceGenerator.cs

https://code.google.com/p/protobuf-csharp-port/
C# | 308 lines | 245 code | 29 blank | 34 comment | 8 complexity | c49b6ecaf1d153f208004f7b63c8e33a MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-2.0
  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. writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
  98. writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
  99. GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
  100. writer.WriteLine("{0} partial interface I{1} {{", ClassAccessLevel, Descriptor.Name);
  101. writer.Indent();
  102. foreach (MethodDescriptor method in Descriptor.Methods)
  103. {
  104. CSharpMethodOptions mth = method.Options.GetExtension(CSharpOptions.CsharpMethodOptions);
  105. if (mth.HasDispatchId)
  106. {
  107. writer.WriteLine("[global::System.Runtime.InteropServices.DispId({0})]", mth.DispatchId);
  108. }
  109. writer.WriteLine("{0} {1}({2} {3});", GetClassName(method.OutputType),
  110. NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType),
  111. NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
  112. }
  113. writer.Outdent();
  114. writer.WriteLine("}");
  115. }
  116. }
  117. private class RpcServiceGenerator : ServiceInterfaceGenerator
  118. {
  119. public RpcServiceGenerator(ServiceDescriptor descriptor)
  120. : base(descriptor)
  121. {
  122. }
  123. public override void Generate(TextGenerator writer)
  124. {
  125. base.Generate(writer);
  126. writer.WriteLine();
  127. // CLIENT Proxy
  128. {
  129. if (Descriptor.File.CSharpOptions.ClsCompliance)
  130. {
  131. writer.WriteLine("[global::System.CLSCompliant(false)]");
  132. }
  133. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  134. writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
  135. writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
  136. GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
  137. writer.WriteLine("{0} partial class {1} : I{1}, pb::IRpcDispatch, global::System.IDisposable {{",
  138. ClassAccessLevel, Descriptor.Name);
  139. writer.Indent();
  140. writer.WriteLine("private readonly bool dispose;");
  141. writer.WriteLine("private readonly pb::IRpcDispatch dispatch;");
  142. writer.WriteLine("public {0}(pb::IRpcDispatch dispatch) : this(dispatch, true) {{", Descriptor.Name);
  143. writer.WriteLine("}");
  144. writer.WriteLine("public {0}(pb::IRpcDispatch dispatch, bool dispose) {{", Descriptor.Name);
  145. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.dispatch = dispatch, \"dispatch\");");
  146. writer.WriteLine(" this.dispose = dispose && dispatch is global::System.IDisposable;");
  147. writer.WriteLine("}");
  148. writer.WriteLine();
  149. writer.WriteLine("public void Dispose() {");
  150. writer.WriteLine(" if (dispose) ((global::System.IDisposable)dispatch).Dispose();");
  151. writer.WriteLine("}");
  152. writer.WriteLine();
  153. writer.WriteLine(
  154. "TMessage pb::IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response) {");
  155. writer.WriteLine(" return dispatch.CallMethod(method, request, response);");
  156. writer.WriteLine("}");
  157. writer.WriteLine();
  158. foreach (MethodDescriptor method in Descriptor.Methods)
  159. {
  160. writer.WriteLine("public {0} {1}({2} {3}) {{", GetClassName(method.OutputType),
  161. NameHelpers.UnderscoresToPascalCase(method.Name),
  162. GetClassName(method.InputType),
  163. NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
  164. writer.WriteLine(" return dispatch.CallMethod(\"{0}\", {1}, {2}.CreateBuilder());",
  165. method.Name,
  166. NameHelpers.UnderscoresToCamelCase(method.InputType.Name),
  167. GetClassName(method.OutputType)
  168. );
  169. writer.WriteLine("}");
  170. writer.WriteLine();
  171. }
  172. }
  173. // SERVER - DISPATCH
  174. {
  175. if (Descriptor.File.CSharpOptions.ClsCompliance)
  176. {
  177. writer.WriteLine("[global::System.CLSCompliant(false)]");
  178. }
  179. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  180. writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
  181. writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
  182. GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
  183. writer.WriteLine("public partial class Dispatch : pb::IRpcDispatch, global::System.IDisposable {");
  184. writer.Indent();
  185. writer.WriteLine("private readonly bool dispose;");
  186. writer.WriteLine("private readonly I{0} implementation;", Descriptor.Name);
  187. writer.WriteLine("public Dispatch(I{0} implementation) : this(implementation, true) {{",
  188. Descriptor.Name);
  189. writer.WriteLine("}");
  190. writer.WriteLine("public Dispatch(I{0} implementation, bool dispose) {{", Descriptor.Name);
  191. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.implementation = implementation, \"implementation\");");
  192. writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
  193. writer.WriteLine("}");
  194. writer.WriteLine();
  195. writer.WriteLine("public void Dispose() {");
  196. writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
  197. writer.WriteLine("}");
  198. writer.WriteLine();
  199. writer.WriteLine(
  200. "public TMessage CallMethod<TMessage, TBuilder>(string methodName, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response)");
  201. writer.WriteLine(" where TMessage : pb::IMessageLite<TMessage, TBuilder>");
  202. writer.WriteLine(" where TBuilder : pb::IBuilderLite<TMessage, TBuilder> {");
  203. writer.Indent();
  204. writer.WriteLine("switch(methodName) {");
  205. writer.Indent();
  206. foreach (MethodDescriptor method in Descriptor.Methods)
  207. {
  208. writer.WriteLine(
  209. "case \"{0}\": return response.MergeFrom(implementation.{1}(({2})request)).Build();",
  210. method.Name, NameHelpers.UnderscoresToPascalCase(method.Name),
  211. GetClassName(method.InputType));
  212. }
  213. writer.WriteLine("default: throw pb::ThrowHelper.CreateMissingMethod(typeof(I{0}), methodName);", Descriptor.Name);
  214. writer.Outdent();
  215. writer.WriteLine("}"); //end switch
  216. writer.Outdent();
  217. writer.WriteLine("}"); //end invoke
  218. writer.Outdent();
  219. writer.WriteLine("}"); //end server
  220. }
  221. // SERVER - STUB
  222. {
  223. if (Descriptor.File.CSharpOptions.ClsCompliance)
  224. {
  225. writer.WriteLine("[global::System.CLSCompliant(false)]");
  226. }
  227. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  228. writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
  229. writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
  230. GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
  231. writer.WriteLine(
  232. "public partial class ServerStub : pb::IRpcServerStub, global::System.IDisposable {");
  233. writer.Indent();
  234. writer.WriteLine("private readonly bool dispose;");
  235. writer.WriteLine("private readonly pb::IRpcDispatch implementation;", Descriptor.Name);
  236. writer.WriteLine("public ServerStub(I{0} implementation) : this(implementation, true) {{",
  237. Descriptor.Name);
  238. writer.WriteLine("}");
  239. writer.WriteLine(
  240. "public ServerStub(I{0} implementation, bool dispose) : this(new Dispatch(implementation, dispose), dispose) {{",
  241. Descriptor.Name);
  242. writer.WriteLine("}");
  243. writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation) : this(implementation, true) {");
  244. writer.WriteLine("}");
  245. writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation, bool dispose) {");
  246. writer.WriteLine(" pb::ThrowHelper.ThrowIfNull(this.implementation = implementation, \"implementation\");");
  247. writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
  248. writer.WriteLine("}");
  249. writer.WriteLine();
  250. writer.WriteLine("public void Dispose() {");
  251. writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
  252. writer.WriteLine("}");
  253. writer.WriteLine();
  254. writer.WriteLine(
  255. "public pb::IMessageLite CallMethod(string methodName, pb::ICodedInputStream input, pb::ExtensionRegistry registry) {{",
  256. Descriptor.Name);
  257. writer.Indent();
  258. writer.WriteLine("switch(methodName) {");
  259. writer.Indent();
  260. foreach (MethodDescriptor method in Descriptor.Methods)
  261. {
  262. writer.WriteLine(
  263. "case \"{0}\": return implementation.CallMethod(methodName, {1}.ParseFrom(input, registry), {2}.CreateBuilder());",
  264. method.Name, GetClassName(method.InputType), GetClassName(method.OutputType));
  265. }
  266. writer.WriteLine("default: throw pb::ThrowHelper.CreateMissingMethod(typeof(I{0}), methodName);", Descriptor.Name);
  267. writer.Outdent();
  268. writer.WriteLine("}"); //end switch
  269. writer.Outdent();
  270. writer.WriteLine("}"); //end invoke
  271. writer.Outdent();
  272. writer.WriteLine("}"); //end server
  273. }
  274. writer.Outdent();
  275. writer.WriteLine("}");
  276. }
  277. }
  278. }
  279. }