ServiceGenerator.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Google.ProtocolBuffers.Descriptors;
  35. namespace Google.ProtocolBuffers.ProtoGen
  36. {
  37. internal class GenericServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
  38. {
  39. private enum RequestOrResponse
  40. {
  41. Request,
  42. Response
  43. }
  44. internal GenericServiceGenerator(ServiceDescriptor descriptor)
  45. : base(descriptor)
  46. {
  47. }
  48. public void Generate(TextGenerator writer)
  49. {
  50. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  51. WriteGeneratedCodeAttributes(writer);
  52. writer.WriteLine("{0} abstract class {1} : pb::IService {{", ClassAccessLevel, Descriptor.Name);
  53. writer.Indent();
  54. foreach (MethodDescriptor method in Descriptor.Methods)
  55. {
  56. writer.WriteLine("{0} abstract void {1}(", ClassAccessLevel,
  57. NameHelpers.UnderscoresToPascalCase(method.Name));
  58. writer.WriteLine(" pb::IRpcController controller,");
  59. writer.WriteLine(" {0} request,", GetClassName(method.InputType));
  60. writer.WriteLine(" global::System.Action<{0}> done);", GetClassName(method.OutputType));
  61. }
  62. // Generate Descriptor and DescriptorForType.
  63. writer.WriteLine();
  64. writer.WriteLine("{0} static pbd::ServiceDescriptor Descriptor {{", ClassAccessLevel);
  65. writer.WriteLine(" get {{ return {0}.Descriptor.Services[{1}]; }}",
  66. DescriptorUtil.GetQualifiedUmbrellaClassName(Descriptor.File.CSharpOptions),
  67. Descriptor.Index);
  68. writer.WriteLine("}");
  69. writer.WriteLine("public pbd::ServiceDescriptor DescriptorForType {");
  70. writer.WriteLine(" get { return Descriptor; }");
  71. writer.WriteLine("}");
  72. GenerateCallMethod(writer);
  73. GenerateGetPrototype(RequestOrResponse.Request, writer);
  74. GenerateGetPrototype(RequestOrResponse.Response, writer);
  75. GenerateStub(writer);
  76. writer.Outdent();
  77. writer.WriteLine("}");
  78. }
  79. private void GenerateCallMethod(TextGenerator writer)
  80. {
  81. writer.WriteLine();
  82. writer.WriteLine("public void CallMethod(");
  83. writer.WriteLine(" pbd::MethodDescriptor method,");
  84. writer.WriteLine(" pb::IRpcController controller,");
  85. writer.WriteLine(" pb::IMessage request,");
  86. writer.WriteLine(" global::System.Action<pb::IMessage> done) {");
  87. writer.Indent();
  88. writer.WriteLine("if (method.Service != Descriptor) {");
  89. writer.WriteLine(" throw new global::System.ArgumentException(");
  90. writer.WriteLine(" \"Service.CallMethod() given method descriptor for wrong service type.\");");
  91. writer.WriteLine("}");
  92. writer.WriteLine("switch(method.Index) {");
  93. writer.Indent();
  94. foreach (MethodDescriptor method in Descriptor.Methods)
  95. {
  96. writer.WriteLine("case {0}:", method.Index);
  97. writer.WriteLine(" this.{0}(controller, ({1}) request,",
  98. NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType));
  99. writer.WriteLine(" pb::RpcUtil.SpecializeCallback<{0}>(", GetClassName(method.OutputType));
  100. writer.WriteLine(" done));");
  101. writer.WriteLine(" return;");
  102. }
  103. writer.WriteLine("default:");
  104. writer.WriteLine(" throw new global::System.InvalidOperationException(\"Can't get here.\");");
  105. writer.Outdent();
  106. writer.WriteLine("}");
  107. writer.Outdent();
  108. writer.WriteLine("}");
  109. writer.WriteLine();
  110. }
  111. private void GenerateGetPrototype(RequestOrResponse which, TextGenerator writer)
  112. {
  113. writer.WriteLine("public pb::IMessage Get{0}Prototype(pbd::MethodDescriptor method) {{", which);
  114. writer.Indent();
  115. writer.WriteLine("if (method.Service != Descriptor) {");
  116. writer.WriteLine(" throw new global::System.ArgumentException(");
  117. writer.WriteLine(" \"Service.Get{0}Prototype() given method descriptor for wrong service type.\");",
  118. which);
  119. writer.WriteLine("}");
  120. writer.WriteLine("switch(method.Index) {");
  121. writer.Indent();
  122. foreach (MethodDescriptor method in Descriptor.Methods)
  123. {
  124. writer.WriteLine("case {0}:", method.Index);
  125. writer.WriteLine(" return {0}.DefaultInstance;",
  126. GetClassName(which == RequestOrResponse.Request ? method.InputType : method.OutputType));
  127. }
  128. writer.WriteLine("default:");
  129. writer.WriteLine(" throw new global::System.InvalidOperationException(\"Can't get here.\");");
  130. writer.Outdent();
  131. writer.WriteLine("}");
  132. writer.Outdent();
  133. writer.WriteLine("}");
  134. writer.WriteLine();
  135. }
  136. private void GenerateStub(TextGenerator writer)
  137. {
  138. writer.WriteLine("public static Stub CreateStub(pb::IRpcChannel channel) {");
  139. writer.WriteLine(" return new Stub(channel);");
  140. writer.WriteLine("}");
  141. writer.WriteLine();
  142. writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
  143. WriteGeneratedCodeAttributes(writer);
  144. writer.WriteLine("{0} class Stub : {1} {{", ClassAccessLevel, GetClassName(Descriptor));
  145. writer.Indent();
  146. writer.WriteLine("internal Stub(pb::IRpcChannel channel) {");
  147. writer.WriteLine(" this.channel = channel;");
  148. writer.WriteLine("}");
  149. writer.WriteLine();
  150. writer.WriteLine("private readonly pb::IRpcChannel channel;");
  151. writer.WriteLine();
  152. writer.WriteLine("public pb::IRpcChannel Channel {");
  153. writer.WriteLine(" get { return channel; }");
  154. writer.WriteLine("}");
  155. foreach (MethodDescriptor method in Descriptor.Methods)
  156. {
  157. writer.WriteLine();
  158. writer.WriteLine("{0} override void {1}(", ClassAccessLevel,
  159. NameHelpers.UnderscoresToPascalCase(method.Name));
  160. writer.WriteLine(" pb::IRpcController controller,");
  161. writer.WriteLine(" {0} request,", GetClassName(method.InputType));
  162. writer.WriteLine(" global::System.Action<{0}> done) {{", GetClassName(method.OutputType));
  163. writer.Indent();
  164. writer.WriteLine("channel.CallMethod(Descriptor.Methods[{0}],", method.Index);
  165. writer.WriteLine(" controller, request, {0}.DefaultInstance,", GetClassName(method.OutputType));
  166. writer.WriteLine(" pb::RpcUtil.GeneralizeCallback<{0}, {0}.Builder>(done, {0}.DefaultInstance));",
  167. GetClassName(method.OutputType));
  168. writer.Outdent();
  169. writer.WriteLine("}");
  170. }
  171. writer.Outdent();
  172. writer.WriteLine("}");
  173. }
  174. }
  175. }