现在的位置: 首页 > 综合 > 正文

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

2013年01月01日 ⁄ 综合 ⁄ 共 2776字 ⁄ 字号 评论关闭

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

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

 

 

抱歉!评论已关闭.