private static void waitForPipe()

in src/com/pty4j/windows/cygwin/CygwinPtyProcess.java [124:153]


  private static void waitForPipe(WinNT.HANDLE handle) throws IOException {
    WinNT.HANDLE connectEvent = CygwinKernel32.INSTANCE.CreateEventA(null, true, false, null);

    WinBase.OVERLAPPED povl = new WinBase.OVERLAPPED();
    povl.hEvent = connectEvent;

    boolean success = Kernel32.INSTANCE.ConnectNamedPipe(handle, povl);
    if (!success) {
      switch (Kernel32.INSTANCE.GetLastError()) {
        case WinError.ERROR_PIPE_CONNECTED:
          success = true;
          break;
        case WinError.ERROR_IO_PENDING:
          if (Kernel32.INSTANCE.WaitForSingleObject(connectEvent, CONNECT_PIPE_TIMEOUT) != WinBase.WAIT_OBJECT_0) {
            CygwinKernel32.INSTANCE.CancelIo(handle);

            success = false;
          }
          else {
            success = true;
          }

          break;
      }
    }

    Kernel32.INSTANCE.CloseHandle(connectEvent);

    if (!success) throw new IOException("Cannot connect to a named pipe");
  }