/// <summary> /// Helper method to open a named pipe via native APIs and return in /// .Net NamedPipeClientStream wrapper object. /// </summary> protected override NamedPipeClientStream DoConnect(int timeout) { // Create pipe flags. uint pipeFlags = NamedPipeNative.FILE_FLAG_OVERLAPPED; // // WaitNamedPipe API is not supported by Windows Server container now, so we need to repeatedly // attempt connection to pipe server until timeout expires. // int startTime = Environment.TickCount; int elapsedTime = 0; SafePipeHandle pipeHandle = null; do { // Get handle to pipe. pipeHandle = NamedPipeNative.CreateFile( _pipeName, NamedPipeNative.GENERIC_READ | NamedPipeNative.GENERIC_WRITE, 0, IntPtr.Zero, NamedPipeNative.OPEN_EXISTING, pipeFlags, IntPtr.Zero); int lastError = Marshal.GetLastWin32Error(); if (pipeHandle.IsInvalid) { if (lastError == NamedPipeNative.ERROR_FILE_NOT_FOUND) { elapsedTime = unchecked (Environment.TickCount - startTime); Thread.Sleep(100); continue; } else { throw new PSInvalidOperationException( StringUtil.Format(RemotingErrorIdStrings.CannotConnectContainerNamedPipe, lastError)); } } else { break; } } while (elapsedTime < timeout); try { return(new NamedPipeClientStream( PipeDirection.InOut, true, true, pipeHandle)); } catch (Exception) { pipeHandle.Dispose(); throw; } }
/// <summary> /// Helper method to open a named pipe via native APIs and return in /// .Net NamedPipeClientStream wrapper object. /// </summary> private NamedPipeClientStream OpenNamedPipe() { // Create pipe flags. uint pipeFlags = NamedPipeNative.FILE_FLAG_OVERLAPPED; // Get handle to pipe. SafePipeHandle pipeHandle = NamedPipeNative.CreateFile( _pipeName, NamedPipeNative.GENERIC_READ | NamedPipeNative.GENERIC_WRITE, 0, IntPtr.Zero, NamedPipeNative.OPEN_EXISTING, pipeFlags, IntPtr.Zero); int lastError = Marshal.GetLastWin32Error(); if (pipeHandle.IsInvalid) { throw new System.ComponentModel.Win32Exception(lastError); } try { return(new NamedPipeClientStream( PipeDirection.InOut, true, // IsAsync true, // IsConnected pipeHandle)); } catch (Exception e) { CommandProcessorBase.CheckForSevereException(e); pipeHandle.Dispose(); throw; } }