Beispiel #1
0
        /// <summary>
        /// 把需要监听的客户端平均分配到各个线程中
        /// </summary>
        /// <param name="request"></param>
        public static void QueueCometWaitRequest(CometWaitRequest request)
        {
            CometWaitThread waitThread;

            //#region 把需要监听的客户端平均分配到各个线程中
            //lock (state)
            //{
            //    // else, get the next wait thread
            //    waitThread = waitThreads[nextWaitThread];
            //    // cycle the thread that we want
            //    nextWaitThread++;
            //    if (nextWaitThread == maxWaitThreads)
            //        nextWaitThread = 0;
            //    CometWaitRequest.RequestCount++;
            //}
            //#endregion

            //根据SendUserId求余分配客户端到指定线程
            lock (state)
            {
                int nThread = GuidCoveter.CoveterFirstChat(request.SendUserId) % maxWaitThreads;
                waitThread = waitThreads[nThread];
                CometWaitRequest.RequestCount++;
            }

            // 加入线程
            waitThread.QueueCometWaitRequest(request);
        }
Beispiel #2
0
 /// <summary>
 /// 根据用户Id从当前线程队列移除监听客户端
 /// </summary>
 public static void DequeueCometWaitRequest(Guid sendUserId)
 {
     lock (state)
     {
         //根据sendUserId求余获取当前用户所在线程
         int             nThread    = GuidCoveter.CoveterFirstChat(sendUserId) % maxWaitThreads;
         CometWaitThread waitThread = waitThreads[nThread];
         waitThread.DequeueCometWaitRequest(sendUserId);
     }
 }
Beispiel #3
0
        /// <summary>
        /// 返回用户所处的线程
        /// </summary>
        public static CometWaitThread GetUserThread(Guid sendUserId)
        {
            //根据sendUserId求余获取当前用户所在线程
            int nThread = GuidCoveter.CoveterFirstChat(sendUserId) % maxWaitThreads;

            if (waitThreads != null)
            {
                CometWaitThread waitThread = waitThreads[nThread];
                return(waitThread);
            }

            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// 获取用户消息
        /// </summary>
        public List <MessageModel> GetMessage(Guid receiveUserId)
        {
            try
            {
                List <MessageModel> listMessageModel = new List <MessageModel>();
                int nGroup = GuidCoveter.CoveterFirstChat(receiveUserId) % groups;

                Thread.Sleep(3);

                if (listMessage[nGroup] == null)
                {
                    listMessage[nGroup] = new SafeCollection <MessageModel>();
                }

                foreach (MessageModel model in listMessage[nGroup])
                {
                    //清理掉超时5分钟没被领走的消息
                    if (DateTime.Now.Subtract(model.SendTimeOfService).TotalSeconds > 300)
                    {
                        listMessage[nGroup].Remove(model);
                        continue;
                    }

                    if (model.ReceiveUserId == receiveUserId)
                    {
                        listMessageModel.Add(model);
                        listMessage[nGroup].Remove(model);
                    }
                }

                return(listMessageModel);
            }
            catch (Exception exception)
            {
                log.Warn("获取线程内消息队列出现异常:" + exception.Message);
                return(new List <MessageModel>());
            }
        }
Beispiel #5
0
        /// <summary>
        /// 取用户的通知
        /// </summary>
        /// <param name="receiveUserId"></param>
        /// <returns></returns>
        public List <NoticeModel> GetNotice(Guid receiveUserId)
        {
            try
            {
                List <NoticeModel> listNoticeModel = new List <NoticeModel>();
                int nGroup = GuidCoveter.CoveterFirstChat(receiveUserId) % groups;

                Thread.Sleep(3);

                if (listNotice[nGroup] == null)
                {
                    listNotice[nGroup] = new SafeCollection <NoticeModel>();
                }

                foreach (NoticeModel model in listNotice[nGroup])
                {
                    //清理掉超时50秒没被领走的消息
                    if (DateTime.Now.Subtract(model.LastActiveDate).Seconds > 50)
                    {
                        listNotice[nGroup].Remove(model);
                        continue;
                    }

                    if (model.ReceiveUserId == receiveUserId)
                    {
                        listNoticeModel.Add(model);
                        listNotice[nGroup].Remove(model);
                    }
                }

                return(listNoticeModel);
            }
            catch (Exception exception)
            {
                log.Warn("获取线程内通知队列出现异常:" + exception.Message);
                return(new List <NoticeModel>());
            }
        }
Beispiel #6
0
        /// <summary>
        /// 添加用户消息
        /// </summary>
        public void AddMessage(MessageModel model)
        {
            try
            {
                if (model == null)
                {
                    return;
                }

                int group = GuidCoveter.CoveterFirstChat(model.ReceiveUserId) % groups;

                if (listMessage[group] == null)
                {
                    listMessage[group] = new SafeCollection <MessageModel>();
                }

                listMessage[group].Add(model);
            }
            catch (Exception exception)
            {
                log.Warn("发送消息给线程内队列出现异常:" + exception.Message);
            }
        }