Beispiel #1
0
        void handleUploadFile(MessageBag msgBag)
        {
            mFileGettedSize = 0;
            try
            {
                string remoteName = msgBag.ClassFullName;
                var    pageDefine = checkRemotingName(remoteName);

                RemotingController currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                currentPage.Session                = this.Session;
                currentPage.RequestHeaders         = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler);
                RemotingContext.Current.Controller = currentPage;

                currentPage.onLoad();
                mFileGettedSize = msgBag.Offset;
                try
                {
                    mUploadFileHandler = currentPage.OnBeginUploadFile(msgBag.FileName, msgBag.State, msgBag.FileSize, msgBag.Offset);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    currentPage.unLoad();
                }

                SendData(MessageType.UploadFileBegined, "ok", "");
            }
            catch (Exception ex)
            {
                var baseException = ex.GetBaseException();
                SendData(MessageType.InvokeError, baseException != null ? baseException.Message : ex.Message, "");
            }
        }
Beispiel #2
0
        void handleMethodInvoke(MessageBag msgBag)
        {
            RemotingController currentPage = null;

            try
            {
                var pageDefine = checkRemotingName(msgBag.ClassFullName);

                currentPage                = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                currentPage.Session        = this.Session;
                currentPage.RequestHeaders = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler);

                RemotingContext.Current.Controller = currentPage;

                currentPage.onLoad();

                try
                {
                    MethodInfo[] methodinfos = pageDefine.Methods.Where(m => m.Name == msgBag.MethodName).ToArray();
                    if (methodinfos.Length == 0)
                    {
                        throw new Exception($"没有找到方法{msgBag.MethodName},可能因为此方法没有定义[RemotingMethod]");
                    }


                    for (int k = 0; k < methodinfos.Length; k++)
                    {
                        var methodinfo = methodinfos[k];
                        RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)methodinfo.GetCustomAttribute(typeof(RemotingMethodAttribute));
                        object[] jsParameters = null;
                        if (msgBag.ParameterJson.StartsWith("["))
                        {
                            jsParameters = msgBag.ParameterJson.FromJson <object[]>();
                        }
                        else
                        {
                            jsParameters = DecrptRSA(this.Session, msgBag.ParameterJson).FromJson <object[]>();
                        }

                        var pInfos = methodinfo.GetParameters();
                        if (pInfos.Length != jsParameters.Length)
                        {
                            if (k == methodinfos.Length - 1)
                            {
                                throw new Exception($"{msgBag.MethodName}参数数量与服务器不一致");
                            }
                            continue;
                        }

                        object[] parameters = new object[pInfos.Length];

                        for (int i = 0; i < parameters.Length; i++)
                        {
                            if (jsParameters[i] == null)
                            {
                                continue;
                            }

                            Type pType = pInfos[i].ParameterType;

                            if (jsParameters[i] is Newtonsoft.Json.Linq.JToken)
                            {
                                parameters[i] = (jsParameters[i] as Newtonsoft.Json.Linq.JToken).ToObject(pType);
                            }
                            else if (pType.GetTypeInfo().IsEnum || (pType.GetTypeInfo().IsGenericType&& pType.GetGenericTypeDefinition() == typeof(Nullable <>) && pType.GetGenericArguments()[0].GetTypeInfo().IsEnum))
                            {
                                Type enumType = pType;
                                if (pType.GetTypeInfo().IsGenericType)
                                {
                                    enumType = pType.GetGenericArguments()[0];
                                }
                                parameters[i] = Enum.Parse(enumType, jsParameters[i].ToSafeString());
                            }
                            else
                            {
                                parameters[i] = Convert.ChangeType(jsParameters[i], pType);
                            }
                        }
                        object result = null;

                        result = currentPage._OnInvokeMethod(methodinfo, parameters);

                        if (methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptResult) && result != null)
                        {
                            SendData(MessageType.Result, result, Session.SessionID, encryptToReturn);
                        }
                        else
                        {
                            SendData(MessageType.Result, result, Session.SessionID);
                        }
                        break;
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    currentPage.unLoad();
                }
            }
            catch (RSADecrptException)
            {
                CreateRSAKey(this.Session);
                SendData(MessageType.RSADecrptError, new { Exponent = this.Session["$$_rsa_PublicKeyExponent"], Modulus = this.Session["$$_rsa_PublicKeyModulus"] }, currentPage.Session.SessionID);
            }
            catch (Exception ex)
            {
                var baseException = ex.GetBaseException();
                SendData(MessageType.InvokeError, baseException != null ? baseException.Message : ex.Message, currentPage.Session.SessionID);
            }
        }