Beispiel #1
0
    public void close() {
      synchronized (JTermiosImpl.this) {
        if (m_FD >= 0) {
          m_OpenPorts.remove(m_FD);
          m_PortFDs[m_FD] = false;
          m_FD = -1;
        }

        if (m_CancelWaitSema4 != null) SetEvent(m_CancelWaitSema4);
        if (m_Comm != null) {
          ResetEvent(m_SelOVL.hEvent);

          if (!CancelIo(m_Comm))
            log =
                log
                    && log(
                        1,
                        "CancelIo() failed, GetLastError()= %d, %s\n",
                        GetLastError(),
                        lineno(1));
          if (!PurgeComm(m_Comm, PURGE_TXABORT + PURGE_TXCLEAR + PURGE_RXABORT + PURGE_RXCLEAR))
            log =
                log
                    && log(
                        1,
                        "PurgeComm() failed, GetLastError()= %d, %s\n",
                        GetLastError(),
                        lineno(1));
        }
        HANDLE h; // / 'hEvent' might never have been 'read' so read it
        // to this var first

        synchronized (m_RdBuffer) {
          h = (HANDLE) m_RdOVL.readField("hEvent");
          m_RdOVL = null;
          if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);
        }

        synchronized (m_WrBuffer) {
          h = (HANDLE) m_WrOVL.readField("hEvent");
          m_WrOVL = null;

          if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);
        }

        // Ensure that select() is through before releasing the m_SelOVL
        waitUnlock();

        h = (HANDLE) m_SelOVL.readField("hEvent");
        m_SelOVL = null;
        if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);

        if (m_Comm != null && m_Comm != NULL && m_Comm != INVALID_HANDLE_VALUE) CloseHandle(m_Comm);
        m_Comm = null;
      }
    }