Exemplo n.º 1
0
        /// <summary>
        /// 读取会话键集合内容。
        /// </summary>
        /// <remarks>读取会话键集合内容。</remarks>
        /// <param name="channel">设置读取通道对象。</param>
        /// <param name="itemCollection">设置会话容器键集合对象。</param>
        private static void ReadItems(SessionTransferChannel channel, SessionStateItemCollection itemCollection)
        {
            //声明获取会话存放键集合数量。
#if DEBUG
            int itemCount = channel.Reader.ReadInt32();
            Debug.WriteLine("ReadItems[itemCount:" + itemCount.ToString() + "]", "SessionStore");
            //验证要读取的会话容器键集合数量。
            if (itemCount > 0)
            {
                //遍历循环读取所有键与对象。
                for (int i = 0; i < itemCount; i++)
                {
                    //获取存储数据类型。
                    SessionStoreDataType dataType = (SessionStoreDataType)channel.Reader.ReadByte();
                    switch (dataType)
                    {
#else
                for (int i = 0; i < channel.Reader.ReadInt32(); i++)
                {
                    switch ((SessionStoreDataType)channel.Reader.ReadByte())
                    {
#endif
                        case SessionStoreDataType.Null:
                            //读取空值对象。
                            itemCollection[channel.ReadString()] = null;
                            break;
                        case SessionStoreDataType.Bool:
                            //读取Bool值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadBoolean();
                            break;
                        case SessionStoreDataType.Byte:
                            //读取Byte值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadByte();
                            break;
                        case SessionStoreDataType.SByte:
                            //读取SByte值对象。
                            itemCollection[channel.ReadString()] = unchecked((sbyte)channel.Reader.ReadByte());
                            break;
                        case SessionStoreDataType.Short:
                            //读取Short值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadInt16();
                            break;
                        case SessionStoreDataType.UShort:
                            //读取UShort值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadUInt16();
                            break;
                        case SessionStoreDataType.Int:
                            //读取Int值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadInt32();
                            break;
                        case SessionStoreDataType.UInt:
                            //读取UInt值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadUInt32();
                            break;
                        case SessionStoreDataType.Long:
                            //读取Long值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadInt64();
                            break;
                        case SessionStoreDataType.ULong:
                            //读取ULong值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadUInt64();
                            break;
                        case SessionStoreDataType.Float:
                            //读取Float值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadSingle();
                            break;
                        case SessionStoreDataType.Decimal:
                            //读取Decimal值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadDecimal();
                            break;
                        case SessionStoreDataType.Double:
                            //读取Double值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadDouble();
                            break;
                        case SessionStoreDataType.String:
                            //读取String值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadString();
                            break;
                        case SessionStoreDataType.Guid:
                            //读取Guid值对象。
                            itemCollection[channel.ReadString()] = channel.ReadGuid();
                            break;
                        case SessionStoreDataType.Buffer:
                            //读取Buffer值对象。
                            itemCollection[channel.ReadString()] = channel.ReadBytes();
                            break;
                        case SessionStoreDataType.DateTime:
                            //读取DateTime值对象。
                            itemCollection[channel.ReadString()] = channel.ReadDateTime();
                            break;
                        case SessionStoreDataType.TimeSpan:
                            //读取TimeSpan值对象。
                            itemCollection[channel.ReadString()] = channel.ReadTimeSpan();
                            break;
                        case SessionStoreDataType.Object:
                            //读取Object值对象。
                            itemCollection[channel.ReadString()] = channel.ReadObject();
                            break;
                        case SessionStoreDataType.Icon:
                            //读取Icon值对象。
                            using (MemoryStream outms = new MemoryStream(channel.ReadBytes()))
                            {
                                itemCollection[channel.ReadString()] = new Icon(outms);
                            }
                            break;
                        case SessionStoreDataType.Image:
                            //读取Image值对象。
                            using (MemoryStream outms = new MemoryStream(channel.ReadBytes()))
                            {
                                itemCollection[channel.ReadString()] = new Bitmap(outms);
                            }
                            break;
                        case SessionStoreDataType.Char:
                            //读取Char值对象。
                            itemCollection[channel.ReadString()] = channel.Reader.ReadChar();
                            break;
                        case SessionStoreDataType.Chars:
                            //读取Chars值对象。
                            itemCollection[channel.ReadString()] = channel.ReadChars();
                            break;
                    }
                }
#if DEBUG
            }
#endif
        }
Exemplo n.º 2
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;
        }