Exemple #1
0
        private void ExecCall(CmdMethodInfo cmdMethodInfo, TcpSocketAsync client, MsgData msg)
        {
            var session = client.UserState as Session;

            this.OnCmdExecutingEvent(session, msg);
            if (cmdMethodInfo != null && client != null)
            {
                ActionResult result = null;

                using (var controller = Activator.CreateInstance(cmdMethodInfo.Type) as Controller)
                {
                    try
                    {
                        controller.Init(session, msg);
                        if (this.OnAuth(cmdMethodInfo, session, msg, out result))
                        {
                            controller.OnExecuting();
                            result = cmdMethodInfo.Method.Invoke(controller, null) as ActionResult;
                            controller.OnResult(result);
                            this.OnCmdExecutedEvent(session, msg, result.Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        bool ishand = false;
                        try
                        {
                            ExceptionContext excontext = new ExceptionContext()
                            {
                                Msg = msg, IsHandle = false, Exception = ex
                            };
                            controller.OnException(excontext);
                            ishand = excontext.IsHandle;
                            result = excontext.Result;
                        }
                        catch (Exception _ex)
                        {
                            this.OnMvcHostServerErrorEvent(_ex);
                        }
                        if (!ishand)
                        {
                            this.OnMvcHostServerErrorEvent(ex);
                        }
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.ServerError, "服务器发生错误!");
                        }
                    }
                }


                if (result != null && client.IsConnected)
                {
                    result.Result.Cmd = msg.Cmd;
                    result.Result.Id  = msg.Id;
                    this.SendMsg(result.Result, session);
                }
            }
        }
Exemple #2
0
        private void LoadController(XmlElement nodeElement)
        {
            Type baseControllerType = typeof(Controller);
            Type actionResultType   = typeof(ActionResult);

            foreach (XmlElement classElement in nodeElement)
            {
                if (classElement.Name == "Action")
                {
                    var s = classElement.GetAttribute("cmd");
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }
                    int cmd = 0;
                    if (!int.TryParse(s, out cmd))
                    {
                        continue;
                    }
                    var method = classElement.GetAttribute("method");
                    if (string.IsNullOrEmpty(method))
                    {
                        continue;
                    }
                    s = classElement.GetAttribute("type");
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }
                    var      arr      = s.Split(',');
                    var      t        = arr[0].Trim();
                    Assembly assembly = null;
                    if (arr.Length > 1 && !string.IsNullOrEmpty(arr[1].Trim()))
                    {
                        assembly = this.GetAssembly(arr[1].Trim());
                    }
                    else
                    {
                        assembly = this.m_defaultAssembly;
                    }

                    if (assembly == null)
                    {
                        continue;
                    }

                    var type = assembly.GetType(t, false);
                    if (type != null && type.IsClass && !type.IsAbstract && type.IsSubclassOf(baseControllerType))
                    {
                        var methodInfo = type.GetMethod(method, BindingFlags.Instance | BindingFlags.Public);
                        if (methodInfo != null && methodInfo.ReturnType == actionResultType && methodInfo.GetParameters().Length == 0)
                        {
                            CmdMethodInfo m = new CmdMethodInfo()
                            {
                                Type   = type,
                                Method = methodInfo,
                                Cmd    = cmd,
                                NoAuth = false
                            };
                            var attrs = type.GetCustomAttributes(typeof(AllowAnonymousAttribute), false);
                            if (attrs == null || attrs.Length == 0)
                            {
                                attrs = methodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), false);
                            }
                            m.NoAuth = attrs != null && attrs.Length > 0;
                            this.cmdCallbackDic.Add(cmd, m);

                            if (m.NoAuth)
                            {
                                m.AuthTypeList = new List <Type>(0);
                            }
                            else
                            {
                                var tlist = new List <Type>();
                                foreach (XmlElement authElement in classElement)
                                {
                                    if (authElement.Name == "Authorize")
                                    {
                                        var authtype = GetAuth(authElement);
                                        if (authtype != null && authtype.IsClass && !authtype.IsAbstract)
                                        {
                                            tlist.Add(authtype);
                                        }
                                    }
                                }
                                m.AuthTypeList = new List <Type>(tlist.Count);
                                m.AuthTypeList.AddRange(tlist);
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private bool OnAuth(CmdMethodInfo cmdMethodInfo, Session session, MsgData msg, out ActionResult result)
        {
            result = null;
            if (cmdMethodInfo.NoAuth)
            {
                return(true);
            }
            using (AuthorizationContext authContext = new AuthorizationContext()
            {
                Session = session,
                Cmd = msg.Cmd,
                IsAuth = false,
                Result = null
            })
            {
                foreach (var t in this.GlobalAuthTypeList)
                {
                    IAuthorize auth = (IAuthorize)Activator.CreateInstance(t);
                    auth.OnAuthorization(authContext);
                    if (!authContext.IsAuth)
                    {
                        result = authContext.Result;
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.NeedAuth, "无权限请求!");
                        }

                        return(false);
                    }
                }

                var tarr = cmdMethodInfo.Type.GetCustomAttributes(typeof(AuthorizeAttribute), true);
                foreach (var attr in tarr)
                {
                    var auth = attr as AuthorizeAttribute;
                    auth.OnAuthorization(authContext);
                    if (!authContext.IsAuth)
                    {
                        result = authContext.Result;
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.NeedAuth, "无权限请求!");
                        }

                        return(false);
                    }
                }


                if (cmdMethodInfo.AuthTypeList != null && cmdMethodInfo.AuthTypeList.Count > 0)
                {
                    foreach (var t in cmdMethodInfo.AuthTypeList)
                    {
                        IAuthorize auth = (IAuthorize)Activator.CreateInstance(t);
                        auth.OnAuthorization(authContext);
                        if (!authContext.IsAuth)
                        {
                            result = authContext.Result;
                            if (result == null)
                            {
                                result = new ActionResult();
                                result.SetMsg(MsgStatus.NeedAuth, "无权限请求!");
                            }

                            return(false);
                        }
                    }
                }

                tarr = cmdMethodInfo.Method.GetCustomAttributes(typeof(AuthorizeAttribute), true);
                foreach (var attr in tarr)
                {
                    var auth = attr as AuthorizeAttribute;
                    auth.OnAuthorization(authContext);
                    if (!authContext.IsAuth)
                    {
                        result = authContext.Result;
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.NeedAuth, "无权限请求!");
                        }

                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #4
0
        private void LoadController(XmlElement nodeElement)
        {
            Type baseControllerType = typeof(Controller);

            foreach (XmlElement classElement in nodeElement)
            {
                if (classElement.Name == "Action")
                {
                    var s = classElement.GetAttribute("cmd");
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }
                    int cmd = 0;
                    if (!int.TryParse(s, out cmd))
                    {
                        continue;
                    }
                    var method = classElement.GetAttribute("method");
                    if (string.IsNullOrEmpty(method))
                    {
                        continue;
                    }
                    s = classElement.GetAttribute("type");
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }
                    var      arr      = s.Split(',');
                    var      t        = arr[0].Trim();
                    Assembly assembly = null;
                    if (arr.Length > 1 && !string.IsNullOrEmpty(arr[1].Trim()))
                    {
                        assembly = this.GetAssembly(arr[1].Trim());
                    }
                    else
                    {
                        assembly = this.m_defaultAssembly;
                    }

                    if (assembly == null)
                    {
                        continue;
                    }

                    var type = assembly.GetType(t, false);
                    if (type == null)
                    {
                        throw new TypeLoadException(t + " not found!");
                    }
                    if (!type.IsClass)
                    {
                        throw new TypeLoadException(t + " is not class!");
                    }
                    if (!type.IsSubclassOf(baseControllerType))
                    {
                        throw new TypeLoadException(t + " is not Controller!");
                    }
                    if (type.IsAbstract)
                    {
                        throw new TypeLoadException(t + " is abstract!");
                    }
                    var methodInfo = type.GetMethod(method, BindingFlags.Instance | BindingFlags.Public);
                    if (methodInfo == null)
                    {
                        throw new MethodAccessException(t + ", Method: " + method + " not found!");
                    }
                    var parameterinfos = methodInfo.GetParameters();
                    if (!(parameterinfos.Length == 0 || (parameterinfos.Length == 1 && !parameterinfos[0].IsOut)))
                    {
                        throw new MethodAccessException(t + ", Method: " + method + " parameter is error!");
                    }
                    CmdMethodInfo m = new CmdMethodInfo()
                    {
                        Type          = type,
                        Method        = methodInfo,
                        ParameterType = parameterinfos.Length > 0 ? parameterinfos[0].ParameterType : null,
                        Cmd           = cmd,
                        NoAuth        = false
                    };
                    var attrs = type.GetCustomAttributes(typeof(AllowAnonymousAttribute), false);
                    if (attrs == null || attrs.Length == 0)
                    {
                        attrs = methodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), false);
                    }
                    m.NoAuth = attrs != null && attrs.Length > 0;
                    this.cmdCallbackDic[cmd] = m;

                    if (m.NoAuth)
                    {
                        m.AuthTypeList = new List <Type>(0);
                    }
                    else
                    {
                        var tlist = new List <Type>();
                        foreach (XmlElement authElement in classElement)
                        {
                            if (authElement.Name == "Authorize")
                            {
                                var authtype = GetAuth(authElement);
                                if (authtype != null && authtype.IsClass && !authtype.IsAbstract)
                                {
                                    tlist.Add(authtype);
                                }
                            }
                        }
                        m.AuthTypeList = new List <Type>(tlist.Count);
                        m.AuthTypeList.AddRange(tlist);
                    }
                }
            }
        }
Exemple #5
0
        private void ExecCall(CmdMethodInfo cmdMethodInfo, ITcpClientAsync client, MsgData msg)
        {
            var session = client.UserState as Session;

            this.OnCmdExecutingEvent(session, msg);
            if (cmdMethodInfo != null && client != null)
            {
                ActionResult result = null;

                using (var controller = Activator.CreateInstance(cmdMethodInfo.Type) as Controller)
                {
                    try
                    {
                        controller.Init(session, msg);
                        if (this.OnAuth(cmdMethodInfo, session, msg, out result))
                        {
                            controller.OnExecuting();
                            object[] parameters = null;
                            if (cmdMethodInfo.ParameterType != null)
                            {
                                var o = msg.GetData(cmdMethodInfo.ParameterType);
                                parameters = new object[] { o };
                            }
                            var obj = cmdMethodInfo.Method.Invoke(controller, parameters);
                            if (obj != null && obj is ActionResult)
                            {
                                result = obj as ActionResult;
                            }
                            else if (obj != null && obj is MsgData)
                            {
                                result = new ActionResult(obj as MsgData);
                            }
                            else
                            {
                                result = new ActionResult();
                                result.SetMsg(MsgStatus.Succeed, null);
                                if (obj != null)
                                {
#if NET20
                                    result.Result.SetData(obj);
#else
                                    var t = cmdMethodInfo.Method.ReturnType;
                                    if (t == typeof(System.Threading.Tasks.Task) ||
                                        (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>)))
                                    {
                                        var task = obj as System.Threading.Tasks.Task;
                                        if (task.Status == System.Threading.Tasks.TaskStatus.Created)
                                        {
                                            task.Start();
                                        }
                                        if (!task.IsCompleted || !task.IsCanceled)
                                        {
                                            task.Wait();
                                        }
                                        if (task.IsFaulted && task.Exception != null)
                                        {
                                            throw task.Exception;
                                        }
                                        if (t.IsGenericType)
                                        {
                                            var o = t.GetProperty("Result").GetValue(obj, null);
                                            result.Result.SetData(o);
                                        }
                                    }
                                    else
                                    {
                                        result.Result.SetData(obj);
                                    }
#endif
                                }
                            }
                            result.Result.Cmd = msg.Cmd;
                            result.Result.Id  = msg.Id;
                            controller.OnResult(result);
                            this.OnCmdExecutedEvent(session, msg, result.Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        bool ishand = false;
                        try
                        {
                            ExceptionContext excontext = new ExceptionContext()
                            {
                                Msg = msg, IsHandle = false, Exception = ex
                            };
                            controller.OnException(excontext);
                            ishand = excontext.IsHandle;
                            result = excontext.Result;
                        }
                        catch (Exception _ex)
                        {
                            this.OnMvcHostServerErrorEvent(_ex);
                        }
                        if (!ishand)
                        {
                            this.OnMvcHostServerErrorEvent(ex);
                        }
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.ServerError, "服务器发生错误!");
                        }
                    }
                }


                if (result != null && client.IsConnected)
                {
                    result.Result.Cmd = msg.Cmd;
                    result.Result.Id  = msg.Id;
                    this.SendMsg(result.Result, session);
                }
            }
        }