Android中java层使用LocalSocket和底层进行通讯

时间:2023-03-10 01:26:38
Android中java层使用LocalSocket和底层进行通讯

原始文件:frameworks\base\services\java\com\android\server\NativeDaemonConnector.java

  1. private
    void listenToSocket() throws IOException {
  2.     LocalSocket socket = null;
  3.     try {
  4.         socket = new LocalSocket();
  5.         LocalSocketAddress address = new LocalSocketAddress(mSocket,
  6.                 LocalSocketAddress.Namespace.RESERVED);
  7.         socket.connect(address);
  8.         InputStream inputStream = socket.getInputStream();
  9.         mOutputStream = socket.getOutputStream();
  10.         mCallbacks.onDaemonConnected();
  11.         byte[] buffer = new
    byte[BUFFER_SIZE];
  12.         int start = 0;
  13.         while (true) {
  14.             int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
  15.             if (count < 0) break;
  16.             // Add our starting point to the count and reset the start.
  17.             count += start;
  18.             start = 0;
  19.             for (int i = 0; i < count; i++) {
  20.                 if (buffer[i] == 0) {
  21.                     String event = new String(buffer, start, i - start);
  22.                     if (LOCAL_LOGD) Slog.d(TAG, String.format("RCV <- {%s}", event));
  23.                     String[] tokens = event.split("
    ", 2);
  24.                     try {
  25.                         int code = Integer.parseInt(tokens[0]);
  26.                         if (code >= ResponseCode.UnsolicitedInformational) {
  27.                             mCallbackHandler.sendMessage(
  28.                                     mCallbackHandler.obtainMessage(code, event));
  29.                         } else {
  30.                             try {
  31.                                 mResponseQueue.put(event);
  32.                             } catch (InterruptedException ex) {
  33.                                 Slog.e(TAG, "Failed to put response onto queue", ex);
  34.                             }
  35.                         }
  36.                     } catch (NumberFormatException nfe) {
  37.                         Slog.w(TAG, String.format("Bad msg (%s)", event));
  38.                     }
  39.                     start = i + 1;
  40.                 }
  41.             }
  42.             // We should end at the amount we read. If not, compact then
  43.             // buffer and read again.
  44.             if (start != count) {
  45.                 final
    int remaining = BUFFER_SIZE - start;
  46.                 System.arraycopy(buffer, start, buffer, 0, remaining);
  47.                 start = remaining;
  48.             } else {
  49.                 start = 0;
  50.             }
  51.         }
  52.     } catch (IOException ex) {
  53.         Slog.e(TAG, "Communications error", ex);
  54.         throw ex;
  55.     } finally {
  56.         synchronized (mDaemonLock) {
  57.             if (mOutputStream != null) {
  58.                 try {
  59.                     mOutputStream.close();
  60.                 } catch (IOException e) {
  61.                     Slog.w(TAG, "Failed closing output stream", e);
  62.                 }
  63.                 mOutputStream = null;
  64.             }
  65.         }
  66.         try {
  67.             if (socket != null) {
  68.                 socket.close();
  69.             }
  70.         } catch (IOException ex) {
  71.             Slog.w(TAG, "Failed closing socket", ex);
  72.         }
  73.     }
  74. }