Beispiel #1
0
        public void RegClass(System.Type klass, string macro, ECSType csType)
        {
            if (klass.IsGenericType)
            {
                return;
            }
            var methods = klass.GetMethods();

            foreach (var i in methods)
            {
                var rpcAttr = Rtti.AttributeHelper.GetCustomAttribute(i, typeof(RPCCallAttribute).FullName, true);
                if (rpcAttr == null)
                {
                    continue;
                }

                var args = i.GetParameters();

                var proc_type = GetRPCMethodProcessor(args);
                if (proc_type == null)
                {
                    Profiler.Log.WriteLine(Profiler.ELogTag.Warning, "RPC", $"Method: {i.DeclaringType.FullName}.{i.Name} arguments is not valid");
                    continue;
                }

                var type = Rtti.RttiHelper.GetTypeFromTypeFullName(proc_type.FullName, csType);
                //var atts = type.GetCustomAttributes(typeof(RPCProcessorAttribute), false);
                //if (atts == null || atts.Length == 0)
                //    continue;
                //var senderDesc = atts[0] as RPCProcessorAttribute;
                //var sender = RPCProcessor.InitProcessor(senderDesc.ProcessorType, type, senderDesc.ReturnType);
                var sender = RPCProcessor.GetProcessorByArgument(type);
                RegRPC(sender, i, type, macro);
            }
        }
Beispiel #2
0
        public void BuildAssemblyRPC(System.Reflection.Assembly[] assems, string[] assemMacro, EngineNS.ECSType csType)
        {
            if (assems == null)
            {
                assems = AppDomain.CurrentDomain.GetAssemblies();
            }
            List <RPCTypeInfo> AllClass = new List <RPCTypeInfo>();

            for (var i = 0; i < assems.Length; i++)
            {
                try
                {
                    var assm = assems[i];

                    if (assemMacro != null && assemMacro.Length == assems.Length && string.IsNullOrEmpty(assemMacro[i]))
                    {
                        assemMacro[i] = GetAssmemblyRPCMacro(assm);
                    }
                    foreach (var j in assm.GetTypes())
                    {
                        if (j.IsSubclassOf(typeof(RPCProcessor)))
                        {
                            if (j.IsGenericType)
                            {
                                continue;
                            }
                            if (RPCProcessor.GetProcessor(j) == null)
                            {
                                RPCProcessor.InitProcessor(j);
                            }
                        }

                        if (IsRPCClass(j))
                        {
                            var tmp = new RPCTypeInfo();
                            tmp.Type = j;
                            if (assemMacro != null && assemMacro.Length == assems.Length)
                            {
                                tmp.Macro = assemMacro[i];
                            }
                            AllClass.Add(tmp);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    EngineNS.Profiler.Log.WriteException(ex);
                }
            }
            //只有不会再被派生的类才能做RPC分析,否则就会出现一个RPCParameter被多个函数使用的情况
            var finalKlass = GetFinalKlass(AllClass);

            foreach (var i in finalKlass)
            {
                RegClass(i.Type, i.Macro, csType);
            }

            RefreshRPCMap(null);
        }
Beispiel #3
0
        public void RegRPC(RPCProcessor arg, System.Reflection.MethodInfo method, System.Type argType, string macro)
        {
            if (arg.Method != null)
            {
                System.Diagnostics.Debug.Assert(arg.Method == method);
                //同一个RPCParameter被多个RPCMethod作为参数使用,这是不符合系统需求的
                System.Diagnostics.Debug.Assert(false);
                return;
            }

            var index = arg.RPCIndex;

            if (index == MaxRPC)
            {
                index = FindValidIndex();
            }

            arg.SetMethod(method, index, argType);

            var rpcAttr = Rtti.AttributeHelper.GetCustomAttribute(method, typeof(RPCCallAttribute).FullName, true);

            if (rpcAttr != null)
            {
                var cattr = rpcAttr as RPCCallAttribute;
                if (cattr != null)
                {
                    arg.CallAttr = cattr;
                }
            }
            else
            {
                arg.CallAttr = null;
            }

            DescTable[arg.MethodHash] = arg;
            DescArray[index]          = arg;
        }
Beispiel #4
0
        public void RefreshRPCMap(List <System.Reflection.MethodInfo> funcs)
        {
            var rn       = RName.GetRName("RPCMapping.xml");
            var mappings = IO.XmlHolder.CreateObjectFromXML(rn) as RPCProcessorMapping;

            if (mappings == null)
            {
#if PWindow
                mappings = new RPCProcessorMapping();
                for (UInt16 i = 0; i < DescArray.Length; i++)
                {
                    if (DescArray[i] == null)
                    {
                        continue;
                    }

                    var desc = new RPCProcessorMapping.Desc();
                    desc.ProcessorName = Rtti.RttiHelper.GetTypeSaveString(DescArray[i].GetType());
                    desc.Index         = i;
                    mappings.Descs.Add(desc);
                }
                IO.XmlHolder.SaveObjectToXML(mappings, rn);
#endif
            }
            else
            {
                bool           isdirty        = false;
                RPCProcessor[] SavedDescArray = DescArray;
                DescArray = new RPCProcessor[MaxRPC];
                for (UInt16 i = 0; i < (UInt16)mappings.Descs.Count; i++)
                {
                    var type = Rtti.RttiHelper.GetTypeFromSaveString(mappings.Descs[i].ProcessorName);
                    if (type == null)
                    {
                        isdirty = true;
                        continue;
                    }
                    var proc = RPCProcessor.GetProcessor(type);
                    if (proc == null)
                    {
                        isdirty = true;
                        continue;
                    }
                    DescArray[mappings.Descs[i].Index] = proc;
                    proc.RPCIndex = mappings.Descs[i].Index;
                }
                for (UInt16 i = 0; i < SavedDescArray.Length; i++)
                {
                    if (SavedDescArray[i] == null)
                    {
                        continue;
                    }

                    bool finded = false;
                    for (UInt16 j = 0; j < DescArray.Length; j++)
                    {
                        if (DescArray[j] == SavedDescArray[i])
                        {
                            finded = true;
                            break;
                        }
                    }

                    if (finded == false)
                    {
                        var idx = this.FindValidIndex();
                        DescArray[idx]          = SavedDescArray[i];
                        DescArray[idx].RPCIndex = idx;
                        isdirty = true;
                    }
                }

                if (isdirty)
                {
#if PWindow
                    Profiler.Log.WriteLine(Profiler.ELogTag.Info, "RPC", $"RPC mapping changed!");
                    mappings = new RPCProcessorMapping();
                    for (UInt16 i = 0; i < DescArray.Length; i++)
                    {
                        if (DescArray[i] == null)
                        {
                            continue;
                        }

                        var desc = new RPCProcessorMapping.Desc();
                        desc.ProcessorName = Rtti.RttiHelper.GetTypeSaveString(DescArray[i].GetType());
                        desc.Index         = i;
                        mappings.Descs.Add(desc);
                    }
                    IO.XmlHolder.SaveObjectToXML(mappings, rn);
#endif
                }
            }
        }
Beispiel #5
0
        public object Execute(ref NetCore.PkgReader pkg, NetCore.NetConnection connect, ref NetCore.RPCRouter.RouteData routeInfo)
        {
            UInt16 rpcIndex    = 0;
            UInt32 rpcHash     = 0;
            bool   hasReturn   = false;
            bool   isHashIndex = pkg.IsHashIndex();

            if (isHashIndex)
            {
                hasReturn = pkg.IsHasReturn();
                pkg.Read(out rpcHash);
            }
            else
            {
                pkg.Read(out rpcIndex);
                hasReturn = (rpcIndex & WaitFlag) != 0 ? true : false;
            }
            if (hasReturn != pkg.IsHasReturn())
            {
                System.Diagnostics.Debug.Assert(false);
            }
            UInt16 serialId = 0;

            if (hasReturn)
            {
                pkg.Read(out serialId);
                rpcIndex = (UInt16)(rpcIndex & MaxRPC);
            }
            RPCProcessor exec = null;

            if (isHashIndex)
            {
                exec = GetExecByHash(rpcHash);
            }
            else
            {
                exec = GetExecByIndex(rpcIndex);
            }
            if (exec == null)
            {
                System.Diagnostics.Debug.WriteLine($"RPC GetExecByIndex is null:{rpcIndex}");
                return(null);
            }
            object host = exec.GetHostObject(ref routeInfo);

            if (host == null)
            {
                System.Diagnostics.Debug.WriteLine($"RPC HostObject is null:{exec.GetType().FullName}");
                return(null);
            }
            var authority = exec.GetAuthority(host);

            if (authority < exec.CallAttr.LimitLevel)
            {//超越权限
                //System.Diagnostics.Debug.WriteLine($"Over Authority[{authority}<{exec.CallAttr.LimitLevel.ToString()}]:{parameter.GetHostObjectName()}=>{parameter.GetRPCMethod().Name}");
                return(null);
            }

            try
            {
                //这里如果反射调用就有GC,生成代码可以去掉GC
                return(exec.Execute(host, ref pkg, serialId, connect, ref routeInfo));
            }
            catch (Exception ex)
            {
                Profiler.Log.WriteException(ex);
                return(null);
            }
            finally
            {
            }
        }