您的位置:首页 > 移动开发 > Android开发

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

2013-06-24 11:32 686 查看
原始文件:frameworks\base\services\java\com\android\server\NativeDaemonConnector.java

private
void listenToSocket() throws IOException {

    LocalSocket socket = null;

 
    try {

        socket = new LocalSocket();

        LocalSocketAddress address = new LocalSocketAddress(mSocket,

                LocalSocketAddress.Namespace.RESERVED);

 
        socket.connect(address);

 
        InputStream inputStream = socket.getInputStream();

        mOutputStream = socket.getOutputStream();

 
        mCallbacks.onDaemonConnected();

 
        byte[] buffer = new
byte[BUFFER_SIZE];

        int start = 0;

 
        while (true) {

            int count = inputStream.read(buffer, start, BUFFER_SIZE - start);

            if (count < 0) break;

 
            // 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) {

                    String event = new String(buffer, start, i - start);

                    if (LOCAL_LOGD) Slog.d(TAG, String.format("RCV <- {%s}", event));

 
                    String[] tokens = event.split("
", 2);

                    try {

                        int code = Integer.parseInt(tokens[0]);

 
                        if (code >= ResponseCode.UnsolicitedInformational) {

                            mCallbackHandler.sendMessage(

                                    mCallbackHandler.obtainMessage(code, event));

                        } else {

                            try {

                                mResponseQueue.put(event);

                            } catch (InterruptedException ex) {

                                Slog.e(TAG, "Failed to put response onto queue", ex);

                            }

                        }

                    } catch (NumberFormatException nfe) {

                        Slog.w(TAG, String.format("Bad msg (%s)", event));

                    }

                    start = i + 1;

                }

            }

 
            // We should end at the amount we read. If not, compact then

            // 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) {

        Slog.e(TAG, "Communications error", ex);

        throw ex;

    } finally {

        synchronized (mDaemonLock) {

            if (mOutputStream != null) {

                try {

                    mOutputStream.close();

                } catch (IOException e) {

                    Slog.w(TAG, "Failed closing output stream", e);

                }

                mOutputStream = null;

            }

        }

 
        try {

            if (socket != null) {

                socket.close();

            }

        } catch (IOException ex) {

            Slog.w(TAG, "Failed closing socket", ex);

        }

    }

}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: