Exemple #1
0
        /// <summary>
        /// Establishes a connection to a remote system
        /// </summary>
        public void Connect(PipeName pipeName)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (_instance != null)
            {
                throw new InvalidOperationException("Pipe is already connected");
            }

            // connect to the remote host
            _instance = PipeInstance.Connect(pipeName);
        }
Exemple #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;
        }
 public PipeInstance GetInstance()
 {
     lock (_instances)
     {
         if (_instances.Count == 0)
         {
             // create a new pipe instance
             return(PipeInstance.Create(_pipe.Name, false, _pipe.SecurityDescriptor));
         }
         else
         {
             // reuse pooled instance (newer one)
             PipeInstance instance = (PipeInstance)_instances[_instances.Count - 1];
             _instances.Remove(instance);
             return(instance);
         }
     }
 }
Exemple #4
0
        /// <summary>
        /// Creates pipe on the server side to allow client to connect to
        /// </summary>
        public void Bind(PipeName pipeName, SecurityDescriptor securityDescriptor)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (_instance != null)
            {
                throw new InvalidOperationException("Pipe is already connected");
            }

            // store security descriptor
            _securityDescriptor = securityDescriptor;

            // create pipe
            _instance     = PipeInstance.Create(pipeName, true, _securityDescriptor);
            _instancePool = new PipeInstancePool(this);
        }
        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();
                }
            }
        }
		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();
				}
			}
		}
Exemple #8
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;
		}
Exemple #9
0
		internal Pipe(PipeInstance instance, Pipe parent)
		{
			_instance = instance;
			_parent = parent;
		}
Exemple #10
0
		/// <summary>
		/// Establishes a connection to a remote system
		/// </summary>
		public void Connect(PipeName pipeName)
		{
			// check object state
			if (_disposed)
				throw new ObjectDisposedException(GetType().FullName);
			if (_instance != null)
				throw new InvalidOperationException("Pipe is already connected");

			// connect to the remote host
			_instance = PipeInstance.Connect(pipeName);
		}
Exemple #11
0
		/// <summary>
		/// Creates pipe on the server side to allow client to connect to
		/// </summary>
		public void Bind(PipeName pipeName, SecurityDescriptor securityDescriptor)
		{
			// check object state
			if (_disposed)
				throw new ObjectDisposedException(GetType().FullName);
			if (_instance != null)
				throw new InvalidOperationException("Pipe is already connected");

			// store security descriptor
			_securityDescriptor = securityDescriptor;

			// create pipe
			_instance = PipeInstance.Create(pipeName, true, _securityDescriptor);
			_instancePool = new PipeInstancePool(this);
		}
Exemple #12
0
 internal Pipe(PipeInstance instance, Pipe parent)
 {
     _instance = instance;
     _parent   = parent;
 }