diff options
| author | 2022-05-13 05:36:43 +0000 | |
|---|---|---|
| committer | 2022-05-13 05:36:43 +0000 | |
| commit | 234b3363fe03373405b9643ef0fbfd1abd771aec (patch) | |
| tree | 26192205879e591c00da6d9c1e180a2f9a2f2c40 | |
| parent | fd5ed2cfd6248193b2ee8f2e90fffc3c77bc3ae4 (diff) | |
| parent | 185fd89665b67ef68d64887c65db693cb922f951 (diff) | |
Merge "Cleanup in EmulatorClipboardMonitor (2)" into sc-v2-dev
| -rw-r--r-- | services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java index 14259df9879c..0ccb4554c057 100644 --- a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java +++ b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java @@ -59,11 +59,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,15 +72,32 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { final byte[] handshake = createOpenHandshake(); writeFully(fd, handshake, 0, handshake.length); - mPipe = fd; - return true; + 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() { @@ -124,12 +141,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(); |