Ejemplo n.º 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);
				}
			}
		}
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 3
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));
                }
            }
        }
Ejemplo n.º 4
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);
			}
		}