public AddressBookItemConnectionManager this[HttpConnection connection] { get { foreach(AddressBookItemConnectionManager manager in base.InnerList) if (manager.Connection.Id == connection.Id) return manager; return null; } }
/// <summary> /// Removes the connection from the list /// </summary> /// <param name="connection">The connection to remove</param> /// <returns></returns> public bool Remove(HttpConnection connection) { if (connection == null) throw new ArgumentNullException("connection", "A null connection cannot be removed from the list."); if (this.Contains(connection.Id)) base.InnerList.Remove(connection); return true; }
/// <summary> /// Adds the connection to the list /// </summary> /// <param name="connection">The connection to add</param> /// <returns></returns> public bool Add(HttpConnection connection) { if (connection == null) throw new ArgumentNullException("connection", "A null connection cannot be added to the list."); if (this.Contains(connection.Id)) throw new Exception(string.Format("A connection already exists in the list with an id of {0}", connection.Id.ToString())); base.InnerList.Add(connection); return true; }
/// <summary> /// Initializes a new instance of the HttpConnectionEventArgs class /// </summary> /// <param name="connection">The connection responsible for the event</param> public HttpConnectionEventArgs(HttpConnection connection) : base() { _connection = connection; }
/// <summary> /// Creates a connection manager for the address book item, and connects the connection to the remote host specified by the address book item /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnConnectSessionForAddressBookItem(object sender, BackgroundThreadStartEventArgs e) { // retrieve the address book item that is requiring attention AddressBookItem item = (AddressBookItem)e.Args[0]; bool disconnectFirst = (bool)e.Args[1]; bool verboseSession = (bool)e.Args[2]; bool autoRecv = (bool)e.Args[3]; try { /* * if we are supposed to disconnect the current connection first * */ if (disconnectFirst) { lock(_sessionManagers) { // look up the item to see if an existing connection is already in use AddressBookItemConnectionManager prevManager = _sessionManagers[item]; // if a connection manager is found for the item then disconnect it if (prevManager != null) prevManager.Disconnect(); } } // create a new tcp connection in verbose mode HttpConnection connection = new HttpConnection(verboseSession); // create a new connection manager to manage the connection AddressBookItemConnectionManager manager = new AddressBookItemConnectionManager(item, connection); // wire up to the manager's events manager.ConnectionEstablishContext += new AddressBookItemConnectionManagerContextEventHandler(OnConnectionEstablishContext); manager.ConnectionResolvingAddress += new AddressBookItemConnectionManagerResolvingAddressEventHandler(OnConnectionResolvingAddress); manager.BeforeConnectionOpened += new AddressBookItemConnectionManagerCancelEventHandler(OnBeforeConnectionOpened); manager.BeforeConnectionClosed += new AddressBookItemConnectionManagerCancelEventHandler(OnBeforeConnectionClosed); manager.ConnectionOpened += new AddressBookItemConnectionManagerEventHandler(OnConnectionOpened); manager.ConnectionClosed += new AddressBookItemConnectionManagerEventHandler(OnConnectionClosed); manager.ConnectionException += new AddressBookItemConnectionManagerExceptionEventHandler(OnConnectionException); // create the thread context that will enable us to determine the background thread upon which this connection manager is operating manager.ThreadContext = new AddressBookItemBackgroundThreadContext((BackgroundThread)sender, item); // instruct the manager to connect the connection to the remote host // pass it instructions on whether it should begin receiving automatically or not // NOTE: In almost every case from a client created connection this will be false. // Only server created sessions will be set to automatically receive. manager.Connect(autoRecv, manager.ThreadContext); // add the connection manager to our list of connection managers lock(_sessionManagers) _sessionManagers.Add(manager); } catch(ThreadAbortException) { // ignore thread abort exceptions } catch(Exception ex) { Debug.WriteLine(ex); } }