Ejemplo n.º 1
0
 private bool pushBasicType(object obj, DataStorage ds)
 {
     try
     {
         ds.Push(obj);
     }
     catch (Exception e)
     {
         NetworkController.ShowException(e);
         return(false);
     }
     return(true);
 }
Ejemplo n.º 2
0
        public static Dictionary <string, IMarshalable> GetMethodParamsObjects(MethodInfo methodInfo)
        {
            Dictionary <string, IMarshalable> result = new Dictionary <string, IMarshalable>();

            ParameterInfo[] args = methodInfo.GetParameters();
            foreach (ParameterInfo par in args)
            {
                if (NetView.IsValidBasicType(par.ParameterType))
                {
                    continue;
                }

                var constructor = par.ParameterType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                    null, Type.EmptyTypes, null);
                if (constructor == null)
                {
                    NetworkController.ShowException(new Exception("method's parameter " + par.ParameterType.FullName + " should have a parameterless constructor"));
                    return(null);
                }
                if (par.ParameterType.FullName == "System.Object[]")
                {
                    NetworkController.ShowException(new Exception("invalid parameter type " + par.ParameterType.FullName));
                    return(null);
                }


                var    obj     = Activator.CreateInstance(par.ParameterType);
                string objName = obj.ToString();



                if (!(obj is IMarshalable))
                {
                    NetworkController.ShowException(new Exception("argument " + objName + " not implement IMarshalable"));
                    return(null);
                }

                if (result.ContainsKey(objName))
                {
                    continue;
                }

                result.Add(objName, obj as IMarshalable);
            }
            return(result);
        }
Ejemplo n.º 3
0
        public static object CheckNonBasicType(Type type)
        {
            if (NetView.IsValidBasicType(type))
            {
                return(null);
            }

            var constructor = type.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null, Type.EmptyTypes, null);

            if (constructor == null)
            {
                if (FormatterServices.GetUninitializedObject(type) == null)
                {
                    NetworkController.ShowException(new Exception("method's parameter " + type.FullName + " should have a parameterless constructor"));
                    return(null);
                }
            }
            if (type.FullName == "System.Object[]")
            {
                NetworkController.ShowException(new Exception("invalid parameter type " + type.FullName));
                return(null);
            }


            var    obj     = Activator.CreateInstance(type);
            string objName = obj.ToString();



            if (!(obj is IMarshalable))
            {
                NetworkController.ShowException(new Exception("argument " + objName + " not implement IMarshalable"));
                return(null);
            }

            if (type.GetMethod("GetHashCode").DeclaringType != type)
            {
                NetworkController.ShowException(new Exception("argument " + objName + " not override GetHashCode"));
                return(null);
            }

            return(obj);
        }
Ejemplo n.º 4
0
        private object[] parseRequest(DataStorage ds)
        {
            Dictionary <int, object> result = new Dictionary <int, object>();
            int i = 0;

            while (!ds.Empty)
            {
                string key       = ds.ReadString();
                object resObject = parseObject(key, ds);
                if (resObject == null)
                {
                    NetworkController.ShowException(new Exception("invalid rpc parameter"));
                }

                result.Add(i, resObject);
                i++;
            }
            object[] resultSlice = new object[i];
            foreach (KeyValuePair <int, object> res in result)
            {
                resultSlice[res.Key] = res.Value;
            }
            return(resultSlice);
        }
Ejemplo n.º 5
0
        public void InitInvoke(params Object[] classes)
        {
            var invokeSystemType = new InvokeAttribute().GetType();

            foreach (var c in classes)
            {
                if (!c.GetType().IsClass)
                {
                    continue;
                }

                int    num;
                string key = c.ToString();
                if (countOfClasses.TryGetValue(key, out num))
                {
                    num = num + 1;
                    countOfClasses.Remove(key);
                }
                else
                {
                    num = 1;
                }
                countOfClasses.Add(key, num);
                hashToNum.Add(c.GetHashCode(), num);
                stringToObject.Add(getUniqueClassString(c), c);

                IEnumerable <MethodInfo> miInfos = ReflectionHelper.GetMethodsWithAttribute(c.GetType(), invokeSystemType);
                foreach (var member in miInfos)
                {
                    InvokeAttribute attr       = member.GetCustomAttribute <InvokeAttribute>();
                    string          methodName = getUniqueClassString(c) + "." + member.Name;

                    NetworkController.ShowMessage("Register method " + member + " as " + methodName + " with invokeType " + attr.Type);
                    Console.WriteLine("Register method " + member + " as " + methodName + " with invokeType " + attr.Type);

                    methods.Add(methodName, new InvokeHelper(c, member, attr.Type));
                    netCon.RegisterInvoke(methodName, this);
                    Dictionary <string, IMarshalable> res = ReflectionHelper.GetMethodParamsObjects(member);

                    foreach (KeyValuePair <string, IMarshalable> param in res)
                    {
                        if (methodsArguments.ContainsKey(param.Key))
                        {
                            continue;
                        }
                        methodsArguments.Add(param.Key, param.Value);
                        NetworkController.ShowMessage($"Register non-basic type {param.Value.GetType()}");
                        Console.WriteLine($"Register non-basic type {param.Value.GetType()}");
                    }
                }

                IEnumerable <FieldInfo>         fieldInfos  = ReflectionHelper.GetFieldsWithAttribute(c.GetType(), new SyncAttribute().GetType());
                Dictionary <string, InfoHelper> propertyMap = new Dictionary <string, InfoHelper>();
                Console.WriteLine(fieldInfos);
                foreach (var fieldInfo in fieldInfos)
                {
                    Console.WriteLine("Register field " + fieldInfo.Name);
                    NetworkController.ShowMessage("Register field " + fieldInfo.Name);
                    string propName = fieldInfo.Name;
                    propertyMap.Add(propName, new InfoHelper(fieldInfo));
                    var nonBasicObj = ReflectionHelper.CheckNonBasicType(fieldInfo.FieldType);
                    if (nonBasicObj != null)
                    {
                        if (!methodsArguments.ContainsKey(nonBasicObj.ToString()))
                        {
                            methodsArguments.Add(nonBasicObj.ToString(), nonBasicObj as IMarshalable);
                        }
                    }
                }
                IEnumerable <PropertyInfo> propInfos = ReflectionHelper.GetPropertiesWithAttribute(c.GetType(), new SyncAttribute().GetType());
                Console.WriteLine(propInfos);
                foreach (var propInfo in propInfos)
                {
                    Console.WriteLine("Register property " + propInfo.Name);
                    NetworkController.ShowMessage("Register property " + propInfo.Name);
                    string propName = propInfo.Name;
                    propertyMap.Add(propName, new InfoHelper(propInfo));
                    var nonBasicObj = ReflectionHelper.CheckNonBasicType(propInfo.PropertyType);
                    if (nonBasicObj != null)
                    {
                        if (!methodsArguments.ContainsKey(nonBasicObj.ToString()))
                        {
                            methodsArguments.Add(nonBasicObj.ToString(), nonBasicObj as IMarshalable);
                        }
                    }
                }
                properties.Add(getUniqueClassString(c), propertyMap);
                netCon.RegisterInvoke(getSyncMethod(c), this);
            }
            Dispatcher.StartSync(this);
        }