Ejemplo n.º 1
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    if (_clientList != null)
                    {
                        // Close and dispose of all connections.
                        foreach (KeyValuePair <string, Nequeo.Net.Sockets.IClient> item in _clientList)
                        {
                            if (item.Value != null)
                            {
                                try
                                {
                                    item.Value.Close();
                                    item.Value.Dispose();
                                }
                                finally
                                {
                                    Nequeo.Net.Sockets.IClient client = item.Value;
                                    client = null;
                                }
                            }
                        }
                    }

                    // Dispose managed resources.
                    if (_clientList != null)
                    {
                        _clientList.Clear();
                    }

                    if (_messageQueue != null)
                    {
                        _messageQueue.Clear();
                    }

                    if (_clientTokens != null)
                    {
                        _clientTokens.Clear();
                    }
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                _clientList   = null;
                _messageQueue = null;
                _clientTokens = null;
                _lockObject   = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add the client item.
        /// </summary>
        /// <param name="key">The key for the client.</param>
        /// <param name="client">The client connected to the machine.</param>
        /// <exception cref="System.Exception"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public virtual void AddClient(string key, Nequeo.Net.Sockets.IClient client)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            lock (_lockObject)
            {
                // Add the item.
                _clientList.Add(key, client);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Find the client.
        /// </summary>
        /// <param name="key">The key for the client.</param>
        /// <returns>The client; else null.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public virtual Nequeo.Net.Sockets.IClient FindClient(string key)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            // Attempt to find the client.
            Nequeo.Net.Sockets.IClient client = null;
            bool ret = _clientList.TryGetValue(key, out client);

            if (ret)
            {
                return(client);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Remove the client item.
        /// </summary>
        /// <param name="key">The key for the client.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        public virtual void RemoveClient(string key)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            lock (_lockObject)
            {
                // Find the client.
                Nequeo.Net.Sockets.IClient client = FindClient(key);
                if (client != null)
                {
                    // Close the connection.
                    client.Close();
                    client.Dispose();
                }

                // Remove the item.
                _clientList.Remove(key);
            }
        }