Exemplo n.º 1
0
        protected IRunnable WaitTask()
        {
            IRunnable task;

            if (!mTaskQueue.TryDequeue(out task))
            {
                mEmptySemaphore.Reset();
                if (!mTaskQueue.TryDequeue(out task) && !mIsShuttingDown)
                {
                    ITriggerRunnable nextTriggerTask = this.PeekTrigger();
                    if (nextTriggerTask != null)
                    {
                        long delayticks = nextTriggerTask.DeadTime - Converter.Cast <long>(DateTime.Now);
                        if (delayticks > 0)
                        {
                            mEmptySemaphore.WaitOne((int)Math.Min(delayticks, int.MaxValue - 1));
                        }
                    }
                    else
                    {
                        mEmptySemaphore.WaitOne();
                    }
                }
            }
            return(task);
        }
Exemplo n.º 2
0
        public void WriteFinish(IContext context, object msg, bool isssl)
        {
            ChannelBase  channel  = context.Channel;
            HttpResponse response = msg as HttpResponse;

            if (response != null)
            {
                if (_keepalive)
                {
                    ClearTimeOut(channel);
                    ///指定时间后关闭连接
                    _timeout = new TimeOut <IContext>(c =>
                    {
                        c.Channel.Dispose();
                    }, context, _config.KeepTime * 1000);
                    channel.Loop.Execute(_timeout);
                }
                else
                {
                    context.Channel.Dispose();
                }
                if (isssl)
                {
                    response.Dispose();
                }
            }
        }
Exemplo n.º 3
0
 protected void EnqueueTrigger(ITriggerRunnable trigger)
 {
     lock (mTriggers)
     {
         mTriggers.Enqueue(trigger);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 弹出下一个到期触发器
 /// </summary>
 /// <returns></returns>
 protected ITriggerRunnable PopNextTrigger()
 {
     lock (mTriggers)
     {
         ITriggerRunnable tr = mTriggers.Peek();
         if (tr == null)
         {
             return(null);
         }
         long nowticks = Converter.Cast <long>(DateTime.Now);
         if (tr.DeadTime <= nowticks)
         {
             tr = mTriggers.Dequeue();
             if (tr.RepeatCount > 0)
             {
                 tr.RepeatCount--;
             }
             if (tr.RepeatCount != 0)
             {
                 tr.DeadTime = nowticks + tr.RepeatInterval;
                 mTriggers.Enqueue(tr);
             }
             return(tr);
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 5
0
 protected void ClearTimeOut(ChannelBase channel)
 {
     if (_timeout != null)
     {
         channel.Loop.ClearRun(_timeout);
         _timeout = null;
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 获取当前到期的定时任务并放入任务队列
        /// </summary>
        protected void FetchFromTriggerTask()
        {
            ITriggerRunnable triggerTask = this.PopNextTrigger();

            while (triggerTask != null)
            {
                mTaskQueue.Enqueue(triggerTask);
                triggerTask = this.PopNextTrigger();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 将要执行的任务压入任务队列
        /// </summary>
        /// <param name="task"></param>
        public override void Execute(IRunnable task)
        {
            ITriggerRunnable trigger = task as ITriggerRunnable;

            if (trigger != null)
            {
                EnqueueTrigger(trigger);
            }
            else
            {
                mTaskQueue.Enqueue(task);
            }
            if (mThread != Thread.CurrentThread)
            {
                mEmptySemaphore.Set();
            }
        }
Exemplo n.º 8
0
        public void RestartIdleTimeOut(IContext context)
        {
            ChannelBase channel = context.Channel;

            if (_readerIdleTimeMilliSeconds > 0)
            {
                if (_readIdle == null)
                {
                    _readIdle = new TimeOut <IContext>(c =>
                    {
                        if (!c.Channel.Active)
                        {
                            return;
                        }
                        long curtime = Converter.Cast <long>(DateTime.Now);
                        if (_readNextTime <= curtime)
                        {
                            c.Channel.Emit(CHANNEL_READ_IDLE, EventArgs.Empty);
                            _readNextTime = curtime + _readerIdleTimeMilliSeconds;
                        }
                        _readIdle.DeadTime = _readNextTime;
                        c.Channel.Loop.Execute(_readIdle);
                    }, context, _readerIdleTimeMilliSeconds);
                    channel.Loop.Execute(_readIdle);
                }
            }

            if (_writerIdleTimeMilliSeconds > 0)
            {
                if (_writeIdle == null)
                {
                    _writeIdle = new TimeOut <IContext>(c =>
                    {
                        if (!c.Channel.Active)
                        {
                            return;
                        }
                        long curtime = Converter.Cast <long>(DateTime.Now);
                        if (_writeNextTime <= curtime)
                        {
                            c.Channel.Emit(CHANNEL_WRITE_IDLE, EventArgs.Empty);
                            _writeNextTime = curtime + _writerIdleTimeMilliSeconds;
                        }
                        _writeIdle.DeadTime = _writeNextTime;
                        c.Channel.Loop.Execute(_writeIdle);
                    }, context, _writerIdleTimeMilliSeconds);
                    channel.Loop.Execute(_writeIdle);
                }
            }

            if (_allIdleTimeMilliSeconds > 0)
            {
                if (_allIdle == null)
                {
                    _allIdle = new TimeOut <IContext>(c =>
                    {
                        if (!c.Channel.Active)
                        {
                            return;
                        }
                        long curtime = Converter.Cast <long>(DateTime.Now);
                        if (_allNextTime <= curtime)
                        {
                            c.Channel.Emit(CHANNEL_ALL_IDLE, EventArgs.Empty);
                            _allNextTime = curtime + _allIdleTimeMilliSeconds;
                        }
                        _allIdle.DeadTime = _allNextTime;
                        c.Channel.Loop.Execute(_allIdle);
                    }, context, _allIdleTimeMilliSeconds);
                    channel.Loop.Execute(_allIdle);
                }
            }
        }