public void StoreInstance(PipeInstance instance)
        {
            // parameters validation
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            if (instance.IsConnected)
            {
                try
                {
                    // we can reuse this instance later, so disconnect a client
                    // from the pipe to allow another client to connect
                    instance.DisconnectFromClient();
                }
                catch
                {
                    // we lose this instance...
                    instance.Close();
                    return;
                }
            }

            lock (_instances)
            {
                if (_instances.Count < _pipe.PoolSize)
                {
                    // store pipe instance for future use
                    _instances.Add(instance);
                }
                else
                {
                    // we are reach a maximum pool size, so simply close this instance
                    instance.Close();
                }
            }
        }
示例#2
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            try
            {
                if (_parent == null)
                {
                    if (_clientRequests != null)
                    {
                        // stop the request queue
                        _clientRequests.StopListening();
                    }
                }
                else
                {
                    if (_instance == _parent.Instance)
                    {
                        // LAME: Two Pipe objects can share the same pipe instance - that is
                        // the first pipe instance. In this case we could not close pipe as
                        // it can be the last one...
                        _instance.DisconnectFromClient();
                        _instance = null;
                    }
                    else if (!AppDomain.CurrentDomain.IsFinalizingForUnload())
                    {
                        // return instance back to the pool
                        _parent.InstancePool.StoreInstance(_instance);
                        _instance = null;
                    }
                }
                if (_instance != null)
                {
                    // close the pipe instance
                    _instance.Close();
                    _instance = null;
                }
            }
            catch
            {
                // it seems reasonable to ignore this error
            }

            _disposed = true;
        }
        private void ListenerStart()
        {
            while (_isListening)
            {
                PipeInstance instance = null;
                try
                {
                    // get an instance from the pool
                    instance = _instancePool.GetInstance();

                    // wait for incoming client request
                    instance.WaitForClientConnection();
                }
                catch
                {
                    if (instance != null)
                    {
                        // close the pipe as it likely is not valid
                        instance.Close();
                        instance = null;
                    }
                }

                if (instance != null)
                {
                    lock (_requests)
                    {
                        while (_requests.Count >= _backlog)
                        {
                            // we are out of space in the queue, so wait for it
                            Monitor.Wait(_requests);
                        }

                        // add request to the queue
                        _requests.Enqueue(instance);

                        // notify the GetRequest thread that there are available requests
                        Monitor.Pulse(_requests);
                    }
                }
            }
        }
		public void StoreInstance(PipeInstance instance)
		{
			// parameters validation
			if (instance == null)
				throw new ArgumentNullException("instance");

			if (instance.IsConnected)
			{
				try
				{
					// we can reuse this instance later, so disconnect a client
					// from the pipe to allow another client to connect
					instance.DisconnectFromClient();
				}
				catch
				{
					// we lose this instance...
					instance.Close();
					return;
				}
			}

			lock (_instances)
			{
				if (_instances.Count < _pipe.PoolSize)
				{
					// store pipe instance for future use
					_instances.Add(instance);
				}
				else
				{
					// we are reach a maximum pool size, so simply close this instance
					instance.Close();
				}
			}
		}