Esempio n. 1
0
        public GrpcClient(
            GrpcOptions options,
            GrpcServiceCallBuilder serviceCallBuilder,
            IBinarySerializer binarySerializer)
        {
            _options = options;

            ID             = Guid.NewGuid();
            GrpcServiceDic = new Dictionary <Type, Type>();
            var namespaces = $"Kadder.Client.Services";

            _codeBuilder = new CodeBuilder(namespaces, namespaces);
            var grpcServiceDic = serviceCallBuilder
                                 .GenerateHandler(_options, this, ref _codeBuilder);

            _codeAssembly     = _codeBuilder.BuildAsync().Result;
            _binarySerializer = binarySerializer;

            GrpcClientExtension.ClientDic.Add(ID.ToString(), this);
            foreach (var item in grpcServiceDic)
            {
                var type = _codeAssembly.Assembly.GetType(item.Value);
                GrpcServiceDic.Add(item.Key, type);
            }
        }
Esempio n. 2
0
        public GrpcClientMetadata RegClient(GrpcOptions options)
        {
            var metadata = new GrpcClientMetadata(options);

            ClientMetadatas.Add(metadata);
            return(metadata);
        }
Esempio n. 3
0
        public IDictionary <Type, string> GenerateHandler(
            GrpcOptions options,
            GrpcClient client,
            ref CodeBuilder codeBuilder)
        {
            var types = RefelectionHelper.GetImplInterfaceTypes(
                typeof(IMessagingServicer), true, options.GetScanAssemblies());
            var grpcServiceDic = new Dictionary <Type, string>();

            foreach (var typeService in types)
            {
                var className       = typeService.Name.Remove(0, 1);
                var classDescripter = new ClassDescripter(
                    className,
                    codeBuilder.Namespace)
                                      .SetBaseType(typeService.Name)
                                      .AddUsing("System.Threading.Tasks", typeService.Namespace)
                                      .AddUsing("Kadder")
                                      .SetAccess(AccessType.Public);
                grpcServiceDic.Add(typeService, $"{codeBuilder.Namespace}.{className}");

                var baseInterfaces = typeService.GetInterfaces();
                foreach (var method in typeService.GetMethods())
                {
                    var notGrpcMethodCount = method.CustomAttributes
                                             .Count(p =>
                                                    p.AttributeType == typeof(NotGrpcMethodAttribute));
                    if (notGrpcMethodCount > 0)
                    {
                        continue;
                    }

                    var parameters = method.GetParameters();
                    if (parameters.Length != 1)
                    {
                        continue;
                    }

                    var requestName  = parameters[0].ParameterType.Name.ToLower();
                    var responseType = GetMethodReturn(method.ReturnType);
                    var methodName   = method.Name.Replace("Async", "");
                    classDescripter.CreateMember(
                        new MethodDescripter(method.Name, true)
                        .SetAccess(AccessType.Public)
                        .SetReturn($"Task<{responseType.Name}>")
                        .SetParams(
                            new ParameterDescripter(
                                parameters[0].ParameterType.Name, requestName))
                        .AppendCode($@"var client=GrpcClientExtension.ClientDic[""{client.ID.ToString()}""];")
                        .AppendCode($@"return await client.CallAsync<{parameters[0].ParameterType.Name},{responseType.Name}>({requestName}, ""{methodName}"");"))
                    .AddUsing(responseType.Namespace)
                    .AddUsing(parameters[0].ParameterType.Namespace);
                }
                codeBuilder.CreateClass(classDescripter)
                .AddAssemblyRefence(typeService.Assembly.Location);
            }
            codeBuilder.AddAssemblyRefence(this.GetType().Assembly.Location);
            return(grpcServiceDic);
        }
Esempio n. 4
0
 public GrpcClientOptions(GrpcOptions options) : this()
 {
     Host           = options.Host;
     Port           = options.Port;
     NamespaceName  = options.NamespaceName;
     ServiceName    = options.ServiceName;
     ScanAssemblies = options.ScanAssemblies;
 }
Esempio n. 5
0
        public GrpcClient(GrpcClientMetadata metadata, GrpcClientBuilder builder, GrpcServiceCallBuilder serviceCallBuilder)
        {
            _clientBuilder     = builder;
            _metadata          = metadata;
            _options           = metadata.Options;
            ID                 = Guid.NewGuid();
            _lastConnectedTime = DateTime.Now;
            GrpcServiceDic     = new Dictionary <Type, Type>();
            _oldMethodDic      = new Dictionary <string, string>();
            var namespaces = $"Kadder.Client.Services";

            _codeBuilder = new CodeBuilder(namespaces, namespaces);

            var grpcServiceDic = serviceCallBuilder.GenerateHandler(_options, this, ref _codeBuilder);

            _codeAssembly = _codeBuilder.BuildAsync().Result;
            GrpcClientExtension.ClientDic.Add(ID.ToString(), this);
            foreach (var keyValuePair in grpcServiceDic)
            {
                var type = _codeAssembly.Assembly.GetType(keyValuePair.Value);
                GrpcServiceDic.Add(keyValuePair.Key, type);
            }
        }
        public IDictionary <Type, string> GenerateHandler(
            GrpcOptions options, GrpcClient client, ref CodeBuilder codeBuilder)
        {
            var types          = options.GetKServicers();
            var grpcServiceDic = new Dictionary <Type, string>();

            foreach (var typeService in types)
            {
                var className       = $"{typeService.Name}GrpcService";
                var classDescripter = new ClassDescripter(className, codeBuilder.Namespace)
                                      .SetBaseType(typeService.Name)
                                      .AddUsing("System.Threading.Tasks", typeService.Namespace)
                                      .AddUsing("Kadder")
                                      .SetAccess(AccessType.Public);
                grpcServiceDic.Add(typeService, $"{codeBuilder.Namespace}.{className}");

                var baseInterfaces = typeService.GetInterfaces();
                foreach (var method in typeService.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    var notGrpcMethodCount = method.CustomAttributes.Count(
                        p => p.AttributeType == typeof(NotGrpcMethodAttribute));
                    if (notGrpcMethodCount > 0)
                    {
                        continue;
                    }

                    var parameters = RpcParameterInfo.Convert(method.GetParameters());
                    if (parameters.Count == 0)
                    {
                        var emptyMessageType = typeof(EmptyMessage);
                        parameters.Add(new RpcParameterInfo()
                        {
                            Name          = emptyMessageType.Name,
                            ParameterType = emptyMessageType,
                            IsEmpty       = true
                        });
                    }

                    var requestName    = parameters[0].ParameterType.Name.ToLower();
                    var responseType   = GetMethodReturn(method);
                    var returnTypeCode = $"new Task<{responseType.Name}>";
                    var returnCode     = "return ";
                    var requestCode    = requestName;
                    if (parameters[0].IsEmpty)
                    {
                        requestCode = "new EmptyMessage()";
                    }
                    if (responseType.IsEmpty)
                    {
                        returnTypeCode = $"new {method.ReturnType.Name}";
                        returnCode     = string.Empty;
                    }
                    var methodName       = method.Name.Replace("Async", "");
                    var methodDescripter = new MethodDescripter(method.Name, true)
                                           .SetAccess(AccessType.Public)
                                           .SetReturn(returnTypeCode)
                                           .AppendCode($@"var client = GrpcClientExtension.ClientDic[""{client.ID.ToString()}""];")
                                           .AppendCode($@"{returnCode}await client.CallAsync<{parameters[0].ParameterType.Name},{responseType.Name}>({requestCode}, ""{methodName}"", ""{typeService.Name}"");");
                    if (!parameters[0].IsEmpty)
                    {
                        methodDescripter.SetParams(new ParameterDescripter(parameters[0].ParameterType.Name, requestName));
                    }
                    classDescripter.CreateMember(methodDescripter)
                    .AddUsing(responseType.Namespace).AddUsing(parameters[0].ParameterType.Namespace);
                }
                codeBuilder.CreateClass(classDescripter)
                .AddAssemblyRefence(typeService.Assembly.Location);
            }
            codeBuilder.AddAssemblyRefence(this.GetType().Assembly.Location);
            return(grpcServiceDic);
        }
Esempio n. 7
0
 public GrpcClientMetadata(GrpcOptions options)
 {
     Options             = options;
     PrivateInterceptors = new List <Type>();
 }
Esempio n. 8
0
 public GrpcClientMetadata RegClient(GrpcOptions options)
 {
     return(RegClient(new GrpcClientOptions(options)));
 }
Esempio n. 9
0
 public GrpcClientBuilder RegClient(GrpcOptions options)
 {
     _clientOptions.Add(options);
     return(this);
 }