Exemplo n.º 1
0
 public QueuedOutMessage(ClientManager clmngr, ISocketMessage msg)
 {
     this.Client = clmngr;
     this.Command = msg;
     this.Length = 0;
     this.ShutdownClientAfterSend = false;
 }
Exemplo n.º 2
0
 public ClientWorkingPolicy(ResponsePolicy policy, ClientManager client, ISocketMessage msg, bool noDelay)
 {
     this.Policy = policy;
     this.Client = client;
     this.Message = msg;
     this.NoDelay = noDelay;
 }
Exemplo n.º 3
0
        public override void HandleRequest(ClientManager clmngr, HttpContext context)
        {
            if (clmngr == null || context == null || context.Request == null)
                return;

            HttpResponse response = context.Response;

            string result = "{\"Title\":\"Message\", \"Content\":\"Welcome!\"}";
            response.Write(result);
        }
Exemplo n.º 4
0
        public override void HandleRequest(ClientManager clmngr, HttpContext context)
        {
            if (clmngr == null || context == null || context.Request == null)
                return;

            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            int userId = 0;
            if (request.Parameters != null && request.Parameters.AllKeys.Contains("UserID"))
            {
                int.TryParse(request.Parameters["UserID"], out userId);
            }
            if (userId <= 0)
                throw new BussinessException("请输入用户ID。");
            string key = Guid.NewGuid().ToString();

            string result = "{\"UserID\":\"" + userId + "\", \"Key\":\"" + key + "\"}";
            response.Write(result);
        }
Exemplo n.º 5
0
        private void HandleErrorRequest(ClientManager clmngr, HttpRequest request)
        {
            if (clmngr == null || request == null)
                return;

            HttpContext context = new HttpContext(this, clmngr, request);
            HttpResponse response = context.Response;
            response.Write(request.Error.Message);
        }
Exemplo n.º 6
0
        private HttpContext GetContext(ClientManager clmngr)
        {
            if (clmngr != null)
                return GetContext(clmngr.nUID);

            return null;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="clmngr"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        protected override bool RegistUser(ClientManager clmngr, int uid)
        {
            if (clmngr == null || uid < 1 || !this._Clients.ContainsKey(clmngr.nSessionID))
                return false;

            clmngr.IsAuthedUser = true;
            clmngr.nUID = uid;

            if (_Users.ContainsKey(uid))
            {
                UserOnlineInfo onlineUser = _Users[uid] as UserOnlineInfo;
                if (!onlineUser.IsOnline)
                {
                    onlineUser.SetLogin();
                }
                onlineUser.RefreshActiveTime();
            }
            else
            {
                UserOnlineInfo onlineUser = new UserOnlineInfo(uid);
                onlineUser.SetLogin();
                lock (_Users.SyncRoot)
                {
                    _Users[uid] = onlineUser;
                }
            }
            return true;
        }
Exemplo n.º 8
0
        /// <summary>
        /// 卸载用户
        /// </summary>
        /// <param name="clmngr"></param>
        public void UnRegistUser(ClientManager clmngr)
        {
            if (clmngr == null || !clmngr.IsAuthedUser)
                return;

            int uid = clmngr.nUID;
            try
            {
                if (_Users.ContainsKey(uid))
                {
                    UserOnlineInfo user = _Users[uid] as UserOnlineInfo;
                    user.SocketOpen = false;
                }
            }
            catch { }
            finally
            {
                clmngr.IsAuthedUser = false;
            }
        }
Exemplo n.º 9
0
        public override void ShutdownClient(ClientManager clmngr)
        {
            if (clmngr == null)
                return;

            if (clmngr.IsAuthedUser)
                UnRegistUser(clmngr);

            base.ShutdownClient(clmngr);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 处理用户请求
        /// </summary>
        /// <param name="clmngr"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public override bool HandleRequest(ClientManager clmngr, ISocketMessage message)
        {
            bool goRecieving = false;
            HttpContext context = null;
            try
            {
                _RequestTimes++;
                HttpRequest request = HttpRequest.ParseFromRawMessage(this, message.ToString());

                if (!request.IsRequestError)
                {
                    //检查主机头是否符合设置
                    if (!CheckRequestHost(request))
                    {
                        //shutdown client
                        ShutdownClient(clmngr);
                        return goRecieving;
                    }
                    //获取该request的handlers
                    CometCommandHandlerPipeline handlePipeline = GetRegistedCommandHandlers(request);

                    if (handlePipeline == null)
                    {
                        //shutdown client
                        ShutdownClient(clmngr);
                        return goRecieving;
                    }

                    context = new HttpContext(this, clmngr, request);

                    CometCommand cometCmd = handlePipeline.Command;
                    request.Command = cometCmd;

                    if (!clmngr.IsAuthedUser)
                    {
                        RegistUser(clmngr, request);
                    }
                    //如果要求长连接
                    if (cometCmd.RequireKeepAlive)
                    {
                        //登记本次请求事务
                        if (RegistKeepAliveRequestContext(clmngr, request) == null)
                        {
                            //shutdown client
                            ShutdownClient(clmngr);
                            return goRecieving;
                        }
                    }
                    //执行处理管道
                    if (handlePipeline.Count > 0)
                    {
                        foreach (Handler.IHttpHandler handler in handlePipeline.Handlers)
                        {
                            if (handler != null)
                            {
                                handler.HandleRequest(clmngr, context);
                            }
                        }
                    }
                }
                else
                {
                    HandleErrorRequest(clmngr, request);
                }
            }
            catch (Exception ex)
            {
                if (ex is BussinessException)
                {
                    ExceptionResult result = new ExceptionResult()
                    {
                        ErrorCode = "B10001",
                        ErrorMessage = ex.Message
                    };
                    context.Response.Write(Common.Utility.Json.JsonHelper.ObjToJson(result));
                }
                else
                {
                    ShutdownClient(clmngr);
                    Console.WriteLine(string.Format("Error occured when HandleRequest:{0} ", ex.Message));
                }
            }
            finally { }
            return goRecieving;
        }
Exemplo n.º 11
0
 public virtual void ShutdownClient(ClientManager clmngr)
 {
     if (clmngr == null)
         return;
     try
     {
         clmngr.Socket.Shutdown(SocketShutdown.Both);
         clmngr.Socket.Close();
     }
     catch (Exception ex) { string strError = ex.Message; }
     finally
     {
         RemoveClientItem(clmngr);
     }
 }
Exemplo n.º 12
0
 public virtual void HandleRequest(ClientManager clmngr, HttpLib.HttpContext context)
 {
 }
Exemplo n.º 13
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="clmngr"></param>
 /// <param name="request"></param>
 /// <returns>true = 继续接收数据</returns>
 public virtual bool HandleRequest(ClientManager clmngr, ISocketMessage request)
 {
     return true;
 }
Exemplo n.º 14
0
 protected void SetupReceiveCallback(ClientManager clmngr)
 {
     if (clmngr == null)
         return;
     try
     {
         AsyncCallback recieveData = new AsyncCallback(OnReceivedData);
         clmngr.Socket.BeginReceive(clmngr._szBuffer, 0, clmngr._szBuffer.Length, SocketFlags.None, recieveData, clmngr);
     }
     catch
     {
         ShutdownClient(clmngr);
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// 从socket队列中移除某项
        /// </summary>
        /// <param name="clmngr"></param>
        protected void RemoveClientItem(ClientManager clmngr)
        {
            if (clmngr == null)
                return;

            try
            {
                lock (this._Clients.SyncRoot)
                {
                    this._Clients.Remove(clmngr.nSessionID);
                }
            }
            catch { }
            finally { clmngr = null; }
        }
Exemplo n.º 16
0
        protected virtual bool RegistUser(ClientManager clmngr, int uid)
        {
            if (clmngr == null || uid < 1 || !this._Clients.ContainsKey(clmngr.nSessionID))
                return false;

            clmngr.IsAuthedUser = true;
            clmngr.nUID = uid;

            return true;
        }
Exemplo n.º 17
0
        protected virtual bool RegistTrustIP(ClientManager clmngr)
        {
            if (clmngr == null)
                return false;

            string ipAddr = ((IPEndPoint)clmngr.Socket.RemoteEndPoint).Address.ToString();
            long remoteIP = IPUtility.GetIPIntValue(ipAddr);
            try
            {
                if (remoteIP == _LocalhostIP)
                    return false;

                if (_Config.TrustRemoteIP != null && _Config.TrustRemoteIP.Count > 0)
                {
                    foreach (long ip in _Config.TrustRemoteIP)
                    {
                        if (ip == remoteIP)
                        {
                            clmngr.IsTrustIP = true;
                            break;
                        }
                    }
                }
            }
            catch { }

            return clmngr.IsTrustIP;
        }
Exemplo n.º 18
0
        /// <summary>
        /// 登记连接到临时队列中
        /// </summary>
        /// <param name="clientSock"></param>
        protected virtual void CreateNewClient(Socket clientSock)
        {
            if (clientSock == null)
                return;
            try
            {
                //将该Socket挂入管理池
                ClientManager clmngr = new ClientManager(clientSock);
                RegistTrustIP(clmngr);

                lock (this._Clients.SyncRoot)
                {
                    this._Clients[clmngr.nSessionID] = clmngr;
                }
                SetupReceiveCallback(clmngr);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error occured when CreateNewClient:{0} ", ex.Message));
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 将本次KeepAlive的请求登记到处理队列中
        /// </summary>
        /// <param name="clmngr"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private HttpContext RegistKeepAliveRequestContext(ClientManager clmngr, HttpRequest request)
        {
            HttpContext context = null;

            if (clmngr == null || !clmngr.IsAuthedUser || !this._Clients.ContainsKey(clmngr.nSessionID) || request == null || request.IsRequestError)
                return context;

            clmngr.IsKeepAlive = true;

            //Context Table使用User ID作为Key
            int uid = clmngr.nUID;

            lock (_ContextTable.SyncRoot)
            {
                if (_ContextTable.ContainsKey(uid))
                {
                    context = _ContextTable[uid] as HttpContext;
                    //将请求添加到队列
                    context.AppendRequest(request);
                }
                else
                {
                    context = new HttpContext(this, clmngr, request);
                    //将请求添加到队列
                    _ContextTable[uid] = context;
                }
            }

            return context;
        }
Exemplo n.º 20
0
 /// <summary>
 /// 注册用户请求
 /// </summary>
 /// <param name="clmngr"></param>
 /// <param name="request"></param>
 private void RegistUser(ClientManager clmngr, HttpRequest request)
 {
     if (clmngr == null || request == null)
         return;
     int userID = UserAuthLogic.AnalysisUserIDFromRequestCookies(request.Cookies);
     if (userID > 0)
     {
         RegistUser(clmngr, userID);
     }
 }
Exemplo n.º 21
0
        public virtual void SendToClient(ClientManager clmngr, ISocketMessage msg)
        {
            try
            {
                if (clmngr == null || msg == null || msg.Length == 0)
                    return;

                byte[] buf = msg.GetBytes();
                if (buf == null || buf.Length < 1)
                    return;
                int i = clmngr.Socket.Send(buf, buf.Length, SocketFlags.None);
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
                DebugMessage(string.Format("SendToClient Error:{0};Source:{1}", ex.Message, ex.Source));
            }
            finally { }
        }