protected override void WriteServiceMethod(GeneratorContext ctx, MethodDescriptorProto method, ref object state)
        {
            var outputType = MakeRelativeName(ctx, method.OutputType);
            var inputType  = MakeRelativeName(ctx, method.InputType);

            ctx.WriteLine($"{Escape( outputType )} {Escape( method.Name )}({Escape( inputType )} request);");
        }
 internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file,
                           ServiceDescriptor parent, int index)
     : base(file, parent.FullName + "." + proto.Name, index)
 {
     this.proto = proto;
     service    = parent;
     file.DescriptorPool.AddSymbol(this);
 }
Example #3
0
 protected virtual void WriteMethodDoc(
     StreamWriter writer,
     bool isClient,
     bool unpack,
     List <NameInfo> names,
     MethodDescriptorProto method,
     int methodIndex,
     params (string name, string doc)?[] additional
Example #4
0
 internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file,
                           ServiceDescriptor parent, int index)
     : base(file, parent.FullName + "." + proto.Name, index)
 {
     this.proto = proto;
     service = parent;
     file.DescriptorPool.AddSymbol(this);
 }
Example #5
0
 public GrpcMethodDefinition(string serviceName, MethodDescriptorProto methodDescriptorProto)
 {
     ServiceName = serviceName;
     Name        = methodDescriptorProto.Name;
     Type        = methodDescriptorProto.ClientStreaming
         ? (methodDescriptorProto.ServerStreaming ? MethodType.DuplexStreaming : MethodType.ClientStreaming)
         : (methodDescriptorProto.ServerStreaming ? MethodType.ServerStreaming : MethodType.Unary);
 }
        /// <summary>
        /// Suggest a normalized identifier
        /// </summary>
        public virtual string GetName(MethodDescriptorProto definition)
        {
            var name = definition?.Options?.GetOptions()?.Name;

            if (!string.IsNullOrWhiteSpace(name))
            {
                return(name);
            }
            return(GetName(definition.Name));
        }
Example #7
0
        protected override void GenerateServerServiceMethods(
            FileDescriptorProto file,
            ServiceDescriptorProto service,
            StreamWriter writer,
            List <NameInfo> names,
            MethodDescriptorProto method,
            int methodIndex)
        {
            var requestType = names
                              .Where(x => x.ProtoBufName == method.InputType)
                              .Select(x => x.CSharpName)
                              .FirstOrDefault();
            var responseType = names
                               .Where(x => x.ProtoBufName == method.OutputType)
                               .Select(x => x.CSharpName)
                               .FirstOrDefault();

            if (requestType is null)
            {
                Log.WriteError(text: $"c# type for protobuf message {method.InputType} not found",
                               file: file.Name);
                return;
            }
            if (responseType is null)
            {
                Log.WriteError(text: $"c# type for protobuf message {method.OutputType} not found",
                               file: file.Name);
                return;
            }
            var resp = Settings.EmptySupport && method.OutputType == ".google.protobuf.Empty"
                ? ""
                : $"<{responseType}{Nullable}>";
            var req = Settings.EmptySupport && method.InputType == ".google.protobuf.Empty"
                ? ""
                : $"{requestType} request, ";

            writer.WriteLine();
            WriteMethodDoc(writer, false, false, names, method, methodIndex,
                           ("request", "The api request object"),
                           ("cancellationToken", "The token that signals the cancellation of the request")
                           );
            writer.WriteLines(
                $"\t\tpublic abstract stt::Task{resp} {method.Name}({req}st::CancellationToken cancellationToken);"
                );
        }
        /// <summary>Serialize the instance into the stream</summary>
        public static void Serialize(Stream stream, MethodDescriptorProto instance)
        {
            var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop();
            if (instance.Name != null)
            {
                // Key for field: 1, LengthDelimited
                stream.WriteByte(10);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name));
            }
            if (instance.InputType != null)
            {
                // Key for field: 2, LengthDelimited
                stream.WriteByte(18);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.InputType));
            }
            if (instance.OutputType != null)
            {
                // Key for field: 3, LengthDelimited
                stream.WriteByte(26);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.OutputType));
            }
            if (instance.Options != null)
            {
                // Key for field: 4, LengthDelimited
                stream.WriteByte(34);
                msField.SetLength(0);
                Google.Protobuf.MethodOptions.Serialize(msField, instance.Options);
                // Length delimited byte array
                uint length4 = (uint)msField.Length;
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length4);
                msField.WriteTo(stream);

            }
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField);
        }
 /// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary>
 public static byte[] SerializeToBytes(MethodDescriptorProto instance)
 {
     using (var ms = new MemoryStream())
     {
         Serialize(ms, instance);
         return ms.ToArray();
     }
 }
        /// <summary>
        /// Emit code representing a service method
        /// </summary>
        protected override void WriteServiceMethod(GeneratorContext ctx, MethodDescriptorProto method, ref object state)
        {
            var name = ctx.NameNormalizer.GetName(method);

            if (name != method.Name)
            {
                ctx.WriteLine($@"[global::System.ServiceModel.OperationContract(Name = @""{method.Name}"")]");
            }
            WriteOptions(ctx, method.Options);

            string returnType, inputType;

            if (method.ServerStreaming)
            {
                returnType = "global::System.Collection.Generics.IAsyncEnumerable<" + GetTypeName(ctx, method.OutputType) + ">";
            }
            else
            {
                if (method.OutputType == WellKnownTypeEmpty)
                {
                    returnType = "global::System.Threading.Tasks.ValueTask";
                }
                else
                {
                    returnType = "global::System.Threading.Tasks.ValueTask<" + GetTypeName(ctx, method.OutputType) + ">";
                }
            }
            if (method.ClientStreaming)
            {
                inputType = "global::System.Collection.Generics.IAsyncEnumerable<" + GetTypeName(ctx, method.InputType) + ">";
            }
            else
            {
                if (method.InputType == WellKnownTypeEmpty)
                {
                    inputType = null;
                }
                else
                {
                    inputType = GetTypeName(ctx, method.InputType);
                }
            }

            var tw = ctx.Write($"{returnType} {Escape(name)}Async(");

            if (inputType != null)
            {
                tw.Write(inputType);
                tw.Write(method.ClientStreaming ? " values, " : " value, ");
            }
            tw.Write("global::ProtoBuf.Grpc.CallContext context");
            if (ctx.Supports(CSharp4))
            {
                tw.Write(" = default");
                if (!ctx.Supports(CSharp7_1))
                {
                    tw.Write("(global::ProtoBuf.Grpc.CallContext)");
                }
            }
            tw.WriteLine(");");
        }
        /// <summary>Serialize the instance into the stream</summary>
        public static void Serialize(Stream stream, MethodDescriptorProto instance)
        {
            if (instance.Name != null)
            {
                // Key for field: 1, LengthDelimited
                stream.WriteByte(10);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name));
            }
            if (instance.InputType != null)
            {
                // Key for field: 2, LengthDelimited
                stream.WriteByte(18);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.InputType));
            }
            if (instance.OutputType != null)
            {
                // Key for field: 3, LengthDelimited
                stream.WriteByte(26);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.OutputType));
            }
            if (instance.Options != null)
            {
                // Key for field: 4, LengthDelimited
                stream.WriteByte(34);
                using (var ms4 = new MemoryStream())
                {
                    Google.protobuf.MethodOptions.Serialize(ms4, instance.Options);
                    // Length delimited byte array
                    uint ms4Length = (uint)ms4.Length;
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms4Length);
                    stream.Write(ms4.GetBuffer(), 0, (int)ms4Length);
                }

            }
        }
 /// <summary>Helper: Serialize with a varint length prefix</summary>
 public static void SerializeLengthDelimited(Stream stream, MethodDescriptorProto instance)
 {
     var data = SerializeToBytes(instance);
     global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length);
     stream.Write(data, 0, data.Length);
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MethodDescriptorProto DeserializeLengthDelimited(Stream stream)
 {
     MethodDescriptorProto instance = new MethodDescriptorProto();
     DeserializeLengthDelimited(stream, instance);
     return instance;
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MethodDescriptorProto DeserializeLength(Stream stream, int length)
 {
     MethodDescriptorProto instance = new MethodDescriptorProto();
     DeserializeLength(stream, length, instance);
     return instance;
 }
 /// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary>
 public static MethodDescriptorProto Deserialize(byte[] buffer)
 {
     MethodDescriptorProto instance = new MethodDescriptorProto();
     using (var ms = new MemoryStream(buffer))
         Deserialize(ms, instance);
     return instance;
 }
Example #16
0
 /// <summary>
 /// Emit code representing a service method
 /// </summary>
 protected virtual void WriteServiceMethod(GeneratorContext ctx, MethodDescriptorProto inner, ref object state)
 {
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MethodDescriptorProto Deserialize(Stream stream)
 {
     var instance = new MethodDescriptorProto();
     Deserialize(stream, instance);
     return instance;
 }