/// <summary> /// 获取会话传输通道。 /// </summary> /// <remarks>获取会话传输通道。</remarks> /// <returns>SessionsStore.SessionTransferChannel</returns> internal SessionTransferChannel GetChannel() { //重新开始等待可用的会话传输通道。 Restart: //锁定会话传输通道列表。 lock (this.SyncRoot) { //验证释放有可用未被锁定的会话传输通道。 if (this.list.Count > this.LockCount()) { //遍历全部会话传输通道对象。 foreach (SessionTransferChannel item in this.list.Values) { //锁定会话传输通道。 if (item.Lock()) { Debug.WriteLine("找到可以用的通道获取它[Key:" + item.Key.ToString() + "]", "SessionTransferChannelManager.GetChannel"); //得到被有效锁定的会话传输通道。 return(item); } } } } //验证是否有最大会话传输通道限制。 if (this.Owner.MaxSessionTransferChannel > 0) { //声明会话传输通道数量。 int n = this.Count; //验证会话传输通道是否已经在最大数量。 if (n >= this.Owner.MaxSessionTransferChannel) { //会话传输通道已经在最大数量不允许再创建新的会话传输通道。 goto Restart; } } //创建新的会话传输通道。 SessionTransferChannel channel = null; try { //新建会话传输通道对象。 channel = this.CreateChannel(); //连接到会话中心。 channel.Connection(); //锁定会话传输通道对象。 channel.Lock(); //将新建的会话传输通道添加到会话传输通道列表中。 this.list.Add(channel.Key, channel); } catch (Exception e) { //验证通道对象是否为空。 if (channel != null) { //在通道列表中删除当前通道。 if (this.list.ContainsKey(channel.Key)) { this.list.Remove(channel.Key); } //是否通道资源。 try { channel.Dispose(); } finally { channel = null; } } throw e; } return(channel); }
/// <summary> /// 连接到全局会话中心。 /// </summary> /// <remarks>连接到全局会话中心。</remarks> internal void Connection() { //锁定会话传输通道管理对象。 lock (this.SyncRoot) { //声明会话传输通道。 SessionTransferChannel channel = null; try { //创建会话传输通道。 channel = this.CreateChannel(); //锁定通道。 lock (channel.SyncRoot) { //锁定。 channel.Lock(); //连接到全局会话中心。 channel.Connection(); //将会话传输通道添加入列表中。 this.list.Add(channel.Key, channel); //发送初始化指令。 channel.Write(new object[] { (byte)SessionStoreCommandHeader.Initialize, (byte)SessionTransferNodeTypes.ASP_NET_FORM }); //初始化会话传输字符编码。 #if DEBUG byte ed = channel.Reader.ReadByte(); Debug.WriteLine("Connection[CharacterEncoding:" + ed.ToString() + "]", "SessionTransferChannelManager"); switch ((CharacterEncoding)ed) #else switch ((CharacterEncoding)channel.Reader.ReadByte()) #endif { case CharacterEncoding.UTF8Encoding: this.Owner.Encoding = System.Text.Encoding.UTF8; break; case CharacterEncoding.UTF7Encoding: this.Owner.Encoding = System.Text.Encoding.UTF7; break; case CharacterEncoding.UTF32Encoding: this.Owner.Encoding = System.Text.Encoding.UTF32; break; case CharacterEncoding.UnicodeEncoding: this.Owner.Encoding = System.Text.Encoding.Unicode; break; case CharacterEncoding.ASCIIEncoding: this.Owner.Encoding = System.Text.Encoding.ASCII; break; case CharacterEncoding.BigEndianUnicode: this.Owner.Encoding = System.Text.Encoding.BigEndianUnicode; break; } //初始化获取全局会话中心当前时间。 this.Owner.SessionCenterCurrentDateTime = channel.ReadDateTime(); Debug.WriteLine("Connection[SessionCenterCurrentDateTime:" + this.Owner.SessionCenterCurrentDateTime.ToString() + "]", "SessionTransferChannelManager"); //初始化获取全局会话中心通道最大限制。 int MaxSessionTransferChannel = channel.Reader.ReadInt32(); if (MaxSessionTransferChannel > 0) { this.Owner.MaxSessionTransferChannel = MaxSessionTransferChannel; } Debug.WriteLine("Connection[MaxSessionTransferChannel:" + this.Owner.MaxSessionTransferChannel.ToString() + "]", "SessionTransferChannelManager"); //发送安全连接内容。 if (string.IsNullOrEmpty(this.Owner.SessionCenterSecureConnectionContent)) { channel.Writer.Write((int)0); Debug.WriteLine("Connection[SessionCenterSecureConnectionContent:" + (this.Owner.SessionCenterSecureConnectionContent == null ? "null" : this.Owner.SessionCenterSecureConnectionContent) + "]", "SessionTransferChannelManager"); } else { byte[] bytes = new byte[this.Owner.Encoding.GetByteCount(this.Owner.SessionCenterSecureConnectionContent)]; Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length), 0, bytes, 0, TypeLengthConstant.IntLength); Buffer.BlockCopy(this.Owner.Encoding.GetBytes(this.Owner.SessionCenterSecureConnectionContent), 0, bytes, TypeLengthConstant.IntLength, bytes.Length); channel.Writer.Write(bytes); Debug.WriteLine("Connection[SessionCenterSecureConnectionContent:" + this.Owner.SessionCenterSecureConnectionContent + "]", "SessionTransferChannelManager"); } //验证安全连接是否通过。 #if DEBUG if (channel.Reader.ReadBoolean()) { Debug.WriteLine("Connection[安全连接验证通过]", "SessionTransferChannelManager"); } else { Debug.WriteLine("Connection[安全连接验证不通过]", "SessionTransferChannelManager"); throw new Exception("安全连接验证不通过!"); } #else if (!channel.Reader.ReadBoolean()) { throw new Exception("安全连接验证不通过!"); } #endif } } catch (Exception e) { Debug.WriteLine("Connection[e:" + e.ToString() + "]", "SessionTransferChannelManager"); //验证通道对象是否为空。 if (channel != null) { //在通道列表中删除当前通道。 if (this.list.ContainsKey(channel.Key)) { this.list.Remove(channel.Key); } //是否通道资源。 try { channel.Dispose(); } finally { channel = null; } } throw e; } finally { //解锁。 if (channel != null) { channel.UnLock(); } } } }