diff --git a/src/com/cgutman/adblib/AdbConnection.java b/src/com/cgutman/adblib/AdbConnection.java index bbdc2f5..63bdd74 100644 --- a/src/com/cgutman/adblib/AdbConnection.java +++ b/src/com/cgutman/adblib/AdbConnection.java @@ -135,12 +135,15 @@ public void run() { case AdbProtocol.CMD_OKAY: case AdbProtocol.CMD_WRTE: case AdbProtocol.CMD_CLSE: + { + AdbStream waitingStream; + /* We must ignore all packets when not connected */ if (!conn.connected) continue; /* Get the stream object corresponding to the packet */ - AdbStream waitingStream = openStreams.get(msg.arg1); + waitingStream = openStreams.get(msg.arg1); if (waitingStream == null) continue; @@ -164,16 +167,15 @@ else if (msg.command == AdbProtocol.CMD_WRTE) } else if (msg.command == AdbProtocol.CMD_CLSE) { - /* He doesn't like us anymore :-( */ - conn.openStreams.remove(msg.arg1); - /* Notify readers and writers */ waitingStream.notifyClose(); + + break; } } - break; - + continue; + } case AdbProtocol.CMD_AUTH: byte[] packet; @@ -199,7 +201,7 @@ else if (msg.command == AdbProtocol.CMD_CLSE) conn.outputStream.write(packet); conn.outputStream.flush(); } - break; + continue; case AdbProtocol.CMD_CNXN: synchronized (conn) { @@ -210,22 +212,20 @@ else if (msg.command == AdbProtocol.CMD_CLSE) conn.connected = true; conn.notifyAll(); } - break; - - default: - /* Unrecognized packet, just drop it */ - break; + continue; } + break; } catch (Exception e) { /* The cleanup is taken care of by a combination of this thread * and close() */ break; } } - + } + public void interrupt() { /* This thread takes care of cleaning up pending streams */ synchronized (conn) { - cleanupStreams(); + conn.cleanupStreams(); conn.notifyAll(); conn.connectAttempted = false; } diff --git a/src/com/cgutman/adblib/AdbStream.java b/src/com/cgutman/adblib/AdbStream.java index cf2c42b..0ae364a 100644 --- a/src/com/cgutman/adblib/AdbStream.java +++ b/src/com/cgutman/adblib/AdbStream.java @@ -116,11 +116,11 @@ public byte[] read() throws InterruptedException, IOException synchronized (readQueue) { /* Wait for the connection to close or data to be received */ - while (!isClosed && (data = readQueue.poll()) == null) { + while (!isClosed() && (data = readQueue.poll()) == null) { readQueue.wait(); } - if (isClosed) { + if (isClosed()) { throw new IOException("Stream closed"); } } @@ -163,10 +163,10 @@ public void write(byte[] payload, boolean flush) throws IOException, Interrupted { synchronized (this) { /* Make sure we're ready for a write */ - while (!isClosed && !writeReady.compareAndSet(true, false)) + while (!isClosed() && !writeReady.compareAndSet(true, false)) wait(); - if (isClosed) { + if (isClosed()) { throw new IOException("Stream closed"); } } @@ -187,7 +187,7 @@ public void write(byte[] payload, boolean flush) throws IOException, Interrupted public void close() throws IOException { synchronized (this) { /* This may already be closed by the remote host */ - if (isClosed) + if (isClosed()) return; /* Notify readers/writers that we've closed */ @@ -203,7 +203,7 @@ public void close() throws IOException { * Retreives whether the stream is closed or not * @return True if the stream is close, false if not */ - public boolean isClosed() { + public synchronized boolean isClosed() { return isClosed; } }