diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2022-05-12 17:21:37 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2022-05-12 17:21:37 +0000 |
commit | 821acc6177c2c6459d223e0a6ecef7f3d24d8876 (patch) | |
tree | 41f6366d428821fb33bf87e6068ddd0c94140a01 | |
parent | e07281f0792fa72b49584b115de18ab20a9df6c8 (diff) | |
parent | a1e0480b6400cf8c758f6035bcab351ddf7a67f4 (diff) | |
download | base-821acc6177c2c6459d223e0a6ecef7f3d24d8876.tar.gz |
Merge changes I5241086c,I3e96ab6a
* changes:
Cleanup in EmulatorClipboardMonitor (2)
Cleanup in EmulatorClipboardMonitor (1)
-rw-r--r-- | services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java index c37d4c6bcaef..44822a3f7429 100644 --- a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java +++ b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java @@ -60,11 +60,11 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { return mPipe; } - private synchronized boolean openPipe() { - if (mPipe != null) { - return true; - } + private synchronized void setPipeFD(final FileDescriptor fd) { + mPipe = fd; + } + private static FileDescriptor openPipeImpl() { try { final FileDescriptor fd = Os.socket(OsConstants.AF_VSOCK, OsConstants.SOCK_STREAM, 0); @@ -72,16 +72,33 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { Os.connect(fd, new VmSocketAddress(HOST_PORT, OsConstants.VMADDR_CID_HOST)); final byte[] handshake = createOpenHandshake(); - Os.write(fd, handshake, 0, handshake.length); - mPipe = fd; - return true; + writeFully(fd, handshake, 0, handshake.length); + return fd; } catch (ErrnoException | SocketException | InterruptedIOException e) { Os.close(fd); } } catch (ErrnoException e) { } - return false; + return null; + } + + private void openPipe() throws InterruptedException { + FileDescriptor fd = getPipeFD(); + + if (fd == null) { + fd = openPipeImpl(); + + // There's no guarantee that QEMU pipes will be ready at the moment + // this method is invoked. We simply try to get the pipe open and + // retry on failure indefinitely. + while (fd == null) { + Thread.sleep(100); + fd = openPipeImpl(); + } + } + + setPipeFD(fd); } private synchronized void closePipe() { @@ -125,12 +142,7 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { this.mHostMonitorThread = new Thread(() -> { while (!Thread.interrupted()) { try { - // There's no guarantee that QEMU pipes will be ready at the moment - // this method is invoked. We simply try to get the pipe open and - // retry on failure indefinitely. - while (!openPipe()) { - Thread.sleep(100); - } + openPipe(); final byte[] receivedData = receiveMessage(); |