Example #1
0
 public virtual void OnDisconnected()
 {
     if (mUploadFileHandler != null)
     {
         try
         {
             mUploadFileHandler.OnUploadFileError();
         }
         catch
         {
         }
         mUploadFileHandler = null;
     }
     try
     {
         if (this.GroupName.IsNullOrEmpty() == false)
         {
             if (KeepAliveHandlers.Contains(this))
             {
                 KeepAliveHandlers.Remove(this);
                 RemotingController.MessageReceiverDisconnect(this.Session, this.GroupName);
             }
         }
     }
     catch
     {
     }
 }
Example #2
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, "");
            }
        }
Example #3
0
        public virtual void OnReceived(string data)
        {
            try
            {
                MessageBag msgBag = Newtonsoft.Json.JsonConvert.DeserializeObject <MessageBag>(data);
                mCurrentBag = msgBag;

                if (this.Session == null)
                {
                    if (msgBag.SessionID.IsNullOrEmpty())
                    {
                        this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
                    }
                    else
                    {
                        this.Session = SessionState.GetSession(msgBag.SessionID);
                        if (this.Session == null)
                        {
                            this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
                        }
                    }
                }

                if (msgBag.Action == "init")
                {
                    handleInit(msgBag);
                }
                else if (msgBag.Action == "exit")
                {
                    mCloseStreamHandler();
                }
                else if (msgBag.Action == "UploadFile")
                {
                    this.StreamType = RemotingStreamType.Bytes;
                    handleUploadFile(msgBag);
                }
                else if (msgBag.Action == "w_heart")//websocket心跳包
                {
                    SendClientMessage("1", 2);
                    return;
                }
                else if (msgBag.Action == "w_msg")//websocket消息
                {
                    RemotingController.MessageReceive(this.Session, msgBag.State);
                    return;
                }
                else if (msgBag.MethodName.IsNullOrEmpty() == false)
                {
                    handleMethodInvoke(msgBag);
                }
                else if (msgBag.GroupName.IsNullOrEmpty() == false)
                {
                    this.GroupName = msgBag.GroupName;
                    if (RemotingController.MessageReceiverConnect(this.Session, this.GroupName) == false)
                    {
                        throw new Exception("服务器拒绝你接收信息");
                    }
                    KeepAliveHandlers.Add(this);
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #4
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);
            }
        }