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

原始文件: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. }

 

 

转载于:https://www.cnblogs.com/jevan/p/3152160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值