Exemplo n.º 1
0
        /// <summary>
        /// 表示线程池线程要执行的回调方法。
        /// </summary>
        /// <param name="state"></param>
        private void SliceWorld(object state)
        {
            if (OneServer.Closing == true)
            {
                Interlocked.Decrement(ref m_SignalCount);
                return;
            }

            // 运行库为每个可执行文件创建一个异常信息表。在异常信息表中,可执行文件的每个方法都有一个关联的异常处理信息数组(可以为空)。
            // 数组中的每一项描述一个受保护的代码块、任何与该代码关联的异常筛选器和任何异常处理程序(Catch 语句)。此异常表非常有效,
            // 在没有发生异常时,在处理器时间或内存使用上没有性能损失。仅在异常发生时使用资源。
            try
            {
                EventHandler <BaseWorldEventArgs> tempEventArgs = m_EventStartSlice;
                if (tempEventArgs != null)
                {
                    tempEventArgs(this, m_BaseWorldEventArgs);
                }

                // 更新ISupportSlice的处理
                m_SliceUpdate.Slice();

                // AI处理
                AISystem.Slice();

                // 时间片
                TimeSlice.Slice();

                // 测试下来在数组方面for内只使用一个索引数值的时候比foreach速度快,当超过使用两个索引数值时就比foreach慢了。
                // 其他方面foreach比较快
                MessagePump[] tempArray = m_MessagePump;
                for (int iIndex = 0; iIndex < tempArray.Length; iIndex++)
                {
                    tempArray[iIndex].Slice();
                }

                // 发送缓存的数据
                this.FlushAll();

                // 处理已经断开的连接
                this.ProcessDisposed();

                tempEventArgs = m_EventEndSlice;
                if (tempEventArgs != null)
                {
                    tempEventArgs(this, m_BaseWorldEventArgs);
                }

                // 最后需要等待的单处理执行
                m_WaitExecute.Slice();
            }
            catch (Exception exception)
            {
                OneServer.UnhandledException(this, new UnhandledExceptionEventArgs(exception, true));
            }
            finally
            {
                Interlocked.Decrement(ref m_SignalCount);
            }
        }