Beispiel #1
0
        private IEnumerable <string> GenerateEvent(short methodID, System.Reflection.EventInfo eventInfo)
        {
            var privateEvent = $"_{eventInfo.Name}";

            var codeLines = new List <string>();

            codeLines.Add($"private event EventHandler<{eventInfo.ArgsTypeName()}> {privateEvent};");
            codeLines.Add($"public event EventHandler<{eventInfo.ArgsTypeName()}> {eventInfo.Name}");
            codeLines.Add("{");

            codeLines.Add("add{");
            codeLines.Add($"Add_{eventInfo.Name}();");
            codeLines.Add($"{privateEvent} += value;");
            codeLines.Add("}");

            codeLines.Add("remove{");
            codeLines.Add($"Remove_{eventInfo.Name}();");
            codeLines.Add($"{privateEvent} -= value;");
            codeLines.Add("}");

            codeLines.Add("}");

            codeLines.Add($"private void Add_{eventInfo.Name}()");
            codeLines.Add("{");
            codeLines.Add($"if ({privateEvent} != null) return;");
            if (OutputDebug)
            {
                var msg = $"proxy: add_event {eventInfo.Name} id={methodID}";
                codeLines.Add($"System.Console.WriteLine(\"{msg}\");");
            }
            codeLines.Add("using var stream = new MemoryStream();");
            codeLines.Add($"_proxyItem.SendCallRequest({methodID}, stream.ToArray());");
            codeLines.Add("}");

            codeLines.Add($"private void Remove_{eventInfo.Name}()");
            codeLines.Add("{");
            codeLines.Add($"if ({privateEvent} == null) return;");
            if (OutputDebug)
            {
                var msg = $"proxy: remove_event {eventInfo.Name} id={methodID}";
                codeLines.Add($"System.Console.WriteLine(\"{msg}\");");
            }
            codeLines.Add("try {");
            codeLines.Add("using var stream = new MemoryStream();");
            codeLines.Add($"_proxyItem.SendCallRequest({methodID + 1}, stream.ToArray());");
            codeLines.Add("} catch {}"); //all exceptions need be caught here
            codeLines.Add("}");

            codeLines.Add($"private void Fire_{eventInfo.Name}(Stream dataStream)");
            codeLines.Add("{");
            if (OutputDebug)
            {
                var msg = $"proxy: on_event {eventInfo.Name} id={methodID}";
                codeLines.Add($"System.Console.WriteLine(\"{msg}\");");
            }
            codeLines.Add("var serializer = new Serializer(dataStream);");
            var argType = eventInfo.EventHandlerType.GenericTypeArguments[0];

            codeLines.Add($"var args = {SerializeStatement.DeserializeObject(argType)}");
            codeLines.Add($"var handler = {privateEvent};");
            codeLines.Add($"if (handler != null) handler(this, args);");
            codeLines.Add("}");

            return(codeLines);
        }
Beispiel #2
0
        private IEnumerable <string> GenerateMethod(short methodID, System.Reflection.MethodInfo method)
        {
            if (method.IsIndexer())
            {
                throw new NotSupportedException("Indexer unsupported, use setter/getter instead");
            }

            var codeLines = new List <string>();

            if (method.IsSynchronized())
            {
                codeLines.Add($"public {method.ReturnName()} {method.Name}(");
            }
            else
            {
                codeLines.Add($"public async {method.ReturnName()} {method.Name}(");
            }

            var isFirstParam = true;

            foreach (var p in method.GetParameters())
            {
                var paramDeclare = $"{p.ParameterType.CompilableClassName()} {p.Name}";
                if (!isFirstParam)
                {
                    codeLines.Add($", {paramDeclare}");
                }
                else
                {
                    codeLines.Add(paramDeclare);
                }
                isFirstParam = false;
            }
            codeLines.Add("){");
            if (OutputDebug)
            {
                var msg = $"proxy: call {method.Name} id={methodID}";
                codeLines.Add($"System.Console.WriteLine(\"{msg}\");");
            }
            codeLines.Add("var stream = new MemoryStream();");
            codeLines.Add("var serializer = new Serializer(stream);");
            foreach (var p in method.GetParameters())
            {
                if (p.ParameterType.IsInterface)
                {
                    codeLines.Add($"if ({p.Name} == null)");
                    codeLines.Add("{");
                    codeLines.Add("serializer.Serialize<Int16>(-1);");
                    codeLines.Add("} else {");
                    codeLines.Add($"var objectID = _proxyItem.CacheService({p.Name});");
                    codeLines.Add("serializer.Serialize<Int16>(objectID);");
                    codeLines.Add("}");
                }
                else
                {
                    codeLines.Add(SerializeStatement.SerializeObject(p.ParameterType, p.Name));
                }
            }

            codeLines.Add("stream.Seek(0, SeekOrigin.Begin);");
            if (method.IsSynchronized())
            {
                codeLines.Add($"var response = _proxyItem.SendCallRequest({methodID}, stream.ToArray());");
            }
            else
            {
                codeLines.Add($"var response = await _proxyItem.SendCallRequestAsync({methodID}, stream.ToArray());");
            }

            if (!method.ReturnType.IsVoid() && !method.ReturnType.IsTask())
            {
                codeLines.Add("serializer = new Serializer(response);");

                if (method.ReturnType.IsGenericTask())
                {
                    var returnType = method.ReturnType.GetGenericArguments()[0];
                    codeLines.Add($"return {SerializeStatement.DeserializeObject(returnType)}");
                }
                else if (method.ReturnType.IsInterface)
                {
                    codeLines.Add("var returnObjectID = serializer.Deserialize<Int16>();");
                    var nonNull = $"_proxyItem.CacheProxy<{method.ReturnType.CompilableClassName()}>(returnObjectID)";
                    codeLines.Add($"return returnObjectID >= 0 ? {nonNull} : null;");
                }
                else
                {
                    var returnType = method.ReturnType;
                    codeLines.Add($"return {SerializeStatement.DeserializeObject(returnType)}");
                }
            }

            codeLines.Add("}");
            return(codeLines);
        }
Beispiel #3
0
        private IEnumerable <string> GenerateMethodCall(MethodInfo method, Int16 methodID)
        {
            if (method.IsIndexer())
            {
                throw new NotSupportedException("Indexer unsupported, use setter/getter instead");
            }
            var codeLines = new List <string>();

            //the method signature
            if (method.IsSynchronized())
            {
                codeLines.Add($"private Task {GeneratedMethodCallName(method, methodID)}(Stream dataStream, MemoryStream resultStream)");
            }
            else
            {
                codeLines.Add($"private async Task {GeneratedMethodCallName(method, methodID)}(Stream dataStream, MemoryStream resultStream)");
            }
            codeLines.Add("{");
            if (OutputDebug)
            {
                var msg = $"stub: call {method.Name} id={methodID}";
                codeLines.Add($"System.Console.WriteLine(\"{msg}\");");
            }

            //deserialize the parameters
            codeLines.Add("var serializer = new Serializer(dataStream);");
            foreach (var p in method.GetParameters())
            {
                if (p.ParameterType.IsInterface)
                {
                    var id_name = $"{p.Name}_objectID";
                    codeLines.Add($"var {id_name} = serializer.Deserialize<Int16>();");
                    var nonNull = $"_serviceItem.CacheProxy<{p.ParameterType.CompilableClassName()}>({id_name})";
                    codeLines.Add($"{p.ParameterType.CompilableClassName()} {p.Name} = {id_name} < 0 ? null : {nonNull} ;");
                }
                else
                {
                    codeLines.Add($"var {p.Name} = {SerializeStatement.DeserializeObject(p.ParameterType)}");
                }
            }

            //call the service object
            if (method.ReturnType.IsVoid())
            {
                codeLines.Add($"_service.{method.Name}(");
            }
            else if (method.ReturnType.IsTask())
            {
                codeLines.Add($"await _service.{method.Name}(");
            }
            else if (method.ReturnType.IsGenericTask())
            {
                codeLines.Add($"var result = await _service.{method.Name}(");
            }
            else
            {
                codeLines.Add($"var result = _service.{method.Name}(");
            }

            //pass the parameters
            var isFirstParam = true;

            foreach (var p in method.GetParameters())
            {
                if (isFirstParam)
                {
                    isFirstParam = false;
                }
                else
                {
                    codeLines.Add(", ");
                }
                codeLines.Add($"{p.Name}:{p.Name}");
            }
            codeLines.Add(");");

            //serialize the returns to result stream
            if (!method.ReturnType.IsVoid() && !method.ReturnType.IsTask())
            {
                codeLines.Add("serializer = new Serializer(resultStream);");
                var resultType = method.ReturnType;
                if (method.ReturnType.IsInterface)
                {
                    codeLines.Add("if (result == null) {");
                    codeLines.Add("serializer.Serialize<Int16>(-1);");
                    codeLines.Add("} else {");
                    codeLines.Add($"var resultObjectID = _serviceItem.CacheService<{method.ReturnType.CompilableClassName()}>(result);");
                    codeLines.Add("serializer.Serialize<Int16>(resultObjectID);");
                    codeLines.Add("}");
                }
                else
                {
                    if (method.ReturnType.IsGenericTask())
                    {
                        resultType = resultType.GetGenericArguments()[0];
                    }
                    codeLines.Add(SerializeStatement.SerializeObject(resultType, "result"));
                }
            }
            if (method.IsSynchronized())
            {
                codeLines.Add("return Task.CompletedTask;");
            }
            codeLines.Add("}");
            return(codeLines);
        }