/// <summary>
        /// 写入打包对象。
        /// </summary>
        /// <remarks>写入打包对象。</remarks>
        /// <param name="pack">设置打包对象。</param>
        internal virtual void Write(WriteStreamPackaging pack)
        {
            //刷新打包数据。
            pack.Flush();
            //声明会话传输通道。
            SessionTransferChannel channel = null;

            try
            {
                //获取会话传输通道对象,返回必须是被锁定的会话传输
                channel = this.GetChannel();
                //验证会话传输通道是否被锁定。
                if (!channel.IsLock)
#if DEBUG
                { throw new Exception("获取会话传输通道没有被锁定错误![Key:" + channel.Key.ToString() + "]"); }
#else
                { throw new Exception("获取会话传输通道没有被锁定错误!"); }
#endif
                //重置通道空闲超时计数器。
                channel.CurrentChannelTimeoutCounter = 0;
                //写入数据流。
                channel.Stream.Write(pack.GetBuffer, 0, pack.GetBuffer.Length);
            }
            finally
            {
                //解锁会话传输通道。
                channel.UnLock();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 写入数组要打包的对象。
        /// </summary>
        /// <remarks>写入数组要打包的对象。</remarks>
        /// <param name="packItems">设置数组要打包的对象。</param>
        internal virtual void Write(object[] packItems)
        {
            //重置通道空闲超时计数器。
            this.CurrentChannelTimeoutCounter = 0;
            //创建写入流打包对象。
            WriteStreamPackaging pack = this.CreateWriteStreamPackaging(true);

            //写入要打包的数组。
            pack.Write(packItems);
            //刷新发送打包的数组对象。
            pack.Flush();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 从会话数据存储区中返回只读会话状态数据。
        /// </summary>
        /// <remarks>从会话数据存储区中返回只读会话状态数据。</remarks>
        /// <param name="lockRecord">锁记录。</param>
        /// <param name="context">当前请求的 HttpContext。</param>
        /// <param name="id">当前请求的 SessionID。</param>
        /// <param name="locked">当此方法返回时,如果请求的会话项在会话数据存储区被锁定,请包含一个设置为 true 的布尔值;否则请包含一个设置为 false 的布尔值。</param>
        /// <param name="lockAge">当此方法返回时,请包含一个设置为会话数据存储区中的项锁定时间的 TimeSpan 对象。</param>
        /// <param name="lockId">当此方法返回时,请包含一个设置为当前请求的锁定标识符的对象。有关锁定标识符的详细信息,请参见 SessionStateStoreProviderBase 类摘要中的“锁定会话存储区数据”。</param>
        /// <param name="actions">当此方法返回时,请包含 SessionStateActions 值之一,指示当前会话是否为未初始化的无 Cookie 会话。</param>
        /// <returns>使用会话数据存储区中的会话值和信息填充的 SessionStateStoreData。</returns>
        private SessionStateStoreData GetSessionStoreItem(bool lockRecord, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            Debug.WriteLine("GetSessionStoreItem[锁记录:" + lockRecord.ToString() + ",会话id:" + (id == null ? "null" : id) + "]", "SessionStore");
            //声明
            //会话容器
            SessionStateStoreData item = null;
            //锁定时间
            lockAge = TimeSpan.Zero;
            //锁定Id
            lockId = null;
            //锁定
            locked = false;
            //动作
            actions = 0;
            //过期
            DateTime expires;
            //找到记录
            bool foundRecord = false;
            //返回过期删除
            bool deleteData = false;
            //超时时间
            int timeout = 0;
            //创建会话数据集合。
            SessionStateItemCollection ic = new SessionStateItemCollection();
            //声明会话传输通道。
            SessionTransferChannel channel = null;
            try
            {
                //获取会话传输通道对象,返回必须是被锁定的会话传输
                channel = this.ChannelManager.GetChannel();
                if (!channel.IsLock)
#if DEBUG
                    throw new Exception("获取会话传输通道没有被锁定错误![Key:" + channel.Key.ToString() + "]");
#else
                    throw new Exception("获取会话传输通道没有被锁定错误!");  
#endif
                //
                channel.Write(new object[] { 
                    //指令
                    (byte)SessionStoreCommandHeader.GetItemExclusive,
                    //会话Id
                    id, 
                    //锁记录
                    lockRecord });
                //锁记录。
                if (lockRecord)
                {
                    channel.Write(new object[] { 
                        //锁定
                        true, 
                        //锁定时间
                        DateTime.Now,
                        //过期时间
                        DateTime.Now, });
                    //读取锁状态。
                    if (!channel.Reader.ReadBoolean())
                        //没有记录被更新,因为记录被锁定或没有找到。
                        locked = true;
                    else
                        //记录已更新。
                        locked = false;
                }
                //
                if (channel.Reader.ReadBoolean())
                {
                    //过期
                    expires = channel.ReadDateTime();
                    //验证是否过期
                    if (expires < DateTime.Now)
                    {
                        //该纪录是过期。将其标记为未锁定。
                        locked = false;
                        //会话已过期。标记为删除的数据。
                        deleteData = true;
                    }
                    else
                        //未过期
                        foundRecord = true;
                    //锁定Id
                    lockId = channel.Reader.ReadInt32();
                    //锁定经过时间
                    lockAge = channel.ReadTimeSpan();
                    //动作
                    actions = (SessionStateActions)channel.Reader.ReadInt32();
                    //超时
                    timeout = channel.Reader.ReadInt32();
                    //读取集合数据。
                    ReadItems(channel, ic);
                }
                //创建写入流打包对象。
                WriteStreamPackaging pack = channel.CreateWriteStreamPackaging(false);
                //是否删除
                pack.Write(deleteData);
                //记录没有被发现。确保锁定的是假的。
                if (!foundRecord)
                    //没有锁定
                    locked = false;
                //如果记录被发现,你获得了锁,然后设置
                //锁定,清除actionFlags,
                //并创建SessionStateStoreItem返回。
                if (foundRecord && !locked)
                {
                    //累加锁定Id
                    lockId = (int)lockId + 1;
                    //锁定
                    pack.Write(true);
                    //锁定Id
                    pack.Write((int)lockId);
                    //如果动作Flags参数未初始化项目。
                    //反序列化存储SessionStateItemCollection。
                    if (actions == SessionStateActions.InitializeItem)
                    {
                        item = new SessionStateStoreData(new SessionStateItemCollection(), SessionStateUtility.GetSessionStaticObjects(context), this.SessionStateConfig.Timeout.Minutes);
                    }
                    else
                    {
                        item = new SessionStateStoreData(ic, SessionStateUtility.GetSessionStaticObjects(context), this.SessionStateConfig.Timeout.Minutes);
                    }
                }
                else
                {
                    //没有锁定
                    pack.Write(false);
                }
                pack.Flush(channel.Stream);
            }
            finally
            {
                channel.UnLock();
            }
            return item;
        }