Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/ServiceInterfaceGenerator.cs @ 13957

Last change on this file since 13957 was 8295, checked in by abeham, 12 years ago

#1897:

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