예제 #1
0
		public static PipeInstance Connect(PipeName pipeName)
		{
			// parameters validation
			if (pipeName == null)
				throw new ArgumentNullException("pipeName", "Pipe name must be specified");

			while (true)
			{
				// try to connect to the pipe
				IntPtr handle = PipeNative.CreateFile(pipeName.ToString(),
					PipeNative.GENERIC_READ | PipeNative.GENERIC_WRITE,
					PipeNative.FILE_SHARE_NONE,
					null,
					PipeNative.OPEN_EXISTING,
					PipeNative.FILE_FLAG_OVERLAPPED,
					IntPtr.Zero);

				if (handle == Win32.InvalidHandle)
				{
					int errorCode = Marshal.GetLastWin32Error();
					if (errorCode != PipeNative.ERROR_PIPE_BUSY)
					{
						throw new PipeIOException(errorCode, "Could not open pipe: " + pipeName);
					}
					if (!PipeNative.WaitNamedPipe(pipeName.ToString(), PipeNative.NMPWAIT_USE_DEFAULT_WAIT))
					{
						throw new PipeIOException(errorCode, "Specified pipe was over-burdened: " + pipeName);
					}
				}
				else
				{
					return new PipeInstance(pipeName, handle, true);
				}
			}
		}
예제 #2
0
        public static PipeInstance Create(PipeName pipeName, bool firstInstance, SecurityDescriptor securityDescriptor)
        {
            // parameters validation
            if (pipeName == null)
            {
                throw new ArgumentNullException("pipeName", "Pipe name must be specified");
            }
            if (!pipeName.IsLocal)
            {
                throw new ArgumentException("Could not bind to the remote pipe");
            }

            PipeNative.SecurityAttributes secAttrs = new PipeNative.SecurityAttributes();
            secAttrs.SecurityDescriptor = (securityDescriptor == null ? IntPtr.Zero : securityDescriptor.Handle);
            secAttrs.InheritHandle      = true;

            // try to create pipe
            IntPtr handle = PipeNative.CreateNamedPipe(pipeName.ToString(),
                                                       PipeNative.PIPE_ACCESS_DUPLEX | PipeNative.FILE_FLAG_OVERLAPPED | (firstInstance ? PipeNative.FILE_FLAG_FIRST_PIPE_INSTANCE : 0),
                                                       PipeNative.PIPE_TYPE_BYTE | PipeNative.PIPE_READMODE_BYTE | PipeNative.PIPE_WAIT,
                                                       PipeNative.PIPE_UNLIMITED_INSTANCES,
                                                       OutBufferSize,
                                                       InBufferSize,
                                                       PipeNative.NMPWAIT_USE_DEFAULT_WAIT,
                                                       secAttrs);

            if (handle == Win32.InvalidHandle)
            {
                throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not create pipe: " + pipeName);
            }
            else
            {
                return(new PipeInstance(pipeName, handle, false));
            }
        }
예제 #3
0
		public PipeInstance(PipeName pipeName, IntPtr handle, bool isConnected)
		{
			// parameters validation
			if (pipeName == null)
				throw new ArgumentNullException("pipeName");
			if (handle == Win32.InvalidHandle)
				throw new ArgumentException("Invalid pipe handle", "handle");

			_pipeName = pipeName;
			_handle = new NativeHandle(handle);
			_isConnected = isConnected;
		}
예제 #4
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);
        }
예제 #5
0
        public PipeInstance(PipeName pipeName, IntPtr handle, bool isConnected)
        {
            // parameters validation
            if (pipeName == null)
            {
                throw new ArgumentNullException("pipeName");
            }
            if (handle == Win32.InvalidHandle)
            {
                throw new ArgumentException("Invalid pipe handle", "handle");
            }

            _pipeName    = pipeName;
            _handle      = new NativeHandle(handle);
            _isConnected = isConnected;
        }
예제 #6
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);
        }
예제 #7
0
        public static PipeInstance Connect(PipeName pipeName)
        {
            // parameters validation
            if (pipeName == null)
            {
                throw new ArgumentNullException("pipeName", "Pipe name must be specified");
            }

            while (true)
            {
                // try to connect to the pipe
                IntPtr handle = PipeNative.CreateFile(pipeName.ToString(),
                                                      PipeNative.GENERIC_READ | PipeNative.GENERIC_WRITE,
                                                      PipeNative.FILE_SHARE_NONE,
                                                      null,
                                                      PipeNative.OPEN_EXISTING,
                                                      PipeNative.FILE_FLAG_OVERLAPPED,
                                                      IntPtr.Zero);

                if (handle == Win32.InvalidHandle)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    if (errorCode != PipeNative.ERROR_PIPE_BUSY)
                    {
                        throw new PipeIOException(errorCode, "Could not open pipe: " + pipeName);
                    }
                    if (!PipeNative.WaitNamedPipe(pipeName.ToString(), PipeNative.NMPWAIT_USE_DEFAULT_WAIT))
                    {
                        throw new PipeIOException(errorCode, "Specified pipe was over-burdened: " + pipeName);
                    }
                }
                else
                {
                    return(new PipeInstance(pipeName, handle, true));
                }
            }
        }
예제 #8
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);
		}
예제 #9
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);
		}
예제 #10
0
		public static PipeInstance Create(PipeName pipeName, bool firstInstance, SecurityDescriptor securityDescriptor)
		{
			// parameters validation
			if (pipeName == null)
				throw new ArgumentNullException("pipeName", "Pipe name must be specified");
			if (!pipeName.IsLocal)
				throw new ArgumentException("Could not bind to the remote pipe");

			PipeNative.SecurityAttributes secAttrs = new PipeNative.SecurityAttributes();
			secAttrs.SecurityDescriptor = (securityDescriptor == null ? IntPtr.Zero : securityDescriptor.Handle);
			secAttrs.InheritHandle = true;

			// try to create pipe
			IntPtr handle = PipeNative.CreateNamedPipe(pipeName.ToString(),
				PipeNative.PIPE_ACCESS_DUPLEX | PipeNative.FILE_FLAG_OVERLAPPED | (firstInstance ? PipeNative.FILE_FLAG_FIRST_PIPE_INSTANCE : 0),
				PipeNative.PIPE_TYPE_BYTE | PipeNative.PIPE_READMODE_BYTE | PipeNative.PIPE_WAIT,
				PipeNative.PIPE_UNLIMITED_INSTANCES,
				OutBufferSize,
				InBufferSize,
				PipeNative.NMPWAIT_USE_DEFAULT_WAIT,
				secAttrs);

			if (handle == Win32.InvalidHandle)
			{
				throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not create pipe: " + pipeName);
			}
			else
			{
				return new PipeInstance(pipeName, handle, false);
			}
		}
예제 #11
0
		private void Initialize(IDictionary properties, IServerChannelSinkProvider sinkProvider)
		{
			if (properties != null)
			{
				// read property values
				foreach (DictionaryEntry property in properties)
				{
					switch ((string)property.Key)
					{
						case "name": _channelName = Convert.ToString(property.Value); break;
						case "priority": _channelPriority = Convert.ToInt32(property.Value); break;
						case "pipe": _pipe = Convert.ToString(property.Value); break;
						case "securityDescriptor": _securityDescriptor = (property.Value as SecurityDescriptor); break;
					}
				}
			}

			// setup pipe name
			_pipeName = new PipeName(@"\\.\pipe\" + _pipe);

			// create the chain of the sink providers that will process all messages
			_sinkProvider = ChannelHelper.ServerChannelCreateSinkProviderChain(sinkProvider);
			_channelData = ChannelHelper.ServerChannelCreateDataStore(ChannelUri, _sinkProvider);

			// create transport sink
			IServerChannelSink nextSink = ChannelServices.CreateServerChannelSinkChain(_sinkProvider, this);
			_transportSink = new ServerTransportSink(nextSink);

			// create listener thread
			_transportListener = new TransportListener(ChannelUri, typeof(PipeTransport));
			_listenerThread = new Thread(new ThreadStart(ListenerStart));
			_listenerThread.IsBackground = true;

			_requestHandler = new ProcessRequestCallback(_transportSink.ProcessRequest);

			// start listening on the channel
			StartListening(null);
		}
예제 #12
0
		private static PipeName GetPipeName(string url)
		{
			IDictionary parts = null;
			if (!ParseUrl(url, out parts))
				throw new ArgumentException("Invalid pipe address specified");

			PipeName pipeName = new PipeName((string)parts["host"], (string)parts["pipe"]);
			return pipeName;
		}