Exemplo n.º 1
0
 /// <summary>
 /// 增加监听客户端到当前线程队列
 /// </summary>
 /// <param name="request"></param>
 internal void QueueCometWaitRequest(CometWaitRequest request)
 {
     lock (this.state)
     {
         waitRequests.Add(request);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 处理服务器请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private object CheckForServerPushEvent(CometWaitRequest request, bool timeout)
        {
            if (timeout)
            {
                return("Response from: " + Thread.CurrentThread.ManagedThreadId.ToString());
            }

            List <MessageModel> listMessage = new List <MessageModel>();
            List <NoticeModel>  listNotice  = new List <NoticeModel>();

            //取用户消息信息
            listMessage = GetMessage(request.SendUserId);
            //取用户好友相关通知
            listNotice = GetNotice(request.SendUserId);

            if (listMessage.Count == 0 && listNotice.Count == 0)
            {
                return(null);
            }

            ArgumentModel modelArgument = new ArgumentModel();

            modelArgument.listMessage = listMessage;
            modelArgument.listNotice  = listNotice;
            return(modelArgument);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 从当前线程队列移除监听客户端
 /// </summary>
 /// <param name="request"></param>
 internal void DequeueCometWaitRequest(CometWaitRequest request)
 {
     lock (this.state)
     {
         this.waitRequests.Remove(request);
         CometWaitRequest.RequestCount--;
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 开始线程处理
        /// </summary>
        private void QueueCometWaitRequest_WaitCallback()
        {
            try
            {
                while (true)
                {
                    CometWaitRequest[] processRequest;

                    lock (this.state)
                    {
                        processRequest = waitRequests.ToArray();
                    }

                    Thread.Sleep(500);

                    for (int i = 0; i < processRequest.Length; i++)
                    {
                        CometWaitRequest cometWaitRequest = processRequest[i];

                        //  超时则从线程队列中移除对该客户端的监听
                        if (DateTime.Now.Subtract(cometWaitRequest.DateTimeAdded).TotalSeconds >= 20)
                        {
                            //从当前线程队列移除监听客户端
                            DequeueCometWaitRequest(cometWaitRequest);
                            this.QueueCometWaitRequest_Finished(cometWaitRequest, null);
                        }
                        else
                        {
                            object serverPushEvent = this.CheckForServerPushEvent(processRequest[i], false);

                            if (serverPushEvent != null)
                            {
                                ArgumentModel modelArgument = (ArgumentModel)serverPushEvent;
                                this.QueueCometWaitRequest_Finished(cometWaitRequest, modelArgument);
                                //从当前线程队列移除监听客户端
                                DequeueCometWaitRequest(cometWaitRequest);
                            }
                        }

                        Thread.Sleep(5);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Warn(ex);
                QueueCometWaitRequest_WaitCallback();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 根据用户Id从当前线程队列移除监听客户端
        /// </summary>
        /// <param name="sendUserId"></param>
        internal void DequeueCometWaitRequest(Guid sendUserId)
        {
            lock (this.state)
            {
                for (int i = 0; i < this.waitRequests.Count; i++)
                {
                    CometWaitRequest request = (CometWaitRequest)this.waitRequests[i];

                    if (request.SendUserId == sendUserId)
                    {
                        this.waitRequests.Remove(request);
                        CometWaitRequest.RequestCount--;
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 当前客户端监听任务完成执行函数
        /// </summary>
        /// <param name="target"></param>
        private void QueueCometWaitRequest_Finished(object target, ArgumentModel modelArgument)
        {
            CometWaitRequest request = target as CometWaitRequest;

            request.execute.BeginInvoke(modelArgument, null, null);
        }