Search in sources :

Example 1 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ParanoidAndroid.

the class UsbDebuggingManager method listenToSocket.

private void listenToSocket() throws IOException {
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        LocalSocketAddress address = new LocalSocketAddress(ADBD_SOCKET, LocalSocketAddress.Namespace.RESERVED);
        InputStream inputStream = null;
        mSocket = new LocalSocket();
        mSocket.connect(address);
        mOutputStream = mSocket.getOutputStream();
        inputStream = mSocket.getInputStream();
        while (true) {
            int count = inputStream.read(buffer);
            if (count < 0) {
                Slog.e(TAG, "got " + count + " reading");
                break;
            }
            if (buffer[0] == 'P' && buffer[1] == 'K') {
                String key = new String(Arrays.copyOfRange(buffer, 2, count));
                Slog.d(TAG, "Received public key: " + key);
                Message msg = mHandler.obtainMessage(UsbDebuggingHandler.MESSAGE_ADB_CONFIRM);
                msg.obj = key;
                mHandler.sendMessage(msg);
            } else {
                Slog.e(TAG, "Wrong message: " + (new String(Arrays.copyOfRange(buffer, 0, 2))));
                break;
            }
        }
    } catch (IOException ex) {
        Slog.e(TAG, "Communication error: ", ex);
        throw ex;
    } finally {
        closeSocket();
    }
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Example 2 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ParanoidAndroid.

the class Process method openZygoteSocketIfNeeded.

/**
     * Tries to open socket to Zygote process if not already open. If
     * already open, does nothing.  May block and retry.
     */
private static void openZygoteSocketIfNeeded() throws ZygoteStartFailedEx {
    int retryCount;
    if (sPreviousZygoteOpenFailed) {
        /*
             * If we've failed before, expect that we'll fail again and
             * don't pause for retries.
             */
        retryCount = 0;
    } else {
        retryCount = 10;
    }
    /*
         * See bug #811181: Sometimes runtime can make it up before zygote.
         * Really, we'd like to do something better to avoid this condition,
         * but for now just wait a bit...
         */
    for (int retry = 0; (sZygoteSocket == null) && (retry < (retryCount + 1)); retry++) {
        if (retry > 0) {
            try {
                Log.i("Zygote", "Zygote not up yet, sleeping...");
                Thread.sleep(ZYGOTE_RETRY_MILLIS);
            } catch (InterruptedException ex) {
            // should never happen
            }
        }
        try {
            sZygoteSocket = new LocalSocket();
            sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, LocalSocketAddress.Namespace.RESERVED));
            sZygoteInputStream = new DataInputStream(sZygoteSocket.getInputStream());
            sZygoteWriter = new BufferedWriter(new OutputStreamWriter(sZygoteSocket.getOutputStream()), 256);
            Log.i("Zygote", "Process: zygote socket opened");
            sPreviousZygoteOpenFailed = false;
            break;
        } catch (IOException ex) {
            if (sZygoteSocket != null) {
                try {
                    sZygoteSocket.close();
                } catch (IOException ex2) {
                    Log.e(LOG_TAG, "I/O exception on close after exception", ex2);
                }
            }
            sZygoteSocket = null;
        }
    }
    if (sZygoteSocket == null) {
        sPreviousZygoteOpenFailed = true;
        throw new ZygoteStartFailedEx("connect failed");
    }
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedWriter(java.io.BufferedWriter)

Example 3 with LocalSocketAddress

use of android.net.LocalSocketAddress in project XobotOS by xamarin.

the class Process method openZygoteSocketIfNeeded.

/**
     * Tries to open socket to Zygote process if not already open. If
     * already open, does nothing.  May block and retry.
     */
private static void openZygoteSocketIfNeeded() throws ZygoteStartFailedEx {
    int retryCount;
    if (sPreviousZygoteOpenFailed) {
        /*
             * If we've failed before, expect that we'll fail again and
             * don't pause for retries.
             */
        retryCount = 0;
    } else {
        retryCount = 10;
    }
    /*
         * See bug #811181: Sometimes runtime can make it up before zygote.
         * Really, we'd like to do something better to avoid this condition,
         * but for now just wait a bit...
         */
    for (int retry = 0; (sZygoteSocket == null) && (retry < (retryCount + 1)); retry++) {
        if (retry > 0) {
            try {
                Log.i("Zygote", "Zygote not up yet, sleeping...");
                Thread.sleep(ZYGOTE_RETRY_MILLIS);
            } catch (InterruptedException ex) {
            // should never happen
            }
        }
        try {
            sZygoteSocket = new LocalSocket();
            sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, LocalSocketAddress.Namespace.RESERVED));
            sZygoteInputStream = new DataInputStream(sZygoteSocket.getInputStream());
            sZygoteWriter = new BufferedWriter(new OutputStreamWriter(sZygoteSocket.getOutputStream()), 256);
            Log.i("Zygote", "Process: zygote socket opened");
            sPreviousZygoteOpenFailed = false;
            break;
        } catch (IOException ex) {
            if (sZygoteSocket != null) {
                try {
                    sZygoteSocket.close();
                } catch (IOException ex2) {
                    Log.e(LOG_TAG, "I/O exception on close after exception", ex2);
                }
            }
            sZygoteSocket = null;
        }
    }
    if (sZygoteSocket == null) {
        sPreviousZygoteOpenFailed = true;
        throw new ZygoteStartFailedEx("connect failed");
    }
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedWriter(java.io.BufferedWriter)

Example 4 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ResurrectionRemix.

the class NativeDaemonConnector method listenToSocket.

private void listenToSocket() throws IOException {
    LocalSocket socket = null;
    try {
        socket = new LocalSocket();
        LocalSocketAddress address = determineSocketAddress();
        socket.connect(address);
        InputStream inputStream = socket.getInputStream();
        synchronized (mDaemonLock) {
            mOutputStream = socket.getOutputStream();
        }
        mCallbacks.onDaemonConnected();
        FileDescriptor[] fdList = null;
        byte[] buffer = new byte[BUFFER_SIZE];
        int start = 0;
        while (true) {
            int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
            if (count < 0) {
                loge("got " + count + " reading with start = " + start);
                break;
            }
            fdList = socket.getAncillaryFileDescriptors();
            // Add our starting point to the count and reset the start.
            count += start;
            start = 0;
            for (int i = 0; i < count; i++) {
                if (buffer[i] == 0) {
                    // Note - do not log this raw message since it may contain
                    // sensitive data
                    final String rawEvent = new String(buffer, start, i - start, StandardCharsets.UTF_8);
                    boolean releaseWl = false;
                    try {
                        final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(rawEvent, fdList);
                        log("RCV <- {" + event + "}");
                        if (event.isClassUnsolicited()) {
                            // TODO: migrate to sending NativeDaemonEvent instances
                            if (mCallbacks.onCheckHoldWakeLock(event.getCode()) && mWakeLock != null) {
                                mWakeLock.acquire();
                                releaseWl = true;
                            }
                            Message msg = mCallbackHandler.obtainMessage(event.getCode(), uptimeMillisInt(), 0, event.getRawEvent());
                            if (mCallbackHandler.sendMessage(msg)) {
                                releaseWl = false;
                            }
                        } else {
                            mResponseQueue.add(event.getCmdNumber(), event);
                        }
                    } catch (IllegalArgumentException e) {
                        log("Problem parsing message " + e);
                    } finally {
                        if (releaseWl) {
                            mWakeLock.release();
                        }
                    }
                    start = i + 1;
                }
            }
            if (start == 0) {
                log("RCV incomplete");
            }
            // buffer and read again.
            if (start != count) {
                final int remaining = BUFFER_SIZE - start;
                System.arraycopy(buffer, start, buffer, 0, remaining);
                start = remaining;
            } else {
                start = 0;
            }
        }
    } catch (IOException ex) {
        loge("Communications error: " + ex);
        throw ex;
    } finally {
        synchronized (mDaemonLock) {
            if (mOutputStream != null) {
                try {
                    loge("closing stream for " + mSocket);
                    mOutputStream.close();
                } catch (IOException e) {
                    loge("Failed closing output stream: " + e);
                }
                mOutputStream = null;
            }
        }
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ex) {
            loge("Failed closing socket: " + ex);
        }
    }
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 5 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ResurrectionRemix.

the class InstallerConnection method connect.

private boolean connect() {
    if (mSocket != null) {
        return true;
    }
    Slog.i(TAG, "connecting...");
    try {
        mSocket = new LocalSocket();
        LocalSocketAddress address = new LocalSocketAddress("installd", LocalSocketAddress.Namespace.RESERVED);
        mSocket.connect(address);
        mIn = mSocket.getInputStream();
        mOut = mSocket.getOutputStream();
    } catch (IOException ex) {
        disconnect();
        return false;
    }
    return true;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Aggregations

LocalSocket (android.net.LocalSocket)33 LocalSocketAddress (android.net.LocalSocketAddress)33 IOException (java.io.IOException)27 LocalServerSocket (android.net.LocalServerSocket)12 SmallTest (android.test.suitebuilder.annotation.SmallTest)11 FileDescriptor (java.io.FileDescriptor)11 InputStream (java.io.InputStream)7 Credentials (android.net.Credentials)6 Message (android.os.Message)6 SocketException (java.net.SocketException)5 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SuppressLint (android.annotation.SuppressLint)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 Method (java.lang.reflect.Method)1 Random (java.util.Random)1