AIDL 如何分片传输大量 Parcelable 数据列表

2023-11-24 14:01

本文主要是介绍AIDL 如何分片传输大量 Parcelable 数据列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文针对 AIDL 跨进程传输大量 Parcelable 数据所产生的问题总结出一套分片传输的解决方案,并分析了一下其实现的原理。

1. 概述

大家在通过 AIDL 实现跨进程数据传输的时候,可能会遇到数据量过大导致异常的情况,通常抛出的异常如下:

E/BpBinderRecord: Too many binder proxy objects sent to pid xxx from pid xxx (2500 proxies held)
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 96)
android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died

下面就来讲述一个分片传输的处理方案,来解决这类数据过大的问题。

2. 问题模拟

首先让我们写个 demo 来模拟一下大数据传输的场景。
先创建一个 AIDL 文件,并定义一个返回数据列表的接口:

import android.app.Notification;interface IAIDLTest {List<Notification> getNotifications();
}

这里使用的 Notification 是 Android 提供的一个实现 Parcelable 接口的实体类,因为其内容比较复杂,所以我们用这个类来进行测试。
然后创建一个实现这个 AIDL 接口的 Service,代码如下:

public class AIDLTestService extends Service {private final IBinder mService = new IAIDLTest.Stub() {@Overridepublic List<Notification> getNotifications() {List<Notification> notifications = new ArrayList<>();for (int i = 0; i < 50; i++) { // 先构建 50 条数据看效果,后面再作增加notifications.add(new Notification.Builder(AIDLTestService.this,"CHANNEL_ID").setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(Icon.createWithResource(AIDLTestService.this,R.drawable.ic_avatar)).setContentTitle("标题文本" + i).setContentText("正文文本" + i).setContentIntent(NotificationUtils.createActivityPendingIntent(AIDLTestService.this, "AIDL 测试" + i)).setAutoCancel(true).addAction(0, "按钮 1",NotificationUtils.createActivityPendingIntent(AIDLTestService.this, "按钮 1" + i)).addAction(0, "按钮 2",NotificationUtils.createReceiverPendingIntent(AIDLTestService.this, "按钮 2" + i)).build());}return notifications;}};@Overridepublic IBinder onBind(Intent intent) {return mService;}
}

这里先构建 50 条数据看下正常传输的效果,后面再模拟过大的情况。
接下来在 AndroidManifest.xml 文件里定义这个 Service:

<serviceandroid:name=".AIDLTestService"android:enabled="true"android:exported="true"android:process=":remote" />

因为要测试跨进程传输,所以这个 Service 定义在另一个 remote 进程中。
接着实现一个 Activity 绑定这个 Service,跨进程取到数据后进行打印:

public class AIDLTestActivity extends AppCompatActivity implements ServiceConnection {private static final String TAG = "AIDLTestActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_aidl_test);bindService(new Intent(this, AIDLTestService.class), this, BIND_AUTO_CREATE);}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {IAIDLTest aidlTest = IAIDLTest.Stub.asInterface(service);try {for (Notification notification : aidlTest.getNotifications()) {Log.d(TAG, "onServiceConnected: notification title = "+ notification.extras.get(Notification.EXTRA_TITLE));}} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {}
}

运行后打印的结果部分截图如下:

打印结果

现在把数据量改大,改成 500 条运行试试:

报错啦

Boom!!!可以看到出现报错的情况了。
下面来看一下该如何解决。

3. 解决方案

熟悉 framework 的同学应该知道 framework 里经常会有 AIDL 跨进程的操作,那么里面肯定会考虑这种大量数据传输场景的,因此我们看一看 framework 里是如何处理的。
例如 framework 中 PackageManagerServicegetInstalledApplications() 方法定义:

PackageManagerService.java

我们发现其使用了一个 ParceledListSlice 类做数据分片的,看下这个类的定义:

ParceledListSlice.java

完整代码见:https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/content/pm/ParceledListSlice.java
看注释这个类是用来 IPC 时对大量 Parcelable 对象传输使用的,但这个类是不支持 App 直接使用的,那么我们将其复制出来一份使用。
它继承了父类 BaseParceledListSlice,这个类有多个子类可以支持多种场景,但我们暂不需要,因此我将这两个类的代码合到了一起,并处理了一些运行时报错的问题(具体的报错问题这里不做展开),最终处理完的类的完整代码如下:

/*** Transfer a large list of Parcelable objects across an IPC.  Splits into* multiple transactions if needed.* <p>* Caveat: for efficiency and security, all elements must be the same concrete type.* In order to avoid writing the class name of each object, we must ensure that* each object is the same type, or else unparceling then reparceling the data may yield* a different result if the class name encoded in the Parcelable is a Base type.*/
public class ParceledListSlice<T extends Parcelable> implements Parcelable {private static final String TAG = "NotificationParceledListSlice";private static final boolean DEBUG = false;private static final int MAX_IPC_SIZE = IBinder.getSuggestedMaxIpcSizeBytes();private final List<T> mList;private int mInlineCountLimit = Integer.MAX_VALUE;public ParceledListSlice(List<T> list) {mList = list;}private ParceledListSlice(Parcel p, ClassLoader loader) {final int N = p.readInt();mList = new ArrayList<T>(N);if (DEBUG) Log.d(TAG, "Retrieving " + N + " items");if (N <= 0) {return;}Creator<?> creator = readParcelableCreator(p, loader);Class<?> listElementClass = null;int i = 0;while (i < N) {if (p.readInt() == 0) {break;}listElementClass = readVerifyAndAddElement(creator, p, loader, listElementClass);if (DEBUG) Log.d(TAG, "Read inline #" + i + ": " + mList.get(mList.size() - 1));i++;}if (i >= N) {return;}final IBinder retriever = p.readStrongBinder();while (i < N) {if (DEBUG) Log.d(TAG, "Reading more @" + i + " of " + N + ": retriever=" + retriever);Parcel data = Parcel.obtain();Parcel reply = Parcel.obtain();data.writeInt(i);try {retriever.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);} catch (RemoteException e) {Log.w(TAG, "Failure retrieving array; only received " + i + " of " + N, e);return;}while (i < N && reply.readInt() != 0) {listElementClass = readVerifyAndAddElement(creator, reply, loader,listElementClass);if (DEBUG) Log.d(TAG, "Read extra #" + i + ": " + mList.get(mList.size() - 1));i++;}reply.recycle();data.recycle();}}private Class<?> readVerifyAndAddElement(Creator<?> creator, Parcel p,ClassLoader loader, Class<?> listElementClass) {final T parcelable = readCreator(creator, p, loader);if (listElementClass == null) {listElementClass = parcelable.getClass();} else {verifySameType(listElementClass, parcelable.getClass());}mList.add(parcelable);return listElementClass;}@SuppressWarnings("unchecked")private T readCreator(Creator<?> creator, Parcel p, ClassLoader loader) {if (creator instanceof ClassLoaderCreator<?>) {ClassLoaderCreator<?> classLoaderCreator =(ClassLoaderCreator<?>) creator;return (T) classLoaderCreator.createFromParcel(p, loader);}return (T) creator.createFromParcel(p);}private static void verifySameType(final Class<?> expected, final Class<?> actual) {if (!actual.equals(expected)) {throw new IllegalArgumentException("Can't unparcel type "+ actual.getName() + " in list of type "+ (expected == null ? null : expected.getName()));}}public List<T> getList() {return mList;}/*** Set a limit on the maximum number of entries in the array that will be included* inline in the initial parcelling of this object.*/public void setInlineCountLimit(int maxCount) {mInlineCountLimit = maxCount;}@Overridepublic int describeContents() {int contents = 0;final List<T> list = getList();for (int i = 0; i < list.size(); i++) {contents |= list.get(i).describeContents();}return contents;}/*** Write this to another Parcel. Note that this discards the internal Parcel* and should not be used anymore. This is so we can pass this to a Binder* where we won't have a chance to call recycle on this.*/@Overridepublic void writeToParcel(Parcel dest, int flags) {final int N = mList.size();final int callFlags = flags;dest.writeInt(N);if (DEBUG) Log.d(TAG, "Writing " + N + " items");if (N > 0) {final Class<?> listElementClass = mList.get(0).getClass();writeParcelableCreator(mList.get(0), dest);int i = 0;while (i < N && i < mInlineCountLimit && dest.dataSize() < MAX_IPC_SIZE) {dest.writeInt(1);final T parcelable = mList.get(i);verifySameType(listElementClass, parcelable.getClass());writeElement(parcelable, dest, callFlags);if (DEBUG) Log.d(TAG, "Wrote inline #" + i + ": " + mList.get(i));i++;}if (i < N) {dest.writeInt(0);Binder retriever = new Binder() {@Overrideprotected boolean onTransact(int code, Parcel data, Parcel reply, int flags)throws RemoteException {if (code != FIRST_CALL_TRANSACTION) {return super.onTransact(code, data, reply, flags);}int i = data.readInt();if (DEBUG) Log.d(TAG, "Writing more @" + i + " of " + N);while (i < N && reply.dataSize() < MAX_IPC_SIZE) {reply.writeInt(1);final T parcelable = mList.get(i);verifySameType(listElementClass, parcelable.getClass());writeElement(parcelable, reply, callFlags);if (DEBUG) Log.d(TAG, "Wrote extra #" + i + ": " + mList.get(i));i++;}if (i < N) {if (DEBUG) Log.d(TAG, "Breaking @" + i + " of " + N);reply.writeInt(0);}return true;}};if (DEBUG) Log.d(TAG, "Breaking @" + i + " of " + N + ": retriever=" + retriever);dest.writeStrongBinder(retriever);}}}protected void writeElement(T parcelable, Parcel reply, int callFlags) {parcelable.writeToParcel(reply, callFlags);}protected void writeParcelableCreator(T parcelable, Parcel dest) {dest.writeParcelableCreator(parcelable);}protected Creator<?> readParcelableCreator(Parcel from, ClassLoader loader) {return from.readParcelableCreator(loader);}@SuppressWarnings("unchecked")public static final ClassLoaderCreator<ParceledListSlice> CREATOR= new ClassLoaderCreator<ParceledListSlice>() {@Overridepublic ParceledListSlice createFromParcel(Parcel in) {return new ParceledListSlice(in, getClass().getClassLoader());}@Overridepublic ParceledListSlice createFromParcel(Parcel in, ClassLoader loader) {return new ParceledListSlice(in, loader);}@Overridepublic ParceledListSlice[] newArray(int size) {return new ParceledListSlice[size];}};
}

别忘了加一下对应的 ParceledListSlice.aidl 文件:

package com.jimmysun.notificationdemo;parcelable NotificationParceledListSlice<T>;

现在来修改一下我们 AIDL 接口文件:

import android.app.Notification;
import com.jimmysun.notificationdemo.ParceledListSlice;interface IAIDLTest {ParceledListSlice<Notification> getNotifications();
}

然后在我们 Service 里的代码修改如下:

private final IBinder mService = new IAIDLTest.Stub() {@Overridepublic ParceledListSlice<Notification> getNotifications() {List<Notification> notifications = new ArrayList<>();for (int i = 0; i < 500; i++) {notifications.add(new Notification.Builder(AIDLTestService.this,"CHANNEL_ID").setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(Icon.createWithResource(AIDLTestService.this,R.drawable.ic_avatar)).setContentTitle("标题文本" + i).setContentText("正文文本" + i).setContentIntent(NotificationUtils.createActivityPendingIntent(AIDLTestService.this, "AIDL 测试" + i)).setAutoCancel(true).addAction(0, "按钮 1",NotificationUtils.createActivityPendingIntent(AIDLTestService.this, "按钮 1" + i)).addAction(0, "按钮 2",NotificationUtils.createReceiverPendingIntent(AIDLTestService.this, "按钮 2" + i)).build());}return new ParceledListSlice<>(notifications);}
};

最后在 Activity 里使用如下:

for (Notification notification : aidlTest.getNotifications().getList()) {Log.d(TAG, "onServiceConnected: notification title = "+ notification.extras.get(Notification.EXTRA_TITLE));
}

运行一下,输出结果部分日志如下:

输出结果

以上输出的结果验证了该方案没有问题~

4. 原理分析

下面来分析一下分片传输的原理,首先贴上写操作的代码,代码说明见注释:

@Override
public void writeToParcel(Parcel dest, int flags) {final int N = mList.size();final int callFlags = flags;// 写入数据长度,为了读取时知道数据列表有多少数据dest.writeInt(N);if (DEBUG) Log.d(TAG, "Writing " + N + " items");if (N > 0) {final Class<?> listElementClass = mList.get(0).getClass();// 写入类名,用于读取时获取 ClassLoaderCreatorwriteParcelableCreator(mList.get(0), dest);int i = 0;// 循环写入数据,mInlineCountLimit 可以由调用方指定,默认为 MAX_VALUE;// MAX_IPC_SIZE 为 binder 传输最大大小(64KB)while (i < N && i < mInlineCountLimit && dest.dataSize() < MAX_IPC_SIZE) {// 写入 1 代表一条数据dest.writeInt(1);final T parcelable = mList.get(i);// 校验当前写入的对象是不是和第一个对象是同一个类,如果不是则抛异常,方法定义见后面verifySameType(listElementClass, parcelable.getClass());// 写入当前数据,方法定义见后面writeElement(parcelable, dest, callFlags);if (DEBUG) Log.d(TAG, "Wrote inline #" + i + ": " + mList.get(i));i++;}if (i < N) {// 如果走到这里,说明上面没写完,需要分片传输了,先写个 0 说明还要读取dest.writeInt(0);// 下面写入 binder,这里是核心了,在读取的时候通过拿到 binder,一次一次调用 transact()// 方法,来回调这里的 onTransact() 方法,每次传输尽可能多的数据,以达到分片传输的目的Binder retriever = new Binder() {@Overrideprotected boolean onTransact(int code, Parcel data, Parcel reply, int flags)throws RemoteException {// 如果 code 不为 FIRST_CALL_TRANSACTION 则执行默认操作if (code != FIRST_CALL_TRANSACTION) {return super.onTransact(code, data, reply, flags);}// data 是读取方发来的数据,先告诉我现在读取到列表的哪个位置了int i = data.readInt();if (DEBUG) Log.d(TAG, "Writing more @" + i + " of " + N);// 循环给 reply 写入数据,直到超出或结束while (i < N && reply.dataSize() < MAX_IPC_SIZE) {// 写入 1 代表一条数据reply.writeInt(1);// 验证并写入数据final T parcelable = mList.get(i);verifySameType(listElementClass, parcelable.getClass());writeElement(parcelable, reply, callFlags);if (DEBUG) Log.d(TAG, "Wrote extra #" + i + ": " + mList.get(i));i++;}if (i < N) {// 走到这里写入 0 来代表还没有读完if (DEBUG) Log.d(TAG, "Breaking @" + i + " of " + N);reply.writeInt(0);}return true;}};if (DEBUG) Log.d(TAG, "Breaking @" + i + " of " + N + ": retriever=" + retriever);dest.writeStrongBinder(retriever);}}
}private static void verifySameType(final Class<?> expected, final Class<?> actual) {if (!actual.equals(expected)) {throw new IllegalArgumentException("Can't unparcel type "+ actual.getName() + " in list of type "+ (expected == null ? null : expected.getName()));}
}protected void writeElement(T parcelable, Parcel reply, int callFlags) {parcelable.writeToParcel(reply, callFlags);
}

接下来贴上读取的代码:

private final List<T> mList;private NotificationParceledListSlice(Parcel p, ClassLoader loader) {// 读取数据长度final int N = p.readInt();// 创建数据列表mList = new ArrayList<T>(N);if (DEBUG) Log.d(TAG, "Retrieving " + N + " items");if (N <= 0) {return;}// 通过类名读取 creatorCreator<?> creator = readParcelableCreator(p, loader);Class<?> listElementClass = null;// 循环遍历读取数据int i = 0;while (i < N) {// 这里每读取一条数据的时候 readInt() 都返回 1,如果返回 0 就要走后面的分片读取的逻辑了if (p.readInt() == 0) {break;}// 验证读取数据的类型,并加入数据列表中,方法定义见后面listElementClass = readVerifyAndAddElement(creator, p, loader, listElementClass);if (DEBUG) Log.d(TAG, "Read inline #" + i + ": " + mList.get(mList.size() - 1));i++;}// 如果读取完了就直接 return 掉if (i >= N) {return;}// 读取 binder 来进行分片传输final IBinder retriever = p.readStrongBinder();while (i < N) {if (DEBUG) Log.d(TAG, "Reading more @" + i + " of " + N + ": retriever=" + retriever);Parcel data = Parcel.obtain();Parcel reply = Parcel.obtain();// 写入当前数据列表已经读取的位置data.writeInt(i);try {// 调用 transact 来回调发送方 onTransact() 方法,把数据写到 reply 里retriever.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);} catch (RemoteException e) {Log.w(TAG, "Failure retrieving array; only received " + i + " of " + N, e);return;}// 循环遍历 reply 里的数据,验证并添加数据到列表中while (i < N && reply.readInt() != 0) {listElementClass = readVerifyAndAddElement(creator, reply, loader,listElementClass);if (DEBUG) Log.d(TAG, "Read extra #" + i + ": " + mList.get(mList.size() - 1));i++;}reply.recycle();data.recycle();}
}private Class<?> readVerifyAndAddElement(Creator<?> creator, Parcel p,ClassLoader loader, Class<?> listElementClass) {final T parcelable = readCreator(creator, p, loader);if (listElementClass == null) {listElementClass = parcelable.getClass();} else {verifySameType(listElementClass, parcelable.getClass());}mList.add(parcelable);return listElementClass;
}@SuppressWarnings("unchecked")
private T readCreator(Creator<?> creator, Parcel p, ClassLoader loader) {if (creator instanceof ClassLoaderCreator<?>) {ClassLoaderCreator<?> classLoaderCreator =(ClassLoaderCreator<?>) creator;return (T) classLoaderCreator.createFromParcel(p, loader);}return (T) creator.createFromParcel(p);
}

总结来说就是如果一次传输的数据量过大,Server 端会给 Client 端传递一个 Binder 过去,Client 端拿到这个 Binder 后通过不断调用 transact() 方法来回调 Server 端 Binder 的 onTransact() 方法,然后 Server 端会在 onTransact() 方法里传输下一组数据,如此循环直到所有数据传输完毕。
以上就是分片传输原理代码的分析了,不得不说设计的还是很巧妙的。不过该方案只能解决数据列表过多的问题,对于单个 Parcelable 对象可能存在的过大问题是无法解决的。

以上就是分片传输 Parcelable 数据列表的方案及原理,希望对大家有所帮助。

这篇关于AIDL 如何分片传输大量 Parcelable 数据列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/422072

相关文章

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

浅析如何保证MySQL与Redis数据一致性

《浅析如何保证MySQL与Redis数据一致性》在互联网应用中,MySQL作为持久化存储引擎,Redis作为高性能缓存层,两者的组合能有效提升系统性能,下面我们来看看如何保证两者的数据一致性吧... 目录一、数据不一致性的根源1.1 典型不一致场景1.2 关键矛盾点二、一致性保障策略2.1 基础策略:更新数

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名