1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd.,
39 // Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
41 // Portions created by LShift Ltd., Cohesive Financial Technologies
42 // LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 // LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
46 // All Rights Reserved.
48 // Contributor(s): ______________________________________.
50 //---------------------------------------------------------------------------
52 using System.Threading;
53 using System.Collections;
55 using RabbitMQ.Client;
56 using RabbitMQ.Client.Exceptions;
59 namespace RabbitMQ.Client.Impl
61 public class SessionManager
63 private readonly Hashtable m_sessionMap = new Hashtable();
64 private readonly ConnectionBase m_connection;
65 private ushort m_channelMax = 0;
66 private bool m_autoClose = false;
68 public SessionManager(ConnectionBase connection)
70 m_connection = connection;
73 public ushort ChannelMax
81 if ((value < 2) && (value != 0))
83 // We currently interpret channel max as
84 // *including* channel zero. We also think it
85 // doesn't make sense to forbid opening of any
86 // real usable channels, so by our ch0-including
87 // assumption, the minimum *useful* value for
90 // This code is here to work around OpenAMQ
91 // 1.2c4's channel max setting of 1.
93 // FIXME: warning? or is there something more
94 // sensible we can do here?
104 public bool AutoClose
121 return m_sessionMap.Count;
125 public ISession Lookup(int number)
129 return (ISession) m_sessionMap[number];
133 public ISession Create()
137 int channelNumber = Allocate();
138 if (channelNumber == -1)
140 throw new ChannelAllocationException();
142 return Create(channelNumber);
146 public ISession Create(int channelNumber)
151 if (m_sessionMap.ContainsKey(channelNumber))
153 throw new ChannelAllocationException(channelNumber);
155 session = new Session(m_connection, channelNumber);
156 session.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
157 //Console.WriteLine("SessionManager adding session "+session);
158 m_sessionMap[channelNumber] = session;
163 ///<summary>Replace an active session slot with a new ISession
164 ///implementation. Used during channel quiescing.</summary>
166 /// Make sure you pass in a channelNumber that's currently in
167 /// use, as if the slot is unused, you'll get a null pointer
170 public ISession Swap(int channelNumber, ISession replacement) {
173 ISession previous = (ISession) m_sessionMap[channelNumber];
174 previous.SessionShutdown -= new SessionShutdownEventHandler(HandleSessionShutdown);
175 m_sessionMap[channelNumber] = replacement;
176 replacement.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
181 ///<summary>Find an unused channel number. Must be called
182 ///while holding m_sessionMap lock!</summary>
184 /// Returns -1 if no unused channel numbers are available.
186 public int Allocate()
188 ushort maxChannels = (m_channelMax == 0) ? ushort.MaxValue : m_channelMax;
189 for (int candidate = 1; candidate < maxChannels; candidate++)
191 if (!m_sessionMap.ContainsKey(candidate))
199 public void HandleSessionShutdown(ISession session, ShutdownEventArgs reason)
201 //Console.WriteLine("SessionManager removing session "+session);
204 m_sessionMap.Remove(session.ChannelNumber);
209 ///<summary>If m_autoClose and there are no active sessions
210 ///remaining, Close()s the connection with reason code
212 public void CheckAutoClose()
218 if (m_sessionMap.Count == 0)
220 // Run this in a background thread, because
221 // usually CheckAutoClose will be called from
222 // HandleSessionShutdown above, which runs in
223 // the thread of the connection. If we were to
224 // attempt to close the connection from within
225 // the connection's thread, we would suffer a
226 // deadlock as the connection thread would be
227 // blocking waiting for its own mainloop to
229 new Thread(new ThreadStart(AutoCloseConnection)).Start();
235 ///<summary>Called from CheckAutoClose, in a separate thread,
236 ///when we decide to close the connection.</summary>
237 public void AutoCloseConnection()
239 m_connection.Abort(200, "AutoClose", ShutdownInitiator.Library, Timeout.Infinite);