Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 234b3363 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Cleanup in EmulatorClipboardMonitor (2)" into sc-v2-dev

parents fd5ed2cf 185fd896
Loading
Loading
Loading
Loading
+25 −13
Original line number Diff line number Diff line
@@ -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();